From c2ccf2dfa6fee20ba2083b006ad2cc78d6bba029 Mon Sep 17 00:00:00 2001 From: Kozar Date: Wed, 2 Oct 2024 18:39:17 +0000 Subject: [PATCH] Initializacia --- cv1/program.c | 115 ++++++++++++++++++++++++++++++-------------------- 1 file changed, 70 insertions(+), 45 deletions(-) diff --git a/cv1/program.c b/cv1/program.c index 0e8d7d5..358af72 100644 --- a/cv1/program.c +++ b/cv1/program.c @@ -1,60 +1,85 @@ #include -#include +#include #include +#include -#define MAX_INPUT_LENGTH 100 +#define LINESIZE 100 +#define MENU_SIZE 100 -char hack3r_script_mapping[][2] = { - {'0', 'o'}, - {'1', 'i'}, - {'2', 'z'}, - {'3', 'e'}, - {'4', 'a'}, - {'5', 's'}, - {'6', 'b'}, - {'7', 't'}, - {'8', 'b'}, - {'9', 'q'} +struct pizza { + char name[LINESIZE]; + float price; }; +char hacker_script(char l) { + switch (tolower(l)) { + case 'o': return '0'; + case 'i': return '1'; + case 'z': return '2'; + case 'e': return '3'; + case 'a': return '4'; + case 's': return '5'; + case 'b': return '6'; + case 't': return '7'; + case 'q': return '9'; + default: return tolower(l); + } +} + +void transform_to_hacker_script(const char *src, char *dest) { + while (*src) { + *dest++ = hacker_script(*src++); + } + *dest = '\0'; +} + +int contains_normalized(const char *name, const char *search) { + char transformed_name[LINESIZE], transformed_search[LINESIZE]; + + transform_to_hacker_script(name, transformed_name); + transform_to_hacker_script(search, transformed_search); + + return strstr(transformed_name, transformed_search) != NULL; +} + +int read_pizza(struct pizza *item) { + char line[LINESIZE]; + + if (!fgets(item->name, LINESIZE, stdin)) { + return 0; + } + item->name[strcspn(item->name, "\n")] = '\0'; + + if (!fgets(line, LINESIZE, stdin)) { + return 0; + } + + item->price = strtof(line, NULL); + return 1; +} + int main() { - char ingredient[MAX_INPUT_LENGTH]; - char menu_item[MAX_INPUT_LENGTH]; + struct pizza menu[MENU_SIZE]; + char search[LINESIZE]; int count = 0; - printf("Zadaj hladanu surovinu: "); - fgets(ingredient, MAX_INPUT_LENGTH, stdin); - ingredient[strcspn(ingredient, "\n")] = 0; + printf("Zadaj hladanu surovinu:\n"); + if (!fgets(search, LINESIZE, stdin)) { + printf("Error reading input.\n"); + return 1; + } + search[strcspn(search, "\n")] = '\0'; printf("Zadaj jedalny listok:\n"); + while (count < MENU_SIZE && read_pizza(&menu[count])) { + count++; + } - while (1) { - fgets(menu_item, MAX_INPUT_LENGTH, stdin); - if (feof(stdin) || strlen(menu_item) == 1) { - break; - } - menu_item[strcspn(menu_item, "\n")] = 0; - - char name_hack3r[MAX_INPUT_LENGTH]; - int j = 0; - for (int i = 0; menu_item[i]; i++) { - int found = 0; - for (int k = 0; k < 10; k++) { - if (menu_item[i] == hack3r_script_mapping[k][0]) { - name_hack3r[j++] = hack3r_script_mapping[k][1]; - found = 1; - break; - } - } - if (!found) { - name_hack3r[j++] = tolower(menu_item[i]); - } - } - name_hack3r[j] = 0; - - if (strstr(name_hack3r, ingredient) != NULL) { - printf("%s\n", menu_item); - count++; + int found_count = 0; + for (int i = 0; i < count; i++) { + if (contains_normalized(menu[i].name, search)) { + printf("%s\n%.2f\n", menu[i].name, menu[i].price); + found_count++; } }