diff --git a/cv1/program.c b/cv1/program.c index b035fab..36d1c65 100644 --- a/cv1/program.c +++ b/cv1/program.c @@ -1,110 +1,117 @@ #include #include -#include #include +#include + +#define LINE_SIZE 100 +#define MAX_MENU_ITEMS 10 + + +struct pizza { + float price; + char name[LINE_SIZE]; +}; + + +void delet(char* str) { + int i = 0; + while (str[i] != '\n' && str[i] != 0) { + i++; + } + str[i] = 0; +} -#define MAX_ITEMS 100 -#define MAX_NAME_LENGTH 100 char hacker_script(char c) { - char numbers[] = "0123456789"; char letters[] = "oizeasbtbq"; - for (int i = 0; i < 10; i++) { if (c == numbers[i]) { return letters[i]; } } - - if(isupper(c)){ - return tolower(c); - } - return c; + return tolower(c); } -int search_string(const char* aber, const char* b) { - int aber_l = strlen(aber); - int b_l = strlen(b); - - for (int i = 0; i <= aber_l - b_l; i++) { +int search(const char* pop, const char* rar) { + int A = strlen(rar); + int B = strlen(pop); + for (int i = 0; i <= B - A; i++) { int j; - for (j = 0; j < b_l; j++) { - if (hacker_script(aber[i + j]) != hacker_script(aber[j])) { + + for (j = 0; j < A; j++) { + if (hacker_script(pop[i + j]) != hacker_script(rar[j])) { break; } } - if (j == b_l) { - return i; + if (j == A) { + return i; } } - return -1; + return -1; } -struct menu { - char name[MAX_NAME_LENGTH]; - float price; -}; - -int read_pizza(struct menu* pizza) { - char line[MAX_NAME_LENGTH]; - char line2[MAX_NAME_LENGTH]; +int read_pizza(struct pizza* item) { - if (fgets(line, sizeof(line), stdin) == NULL) { - return 0; - } - line[strcspn(line, "\n")] = '\0'; + char line[LINE_SIZE]; + memset(line, 0, LINE_SIZE); - if (fgets(line2, sizeof(line2), stdin) == NULL) { - return 0; // Failed to read price - } - line2[strcspn(line2, "\n")] = '\0'; + char* r = fgets(line, LINE_SIZE, stdin); + delet(line); - float value = strtof(line2, NULL); - if (value == 0.0F) { - return 0; + if (r != NULL && line[1] != 0) { + + char line2[LINE_SIZE]; + + memset(line2, 0, LINE_SIZE); + fgets(line2, LINE_SIZE, stdin); + + float value = strtof(line2, NULL); + + if (value == 0.0F) { + return 0; + } + + item->price = value; + strcpy(item->name, line); + return 1; } - - pizza->price = value; - strcpy(pizza->name, line); - return 1; + return 0; } int main() { - char searchStr[MAX_NAME_LENGTH]; + struct pizza menu[MAX_MENU_ITEMS]; + memset(menu, 0, sizeof(struct pizza) * MAX_MENU_ITEMS); + int item_count = 0; + char search_str[LINE_SIZE]; + printf("Zadaj hladanu surovinu:\n"); - if (fgets(searchStr, sizeof(searchStr), stdin) == NULL) { + if (fgets(search_str, sizeof(search_str), stdin) == NULL) { return 1; } - searchStr[strcspn(searchStr, "\n")] = '\0'; - struct menu* menu = malloc(MAX_ITEMS * sizeof(struct menu)); - if (menu == NULL) { - return 1; - } - - int itemCount = 0; + delet(search_str); printf("Zadaj jedalny listok:\n"); - while (itemCount < MAX_ITEMS) { - struct menu pizza; - if (!read_pizza(&pizza)) { - break; - } - - - if (search_string(pizza.name, searchStr) != -1) { - printf("%s\n", pizza.name); - printf("%.2f\n", pizza.price); - } - - itemCount++; + + struct pizza item; + + while (item_count < MAX_MENU_ITEMS && read_pizza(&item)) { + strcpy(menu[item_count].name, item.name); + menu[item_count].price = item.price; + item_count++; } - free(menu); - printf("Nacitanych %d poloziek.\n", itemCount); + for (int i = 0; i < item_count; i++) { + if (search(menu[i].name, search_str) != -1) { + printf("%s\n", menu[i].name); + printf("%.2f\n", menu[i].price); + } + } + + printf("Nacitanych %d poloziek.\n", item_count); return 0; }