From 851dda9ff8569359a72a21be17e6c91153fbc79c Mon Sep 17 00:00:00 2001 From: an760cw Date: Sun, 1 Oct 2023 14:36:23 +0200 Subject: [PATCH] program.c --- cv1/program.c | 165 +++++++++++++++----------------------------------- 1 file changed, 48 insertions(+), 117 deletions(-) diff --git a/cv1/program.c b/cv1/program.c index 29ea423..f1ae26d 100644 --- a/cv1/program.c +++ b/cv1/program.c @@ -1,128 +1,18 @@ #include #include #include -#include -#define LINESIZE 100 +#define MAX_ITEMS 100 +#define MAX_NAME_LENGTH 100 -struct pizza { +struct menu { + char name[MAX_NAME_LENGTH]; float price; - char name[LINESIZE]; }; -int search_string(const char* a, const char* b); -int read_pizza(struct pizza* element); -void transform(char* str); - -int main() { - struct pizza jedalny_listok[5]; - memset(jedalny_listok, 0, sizeof(struct pizza) * 5); - - int counter = 0; - - char line[LINESIZE]; - memset(line, 0, LINESIZE); - - printf("Zadaj hladanu surovinu:\n"); - fgets(line, LINESIZE, stdin); - int len = strlen(line); - if (len > 0 && line[len - 1] == '\n') { - line[len - 1] = '\0'; // Odstráňye znak nového riadku - } - - if (strlen(line) > 1) { - printf("Zadaj jedalny listok:\n"); - struct pizza element; - - while (read_pizza(&element)) { - strcpy(jedalny_listok[counter].name, element.name); - jedalny_listok[counter].price = element.price; - counter++; - - if (counter >= 5) { - break; - } - } - } - - for (int i = 0; i < counter; i++) { - char transformed_name[LINESIZE]; - strcpy(transformed_name, jedalny_listok[i].name); - transform(transformed_name); - - if (search_string(transformed_name, line) != -1) { - printf("%s\n", jedalny_listok[i].name); - printf("%.2f\n", jedalny_listok[i].price); - } - } - - printf("Nacitanych %d poloziek.\n", counter); - - return 0; -} - - -int search_string(const char* a, const char* b) { - int c = strlen(b); - int d = strlen(a); - - for (int i = 0; i <= d - c; i++) { - int j; - for (j = 0; j < c; j++) { - if (a[i + j] != b[j]) { - break; - } - } - if (j == c) { - return i; - } - } - return -1; -} - -int read_pizza(struct pizza* element) { - char line[LINESIZE]; - memset(line, 0, LINESIZE); - - if (fgets(line, LINESIZE, stdin) == NULL) { - return 0; - } - - int len = strlen(line); - if (len > 0 && line[len - 1] == '\n') { - line[len - 1] = '\0'; - } - - if (line[0] == '\0') { - return 0; - } - - //if (line[1] == 0) { - //return 0; - //} - - char line2[LINESIZE]; - memset(line2, 0, LINESIZE); - - if (fgets(line2, LINESIZE, stdin) == NULL) { - return 0; - } - - float value = strtof(line2, NULL); - - if (value == 0.0F) { - return 0; - } - - element->price = value; - strcpy(element->name, line); - return 1; -} - -void transform(char* str) { +void transform(char *str) { for (int i = 0; str[i]; i++) { - if (str[i] != '\n'){ - switch (str[i]) { + switch (str[i]) { case '0': str[i] = 'o'; break; case '1': str[i] = 'i'; break; case '2': str[i] = 'z'; break; @@ -136,5 +26,46 @@ void transform(char* str) { default: break; } } - } +} + +int main() { + char searchStr[MAX_NAME_LENGTH]; + printf("Zadaj hladanu surovinu:\n"); + if (fgets(searchStr, sizeof(searchStr), stdin) == NULL) { + return 1; + } + searchStr[strcspn(searchStr, "\n")] = '\0'; // Odstrániť znak nového riadku + + struct menu menu[MAX_ITEMS]; + int itemCount = 0; + + printf("Zadaj jedalny listok:\n"); + while (itemCount < MAX_ITEMS) { + char pizza_name[MAX_NAME_LENGTH]; + if (fgets(pizza_name, sizeof(pizza_name), stdin) == NULL) { + break; // Koniec vstupu + } + pizza_name[strcspn(pizza_name, "\n")] = '\0'; // Odstrániť znak nového riadku + // Transformácia názvu jedla + transform(pizza_name); + + // Načítanie ceny jedla + float itemPrice; + if (scanf("%f", &itemPrice) != 1) { + break; // Chybný vstup + } + getchar(); // Načítať znak nového riadku + + // Skontrolujte, či názov obsahuje vyhľadávací reťazec + if (strstr(pizza_name, searchStr) != NULL) { + printf("%s\n", pizza_name); + printf("%.2f\n", itemPrice); + } + + itemCount++; + } + + printf("Nacitanych %d poloziek.\n", itemCount); + + return 0; }