first
This commit is contained in:
parent
8890bbf80f
commit
d051758d35
@ -10,6 +10,9 @@ struct pizza{
|
||||
float prize;
|
||||
char name[LINE_SIZE];
|
||||
};
|
||||
/* structura jednej polozky*/
|
||||
|
||||
|
||||
|
||||
char hacker_script(char c){
|
||||
c = tolower((unsigned char)c);
|
||||
@ -26,15 +29,17 @@ char hacker_script(char c){
|
||||
case '9': return 'q';
|
||||
default: return c;}
|
||||
}
|
||||
/*hacker script nam bude vymenovat cisla na bukvy*/
|
||||
|
||||
|
||||
|
||||
void normalize(const char *source, char *dest){
|
||||
int i =0;
|
||||
while(source[i]!='\0'){
|
||||
dest[i]= hacker_script(source[i]);
|
||||
while(source[i]!='\0'){ /* tu je cyklus ktory precahdza slovom a normalizuje ho*/
|
||||
dest[i]= hacker_script(source[i]);
|
||||
i++;
|
||||
}
|
||||
dest[i]='\0';
|
||||
dest[i]='\0'; ///ked slovo konci, cyklus tiez skonci a na konci bude terminator
|
||||
}
|
||||
|
||||
|
||||
@ -42,21 +47,22 @@ void normalize(const char *source, char *dest){
|
||||
|
||||
|
||||
|
||||
int read_pizza(struct pizza *item){
|
||||
char line_1[LINE_SIZE];
|
||||
char line_2[LINE_SIZE];
|
||||
|
||||
if (fgets(line_1,LINE_SIZE,stdin)==NULL) return 0;
|
||||
int read_pizza(struct pizza *item){
|
||||
char line_1[LINE_SIZE]; /// pre name
|
||||
char line_2[LINE_SIZE]; /// pre price
|
||||
|
||||
if (fgets(line_1,LINE_SIZE,stdin)==NULL) return 0; /// nacitava name, ak line_1 bude null tak nastala chyba
|
||||
if (fgets(line_2,LINE_SIZE,stdin)==NULL) return 0;
|
||||
|
||||
line_1[strcspn(line_1,"\n")] = '\0';
|
||||
line_1[strcspn(line_1,"\n")] = '\0'; /// ukoncuje terminatorom
|
||||
line_2[strcspn(line_2,"\n")] = '\0';
|
||||
|
||||
float price = 0.0;
|
||||
if (sscanf(line_2, "%f", &price)!=1) return 0;
|
||||
if (sscanf(line_2, "%f", &price)!=1) return 0;// nacitava len float number
|
||||
|
||||
strcpy(item->name, line_1);
|
||||
item->prize = price;
|
||||
item->prize = price; // uklada name a price do itemu
|
||||
|
||||
return 1;
|
||||
}
|
||||
@ -66,29 +72,31 @@ int main(){
|
||||
char search[LINE_SIZE];
|
||||
char normal_search[LINE_SIZE];
|
||||
|
||||
struct pizza list[MAXIT];
|
||||
int cnt = 0;
|
||||
struct pizza list[MAXIT];
|
||||
int cnt = 0;
|
||||
|
||||
printf("Zadaj hladanu surovinu:\n");
|
||||
if (fgets(search, LINE_SIZE, stdin)==NULL) return 0;
|
||||
search[strcspn(search,"\n")] = '\0';
|
||||
|
||||
normalize(search, normal_search);
|
||||
normalize(search, normal_search); // normalizujeme nas hladany prvok
|
||||
|
||||
printf("Zadaj jedalny listok:\n");
|
||||
|
||||
|
||||
while(cnt<MAXIT&&read_pizza(&list[cnt])){ cnt++;}
|
||||
while(cnt<MAXIT&&read_pizza(&list[cnt])){ cnt++;} // ak cnt menej ako MAXIT aj je nacitana jedna polozka
|
||||
|
||||
for (int i =0;i<cnt; i++){
|
||||
char normal_name[LINE_SIZE];
|
||||
normalize(list[i].name, normal_name);
|
||||
normalize(list[i].name, normal_name); // normalizujeme meno
|
||||
|
||||
if (strstr(normal_name, normal_search) != NULL) {
|
||||
if (strstr(normal_name, normal_search) != NULL) { // ak mame to co hladame
|
||||
printf("%s\n", list[i].name);
|
||||
printf("%.2f\n", list[i].prize);
|
||||
printf("%.2f\n", list[i].prize); // vyprintujeme
|
||||
}
|
||||
|
||||
}
|
||||
printf("Nacitanych %d poloziek.\n", cnt);
|
||||
} // ak ne bude nic podobne, cyklus po nacitavani vsetkych poloziek ukonci
|
||||
|
||||
printf("Nacitanych %d poloziek.\n", cnt); // vypise cislo poloziek
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -13,19 +13,19 @@ struct pizza{
|
||||
|
||||
|
||||
int read_pizza(struct pizza *item){
|
||||
char line_1[LINE_SIZE];
|
||||
char line_2[LINE_SIZE];
|
||||
char line_1[LINE_SIZE]; //nazov
|
||||
char line_2[LINE_SIZE];//cena
|
||||
|
||||
if (fgets(line_1,LINE_SIZE,stdin)==NULL) return 0;
|
||||
if (fgets(line_1,LINE_SIZE,stdin)==NULL) return 0;//ak nie je vstup return o
|
||||
if (fgets(line_2,LINE_SIZE,stdin)==NULL) return 0;
|
||||
|
||||
line_1[strcspn(line_1,"\n")] = '\0';
|
||||
line_1[strcspn(line_1,"\n")] = '\0';//odstrani znak rovnakeho riadku
|
||||
line_2[strcspn(line_2,"\n")] = '\0';
|
||||
|
||||
float price = 0.0;
|
||||
if (sscanf(line_2, "%f", &price)!=1||price<=0) return 0;
|
||||
if (sscanf(line_2, "%f", &price)!=1||price<=0) return 0;//kontrola vystupy
|
||||
|
||||
strcpy(item->name, line_1);
|
||||
strcpy(item->name, line_1);// skopiruje nazov do struktury
|
||||
item->prize = price;
|
||||
|
||||
return 1;
|
||||
@ -34,14 +34,14 @@ int read_pizza(struct pizza *item){
|
||||
|
||||
int read_pizza_list(struct pizza* list){
|
||||
int counter = 0;
|
||||
for(int i = 0; i<MAXIT;i++){
|
||||
struct pizza item;
|
||||
memset(&item,0,sizeof(struct pizza));
|
||||
int r = read_pizza(&item);
|
||||
if (r){
|
||||
memcpy(&list[counter], &item, sizeof(struct pizza));
|
||||
counter++;
|
||||
} else {
|
||||
for(int i = 0; i<MAXIT;i++){ // cyklus nacitava vsetky pizzy
|
||||
struct pizza item; //pomocna premenna
|
||||
memset(&item,0,sizeof(struct pizza)); // vydelime pamat
|
||||
int r = read_pizza(&item); //nacitajme jednu pizzu
|
||||
if (r){ // ak nacitana
|
||||
memcpy(&list[counter], &item, sizeof(struct pizza)); // kopirujeme z itemu do naseho menu
|
||||
counter++; // cnt+1
|
||||
} else { // ak nie
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -50,24 +50,24 @@ int read_pizza_list(struct pizza* list){
|
||||
|
||||
|
||||
int compare_pizza (const void * a, const void * b){
|
||||
const struct pizza *p1 = (const struct pizza *)a;
|
||||
const struct pizza *p2 = (const struct pizza *)b;
|
||||
const struct pizza *p1 = (const struct pizza *)a; // pointer na prvu pizzu
|
||||
const struct pizza *p2 = (const struct pizza *)b; // pointer na druhu
|
||||
|
||||
if(p1->prize<p2->prize) return -1;
|
||||
if(p1->prize<p2->prize) return -1;
|
||||
if(p1->prize>p2->prize) return 1;
|
||||
|
||||
return strcmp(p1->name,p2->name);
|
||||
return strcmp(p1->name,p2->name); // ak cena bude ta ista, porovname mena
|
||||
|
||||
}
|
||||
|
||||
|
||||
int main(){
|
||||
struct pizza list[MAXIT];
|
||||
memset(list,0,sizeof(list));
|
||||
memset(list,0,sizeof(list)); // vydelime pamat pre menu
|
||||
|
||||
int count = read_pizza_list(list);
|
||||
|
||||
qsort(list,count,sizeof(struct pizza), compare_pizza);
|
||||
qsort(list,count,sizeof(struct pizza), compare_pizza); //triedenie menu
|
||||
for (int i = 0;i<count;i++){
|
||||
printf("%s\n%.6f\n",list[i].name,list[i].prize);
|
||||
}
|
||||
|
||||
@ -13,10 +13,10 @@ struct stack {
|
||||
|
||||
|
||||
void init(struct stack *s){
|
||||
s->size=0;
|
||||
s->size=0; //inicializacia stacku
|
||||
}
|
||||
|
||||
int is_empty(struct stack *s){return s->size==0;}
|
||||
int is_empty(struct stack *s){return s->size==0;} // check if stack is empty
|
||||
int is_full(struct stack *s){return s->size==STACK_SIZE;}
|
||||
|
||||
|
||||
@ -25,7 +25,7 @@ void push(struct stack *s, float value){
|
||||
printf("full stack\n");
|
||||
exit(0);
|
||||
}
|
||||
s->values[s->size++]=value;
|
||||
s->values[s->size++]=value; // pridava cislo do stacku a size +=1
|
||||
}
|
||||
|
||||
float pop (struct stack *s){
|
||||
@ -33,7 +33,7 @@ float pop (struct stack *s){
|
||||
printf("Stack is empty");
|
||||
return 1;
|
||||
}
|
||||
return s->values[--(s->size)];
|
||||
return s->values[--(s->size)]; // najprv - size, potom vybera jednu polozku
|
||||
}
|
||||
|
||||
|
||||
@ -55,13 +55,13 @@ int main(){
|
||||
input[strcspn(input,"\n")]='\0';
|
||||
|
||||
char *endpt;
|
||||
float num = strtod(input,&endpt);
|
||||
float num = strtod(input,&endpt); // oddeli vsetky cisla (medzerami) a posledne zapise do endpt
|
||||
|
||||
if(endpt!=input&&*endpt=='\0'){
|
||||
if(endpt!=input&&*endpt=='\0'){ // ked endpt!= plnomu inputu a nie je koncom inputu
|
||||
push(&mystack,num);
|
||||
}
|
||||
else if (strlen(input)==1&&strchr("+-*/",input[0])){
|
||||
if (mystack.size<2){
|
||||
else if (strlen(input)==1&&strchr("+-*/",input[0])){ // ak dlzka inputu po upravach 1 a to nojeaky operator
|
||||
if (mystack.size<2){ // previerka na to ci mame cisla pre operacie
|
||||
printf("not enough operands\n");
|
||||
return 0;}
|
||||
float b=pop(&mystack);
|
||||
|
||||
13
du4/Makefile
Normal file
13
du4/Makefile
Normal file
@ -0,0 +1,13 @@
|
||||
CFLAGS= -std=c99 -g -Wall
|
||||
|
||||
all: train
|
||||
|
||||
%.o: %.c
|
||||
gcc -c -o $@ $< $(CFLAGS)
|
||||
|
||||
train: main.o a_train.o
|
||||
gcc main.o a_train.o -o train
|
||||
|
||||
clean:
|
||||
rm *.o train
|
||||
|
||||
58
du4/a_train.c
Normal file
58
du4/a_train.c
Normal file
@ -0,0 +1,58 @@
|
||||
#include "a_train.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
struct car* add_car(struct car* first,const char* target) {
|
||||
struct car* new = calloc(1, sizeof(struct car));
|
||||
if(!new) return first;
|
||||
strcpy(new->value,target);
|
||||
if (first==NULL){
|
||||
return new;
|
||||
}
|
||||
|
||||
struct car* sm = first;
|
||||
while(sm->next!=NULL){
|
||||
sm = sm->next;
|
||||
}
|
||||
sm->next = new;
|
||||
return first;
|
||||
|
||||
}
|
||||
|
||||
void print_train(struct car* first) {
|
||||
struct car* hlp = first;
|
||||
while(hlp!=NULL){
|
||||
printf("Cielova stanica je %s\n",hlp->value);
|
||||
hlp = hlp->next;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void cancel_train(struct car* first) {
|
||||
if(!first) return;
|
||||
cancel_train(first->next);
|
||||
free(first);
|
||||
}
|
||||
|
||||
|
||||
struct car* clear_train(struct car* first, const char* target) {
|
||||
if(!first) return NULL;
|
||||
if (strcmp(first->value, target)==0){
|
||||
struct car* tmp = first->next;
|
||||
free(first);
|
||||
return tmp;
|
||||
}
|
||||
struct car* hlp = first;
|
||||
while (hlp->next!=NULL&&strcmp(hlp->next->value,target)!=0){
|
||||
hlp = hlp->next;
|
||||
}
|
||||
if (hlp->next!=NULL&&strcmp(hlp->next->value,target)==0){
|
||||
struct car* del = hlp->next;
|
||||
hlp->next=hlp->next->next;
|
||||
free(del);
|
||||
}
|
||||
return first;
|
||||
}
|
||||
|
||||
51
du4/a_train.h
Normal file
51
du4/a_train.h
Normal file
@ -0,0 +1,51 @@
|
||||
#ifndef TRAIN_H
|
||||
#define TRAIN_H
|
||||
#define SIZE 100
|
||||
|
||||
/**
|
||||
* Jeden vozen vlaku
|
||||
*/
|
||||
struct car {
|
||||
/**
|
||||
* Nazov cielovej stanice
|
||||
*/
|
||||
char value[SIZE];
|
||||
/**
|
||||
* Smenik na dalsi vozen
|
||||
*/
|
||||
struct car* next;
|
||||
};
|
||||
|
||||
/**
|
||||
* Prida vozen na koniec vlaku.
|
||||
*
|
||||
* @arg nazov cielovej stanice, ktory sa ma priradit novemu voznu.
|
||||
* @return smernik na zaciatok vlaku.
|
||||
*/
|
||||
struct car* add_car(struct car* first,const char* target);
|
||||
|
||||
|
||||
/**
|
||||
* Vypise vsetky vozne vo vlaku
|
||||
*
|
||||
* @arg smernik na prvy vozen
|
||||
*/
|
||||
void print_train(struct car* first);
|
||||
|
||||
/**
|
||||
* Zrusenie vsetkych voznov vo vlaku.
|
||||
* @arg smernik na prvy vozen
|
||||
*/
|
||||
void cancel_train(struct car* first);
|
||||
|
||||
/**
|
||||
* Vyradenie vsetkych voznov, ktorych cielova stanica je target
|
||||
*
|
||||
* @arg smernik na prvy vozen
|
||||
* @arg cielova stanica, ktora sa ma vyradit z vlaku.
|
||||
* @return smernik na novy prvy vozen
|
||||
*
|
||||
*/
|
||||
struct car* clear_train(struct car* first,const char* target);
|
||||
|
||||
#endif // TRAIN_H
|
||||
BIN
du4/a_train.o
Normal file
BIN
du4/a_train.o
Normal file
Binary file not shown.
17
du4/main.c
Normal file
17
du4/main.c
Normal file
@ -0,0 +1,17 @@
|
||||
#include "a_train.h"
|
||||
#include <stdio.h>
|
||||
|
||||
// Testovaci subor pre vlak
|
||||
int main(){
|
||||
struct car* train = NULL;
|
||||
train = add_car(train,"Presov");
|
||||
train = add_car(train,"Bratislava");
|
||||
train = add_car(train,"Levoca");
|
||||
train = add_car(train,"Spiska Nova Ves");
|
||||
print_train(train);
|
||||
clear_train(train,"Levoca");
|
||||
print_train(train);
|
||||
cancel_train(train);
|
||||
|
||||
return 0;
|
||||
}
|
||||
BIN
du4/main.o
Normal file
BIN
du4/main.o
Normal file
Binary file not shown.
Loading…
Reference in New Issue
Block a user