From c2c30fbd59e2c1874d42e6aedb293405ccdf9f3f Mon Sep 17 00:00:00 2001 From: Kozar Date: Wed, 2 Oct 2024 12:27:38 +0000 Subject: [PATCH 01/35] Initializacia --- cv1/program.c | 104 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 102 insertions(+), 2 deletions(-) diff --git a/cv1/program.c b/cv1/program.c index 4f791d9..556d861 100644 --- a/cv1/program.c +++ b/cv1/program.c @@ -1,5 +1,105 @@ #include -int main() { - return 0; +#include +#include +#include + +#define LINESIZE 100 +#define MENU_SiZE 100 + +// Štruktúra pre položku jedálneho lístka +struct pizza{ + char name[LINESIZE]; + float price; } +// Prepisovací algoritmus pre "Hack3r scr1pt" +char hacker_script(char l){ + switch(tolower(char 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(c); + } +} + +// Function to transform a string into "Hack3r scr1pt" +void transform_to_hacker_script(const char *src, char *dest) { + while (*src) { + *dest++ = hacker_script(*src++); + } + *dest = '\0'; +} + +// Function to search for a normalized string in the name +int contains_normalized(const char *name, const char *search) { + char transformed_name[LINESIZE], transformed_search[LINESIZE]; + + // Transform both strings + transform_to_hacker_script(name, transformed_name); + transform_to_hacker_script(search, transformed_search); + + // Search for the string in the name + return strstr(transformed_name, transformed_search) != NULL; +} + +// Function to read a single menu item +int read_pizza(struct pizza *item) { + char line[LINESIZE]; + + // Read the name + if (!fgets(item->name, LINESIZE, stdin)) { + return 0; // End of input + } + + // Remove newline character + item->name[strcspn(item->name, "\n")] = '\0'; + + // Read the price + if (!fgets(line, LINESIZE, stdin)) { + return 0; // Failed to read price + } + + // Convert string to float + item->price = strtof(line, NULL); + return 1; // Successfully read +} + +int main() { + struct pizza menu[MENU_SIZE]; + char search[LINESIZE]; + int count = 0; + + // Read the search string + printf("Zadaj hladanu surovinu:\n"); + fgets(search, LINESIZE, stdin); + search[strcspn(search, "\n")] = '\0'; // Remove newline + + printf("Zadaj jedalny listok:\n"); + + // Read the menu items + while (read_pizza(&menu[count])) { + count++; + } + + printf("\nVysledky vyhladavania:\n"); + + // Search and print items that match the search string + 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++; + } + } + + // Print the total number of loaded items + printf("Nacitanych %d poloziek.\n", count);s + + return 0; +} From 831b080f14bdb7ef30ce23711048f3e0ca852240 Mon Sep 17 00:00:00 2001 From: Kozar Date: Wed, 2 Oct 2024 12:28:51 +0000 Subject: [PATCH 02/35] Initializacia --- cv1/program.c | 27 ++++++--------------------- 1 file changed, 6 insertions(+), 21 deletions(-) diff --git a/cv1/program.c b/cv1/program.c index 556d861..f740b98 100644 --- a/cv1/program.c +++ b/cv1/program.c @@ -6,13 +6,11 @@ #define LINESIZE 100 #define MENU_SiZE 100 -// Štruktúra pre položku jedálneho lístka struct pizza{ char name[LINESIZE]; float price; } -// Prepisovací algoritmus pre "Hack3r scr1pt" char hacker_script(char l){ switch(tolower(char l)){ case 'o': return '0'; @@ -28,7 +26,6 @@ char hacker_script(char l){ } } -// Function to transform a string into "Hack3r scr1pt" void transform_to_hacker_script(const char *src, char *dest) { while (*src) { *dest++ = hacker_script(*src++); @@ -36,38 +33,30 @@ void transform_to_hacker_script(const char *src, char *dest) { *dest = '\0'; } -// Function to search for a normalized string in the name int contains_normalized(const char *name, const char *search) { char transformed_name[LINESIZE], transformed_search[LINESIZE]; - - // Transform both strings + transform_to_hacker_script(name, transformed_name); transform_to_hacker_script(search, transformed_search); - // Search for the string in the name return strstr(transformed_name, transformed_search) != NULL; } -// Function to read a single menu item int read_pizza(struct pizza *item) { char line[LINESIZE]; - // Read the name if (!fgets(item->name, LINESIZE, stdin)) { - return 0; // End of input + return 0; } - // Remove newline character item->name[strcspn(item->name, "\n")] = '\0'; - // Read the price if (!fgets(line, LINESIZE, stdin)) { - return 0; // Failed to read price + return 0; } - // Convert string to float item->price = strtof(line, NULL); - return 1; // Successfully read + return 1; } int main() { @@ -75,21 +64,18 @@ int main() { char search[LINESIZE]; int count = 0; - // Read the search string printf("Zadaj hladanu surovinu:\n"); fgets(search, LINESIZE, stdin); - search[strcspn(search, "\n")] = '\0'; // Remove newline + search[strcspn(search, "\n")] = '\0'; printf("Zadaj jedalny listok:\n"); - // Read the menu items while (read_pizza(&menu[count])) { count++; } printf("\nVysledky vyhladavania:\n"); - // Search and print items that match the search string int found_count = 0; for (int i = 0; i < count; i++) { if (contains_normalized(menu[i].name, search)) { @@ -97,8 +83,7 @@ int main() { found_count++; } } - - // Print the total number of loaded items + printf("Nacitanych %d poloziek.\n", count);s return 0; From d0d1dc242097e11967c18bd7f847bd684638e487 Mon Sep 17 00:00:00 2001 From: Kozar Date: Wed, 2 Oct 2024 12:33:13 +0000 Subject: [PATCH 03/35] Initializacia --- cv1/program.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/cv1/program.c b/cv1/program.c index f740b98..601e9d4 100644 --- a/cv1/program.c +++ b/cv1/program.c @@ -4,15 +4,15 @@ #include #define LINESIZE 100 -#define MENU_SiZE 100 +#define MENU_SIZE 100 -struct pizza{ +struct pizza { char name[LINESIZE]; float price; -} +}; -char hacker_script(char l){ - switch(tolower(char l)){ +char hacker_script(char l) { + switch (tolower(l)) { case 'o': return '0'; case 'i': return '1'; case 'z': return '2'; @@ -22,7 +22,7 @@ char hacker_script(char l){ case 'b': return '6'; case 't': return '7'; case 'q': return '9'; - default: return tolower(c); + default: return tolower(l); } } @@ -48,7 +48,6 @@ int read_pizza(struct pizza *item) { if (!fgets(item->name, LINESIZE, stdin)) { return 0; } - item->name[strcspn(item->name, "\n")] = '\0'; if (!fgets(line, LINESIZE, stdin)) { @@ -65,8 +64,9 @@ int main() { int count = 0; printf("Zadaj hladanu surovinu:\n"); + fgets(search, LINESIZE, stdin); - search[strcspn(search, "\n")] = '\0'; + search[strcspn(search, "\n")] = '\0'; printf("Zadaj jedalny listok:\n"); @@ -83,8 +83,8 @@ int main() { found_count++; } } - - printf("Nacitanych %d poloziek.\n", count);s + + printf("Nacitanych %d poloziek.\n", count); return 0; } From 2eb10876da7fe90368e3e647ba6d4979097c0719 Mon Sep 17 00:00:00 2001 From: Kozar Date: Wed, 2 Oct 2024 16:57:36 +0000 Subject: [PATCH 04/35] Initializacia --- cv1/program.c | 3 --- 1 file changed, 3 deletions(-) diff --git a/cv1/program.c b/cv1/program.c index 601e9d4..c3fca68 100644 --- a/cv1/program.c +++ b/cv1/program.c @@ -64,18 +64,15 @@ int main() { int count = 0; printf("Zadaj hladanu surovinu:\n"); - fgets(search, LINESIZE, stdin); search[strcspn(search, "\n")] = '\0'; printf("Zadaj jedalny listok:\n"); - while (read_pizza(&menu[count])) { count++; } printf("\nVysledky vyhladavania:\n"); - int found_count = 0; for (int i = 0; i < count; i++) { if (contains_normalized(menu[i].name, search)) { From 127fb512251d5d4bafc620165dfb691ff0649205 Mon Sep 17 00:00:00 2001 From: Kozar Date: Wed, 2 Oct 2024 16:59:35 +0000 Subject: [PATCH 05/35] Initializacia --- cv1/program.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/cv1/program.c b/cv1/program.c index c3fca68..4a2305f 100644 --- a/cv1/program.c +++ b/cv1/program.c @@ -81,6 +81,10 @@ int main() { } } + if (found_count == 0) { + printf("\n"); + } + printf("Nacitanych %d poloziek.\n", count); return 0; From 969442f6e7ffc915346ee316d7845dfe12e655c6 Mon Sep 17 00:00:00 2001 From: Kozar Date: Wed, 2 Oct 2024 17:03:28 +0000 Subject: [PATCH 06/35] Initializacia --- cv1/program.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/cv1/program.c b/cv1/program.c index 4a2305f..be31ec9 100644 --- a/cv1/program.c +++ b/cv1/program.c @@ -67,6 +67,10 @@ int main() { fgets(search, LINESIZE, stdin); search[strcspn(search, "\n")] = '\0'; + for (int i = 0; search[i]; i++) { + search[i] = tolower(search[i]); + } + printf("Zadaj jedalny listok:\n"); while (read_pizza(&menu[count])) { count++; From f7276b6c986bde2f8218aceebab3a9b7767ad463 Mon Sep 17 00:00:00 2001 From: Kozar Date: Wed, 2 Oct 2024 17:06:57 +0000 Subject: [PATCH 07/35] Initializacia --- cv1/program.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/cv1/program.c b/cv1/program.c index be31ec9..69dddbf 100644 --- a/cv1/program.c +++ b/cv1/program.c @@ -54,7 +54,12 @@ int read_pizza(struct pizza *item) { return 0; } - item->price = strtof(line, NULL); + char *endptr; + item->price = strtof(line, &endptr); + if (endptr == line || *endptr != '\n') { + return 0; + } + return 1; } From 9732f585438c81c1a95b74080ac55e1810e344d3 Mon Sep 17 00:00:00 2001 From: Kozar Date: Wed, 2 Oct 2024 17:11:25 +0000 Subject: [PATCH 08/35] Initializacia --- cv1/program.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cv1/program.c b/cv1/program.c index 69dddbf..db2dd66 100644 --- a/cv1/program.c +++ b/cv1/program.c @@ -39,7 +39,7 @@ int contains_normalized(const char *name, const char *search) { transform_to_hacker_script(name, transformed_name); transform_to_hacker_script(search, transformed_search); - return strstr(transformed_name, transformed_search) != NULL; + return strcasestr(transformed_name, transformed_search) != NULL; } int read_pizza(struct pizza *item) { From eccfb157bf7cb81507e70c6c9fd3b538a6f65c53 Mon Sep 17 00:00:00 2001 From: Kozar Date: Wed, 2 Oct 2024 17:14:16 +0000 Subject: [PATCH 09/35] Initializacia --- cv1/program.c | 1 - 1 file changed, 1 deletion(-) diff --git a/cv1/program.c b/cv1/program.c index db2dd66..6cf9a27 100644 --- a/cv1/program.c +++ b/cv1/program.c @@ -81,7 +81,6 @@ int main() { count++; } - printf("\nVysledky vyhladavania:\n"); int found_count = 0; for (int i = 0; i < count; i++) { if (contains_normalized(menu[i].name, search)) { From b5fbf7414eec2c02a46c57824df27d6da029a043 Mon Sep 17 00:00:00 2001 From: Kozar Date: Wed, 2 Oct 2024 17:27:29 +0000 Subject: [PATCH 10/35] Initializacia --- cv1/program.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/cv1/program.c b/cv1/program.c index 6cf9a27..86ab3b1 100644 --- a/cv1/program.c +++ b/cv1/program.c @@ -28,7 +28,12 @@ char hacker_script(char l) { void transform_to_hacker_script(const char *src, char *dest) { while (*src) { - *dest++ = hacker_script(*src++); + if (isupper(*src)) { + *dest++ = tolower(*src); + } else { + *dest++ = hacker_script(*src); + } + src++; } *dest = '\0'; } From 4f08259a05ef5c32adca28dcd4295f51529dc48d Mon Sep 17 00:00:00 2001 From: Kozar Date: Wed, 2 Oct 2024 17:32:07 +0000 Subject: [PATCH 11/35] Initializacia --- cv1/program.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/cv1/program.c b/cv1/program.c index 86ab3b1..2240865 100644 --- a/cv1/program.c +++ b/cv1/program.c @@ -94,8 +94,13 @@ int main() { } } - if (found_count == 0) { - printf("\n"); + if (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++; + } + } } printf("Nacitanych %d poloziek.\n", count); From 43922459dc795f137ec4cd7cd241795275bccb90 Mon Sep 17 00:00:00 2001 From: Kozar Date: Wed, 2 Oct 2024 17:33:56 +0000 Subject: [PATCH 12/35] Initializacia --- cv1/program.c | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/cv1/program.c b/cv1/program.c index 2240865..b45f7ef 100644 --- a/cv1/program.c +++ b/cv1/program.c @@ -87,14 +87,8 @@ int main() { } 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++; - } - } - if (found_count > 0) { + printf("\nVysledky vyhladavania:\n"); 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); From b944c638ad18aec1b8ee52ce53610e53ed487525 Mon Sep 17 00:00:00 2001 From: Kozar Date: Wed, 2 Oct 2024 17:38:24 +0000 Subject: [PATCH 13/35] Initializacia --- cv1/program.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/cv1/program.c b/cv1/program.c index b45f7ef..59381f4 100644 --- a/cv1/program.c +++ b/cv1/program.c @@ -87,13 +87,13 @@ int main() { } int found_count = 0; - if (found_count > 0) { - printf("\nVysledky vyhladavania:\n"); - 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++; + for (int i = 0; i < count; i++) { + if (contains_normalized(menu[i].name, search)) { + if (found_count == 0) { + printf("\nVysledky vyhladavania:\n"); } + printf("%s\n%.2f\n", menu[i].name, menu[i].price); + found_count++; } } From d447a22ab1829c91ae6d074da63c9587c5eb9f23 Mon Sep 17 00:00:00 2001 From: Kozar Date: Wed, 2 Oct 2024 17:59:06 +0000 Subject: [PATCH 14/35] Initializacia --- cv1/program.c | 23 +++-------------------- 1 file changed, 3 insertions(+), 20 deletions(-) diff --git a/cv1/program.c b/cv1/program.c index 59381f4..ce7f787 100644 --- a/cv1/program.c +++ b/cv1/program.c @@ -28,12 +28,7 @@ char hacker_script(char l) { void transform_to_hacker_script(const char *src, char *dest) { while (*src) { - if (isupper(*src)) { - *dest++ = tolower(*src); - } else { - *dest++ = hacker_script(*src); - } - src++; + *dest++ = hacker_script(*src++); } *dest = '\0'; } @@ -44,7 +39,7 @@ int contains_normalized(const char *name, const char *search) { transform_to_hacker_script(name, transformed_name); transform_to_hacker_script(search, transformed_search); - return strcasestr(transformed_name, transformed_search) != NULL; + return strstr(transformed_name, transformed_search) != NULL; } int read_pizza(struct pizza *item) { @@ -59,12 +54,7 @@ int read_pizza(struct pizza *item) { return 0; } - char *endptr; - item->price = strtof(line, &endptr); - if (endptr == line || *endptr != '\n') { - return 0; - } - + item->price = strtof(line, NULL); return 1; } @@ -77,10 +67,6 @@ int main() { fgets(search, LINESIZE, stdin); search[strcspn(search, "\n")] = '\0'; - for (int i = 0; search[i]; i++) { - search[i] = tolower(search[i]); - } - printf("Zadaj jedalny listok:\n"); while (read_pizza(&menu[count])) { count++; @@ -89,9 +75,6 @@ int main() { int found_count = 0; for (int i = 0; i < count; i++) { if (contains_normalized(menu[i].name, search)) { - if (found_count == 0) { - printf("\nVysledky vyhladavania:\n"); - } printf("%s\n%.2f\n", menu[i].name, menu[i].price); found_count++; } From ba3d6439c03817829a7f1878f9f8fc3e5a11703d Mon Sep 17 00:00:00 2001 From: Kozar Date: Wed, 2 Oct 2024 18:09:27 +0000 Subject: [PATCH 15/35] Initializacia --- cv1/program.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/cv1/program.c b/cv1/program.c index ce7f787..5ce3fb2 100644 --- a/cv1/program.c +++ b/cv1/program.c @@ -75,6 +75,9 @@ int main() { int found_count = 0; for (int i = 0; i < count; i++) { if (contains_normalized(menu[i].name, search)) { + if (found_count == 0) { + printf("\nVysledky vyhladavania:\n"); + } printf("%s\n%.2f\n", menu[i].name, menu[i].price); found_count++; } From 05d60af8c23c50c99fec12866b7e4ac0aa52230f Mon Sep 17 00:00:00 2001 From: Kozar Date: Wed, 2 Oct 2024 18:13:40 +0000 Subject: [PATCH 16/35] Initializacia --- cv1/program.c | 3 --- 1 file changed, 3 deletions(-) diff --git a/cv1/program.c b/cv1/program.c index 5ce3fb2..ce7f787 100644 --- a/cv1/program.c +++ b/cv1/program.c @@ -75,9 +75,6 @@ int main() { int found_count = 0; for (int i = 0; i < count; i++) { if (contains_normalized(menu[i].name, search)) { - if (found_count == 0) { - printf("\nVysledky vyhladavania:\n"); - } printf("%s\n%.2f\n", menu[i].name, menu[i].price); found_count++; } From 6a822d93c1efe79948fb4dfca383cb2ffb135fe4 Mon Sep 17 00:00:00 2001 From: Kozar Date: Wed, 2 Oct 2024 18:20:27 +0000 Subject: [PATCH 17/35] Initializacia --- cv1/program.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/cv1/program.c b/cv1/program.c index ce7f787..24bb1b2 100644 --- a/cv1/program.c +++ b/cv1/program.c @@ -64,11 +64,14 @@ int main() { int count = 0; printf("Zadaj hladanu surovinu:\n"); - fgets(search, LINESIZE, stdin); + if (!fgets(search, LINESIZE, stdin)) { + printf("Error reading input.\n"); + return 1; + } search[strcspn(search, "\n")] = '\0'; printf("Zadaj jedalny listok:\n"); - while (read_pizza(&menu[count])) { + while (count < MENU_SIZE && read_pizza(&menu[count])) { count++; } From 839307eea030aa0a1a33e261d9410b29fe09eff9 Mon Sep 17 00:00:00 2001 From: Kozar Date: Wed, 2 Oct 2024 18:22:55 +0000 Subject: [PATCH 18/35] Initializacia --- cv1/program.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cv1/program.c b/cv1/program.c index 24bb1b2..808cbc2 100644 --- a/cv1/program.c +++ b/cv1/program.c @@ -76,7 +76,7 @@ int main() { } int found_count = 0; - for (int i = 0; i < count; i++) { + for (int i = 0; i < 3; i++) { if (contains_normalized(menu[i].name, search)) { printf("%s\n%.2f\n", menu[i].name, menu[i].price); found_count++; From df6e7666dc2d8e5a8c80fedf87b268a5f0d299a3 Mon Sep 17 00:00:00 2001 From: Kozar Date: Wed, 2 Oct 2024 18:26:34 +0000 Subject: [PATCH 19/35] Initializacia --- cv1/program.c | 124 +++++++++++++++++++++----------------------------- 1 file changed, 52 insertions(+), 72 deletions(-) diff --git a/cv1/program.c b/cv1/program.c index 808cbc2..c6af434 100644 --- a/cv1/program.c +++ b/cv1/program.c @@ -1,89 +1,69 @@ #include -#include -#include #include +#include -#define LINESIZE 100 -#define MENU_SIZE 100 +#define MAX_INPUT_LENGTH 100 -struct pizza { - char name[LINESIZE]; - float price; +// Define the "Hack3r scr1pt" mapping +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'} }; -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() { - struct pizza menu[MENU_SIZE]; - char search[LINESIZE]; + char ingredient[MAX_INPUT_LENGTH]; + char menu_item[MAX_INPUT_LENGTH]; int count = 0; - printf("Zadaj hladanu surovinu:\n"); - if (!fgets(search, LINESIZE, stdin)) { - printf("Error reading input.\n"); - return 1; - } - search[strcspn(search, "\n")] = '\0'; + // Read the ingredient to be searched + printf("Zadaj hladanu surovinu: "); + fgets(ingredient, MAX_INPUT_LENGTH, stdin); + ingredient[strcspn(ingredient, "\n")] = 0; // Remove newline character - printf("Zadaj jedalny listok:\n"); - while (count < MENU_SIZE && read_pizza(&menu[count])) { - count++; - } + // Read the menu items + while (1) { + printf("Zadaj jedalny listok: "); + fgets(menu_item, MAX_INPUT_LENGTH, stdin); + if (feof(stdin) || strlen(menu_item) == 1) { + break; + } + menu_item[strcspn(menu_item, "\n")] = 0; // Remove newline character - int found_count = 0; - for (int i = 0; i < 3; i++) { - if (contains_normalized(menu[i].name, search)) { - printf("%s\n%.2f\n", menu[i].name, menu[i].price); - found_count++; + // Apply the "Hack3r scr1pt" mapping to the name + 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; + + // Search for the ingredient in the name + if (strstr(name_hack3r, ingredient) != NULL) { + printf("%s\n", menu_item); + count++; } } + // Display the count of successful searches printf("Nacitanych %d poloziek.\n", count); return 0; -} +} \ No newline at end of file From ec266a099f68d60430b569acfe88647eeff5f05c Mon Sep 17 00:00:00 2001 From: Kozar Date: Wed, 2 Oct 2024 18:31:47 +0000 Subject: [PATCH 20/35] Initializacia --- cv1/program.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cv1/program.c b/cv1/program.c index c6af434..380dfcc 100644 --- a/cv1/program.c +++ b/cv1/program.c @@ -28,9 +28,10 @@ int main() { fgets(ingredient, MAX_INPUT_LENGTH, stdin); ingredient[strcspn(ingredient, "\n")] = 0; // Remove newline character + printf("Zadaj jedalny listok:\n"); // Print the prompt + // Read the menu items while (1) { - printf("Zadaj jedalny listok: "); fgets(menu_item, MAX_INPUT_LENGTH, stdin); if (feof(stdin) || strlen(menu_item) == 1) { break; From e913bf53ed2d29a0e598171f54677f9c06b3e1bd Mon Sep 17 00:00:00 2001 From: Kozar Date: Wed, 2 Oct 2024 18:33:43 +0000 Subject: [PATCH 21/35] Initializacia --- cv1/program.c | 1 + 1 file changed, 1 insertion(+) diff --git a/cv1/program.c b/cv1/program.c index 380dfcc..f7fdf7b 100644 --- a/cv1/program.c +++ b/cv1/program.c @@ -32,6 +32,7 @@ int main() { // Read the menu items while (1) { + printf("Zadaj jedalny listok: "); // Print the prompt for each menu item fgets(menu_item, MAX_INPUT_LENGTH, stdin); if (feof(stdin) || strlen(menu_item) == 1) { break; From 828b632b59f7e6123a6a6598a689bd5f45fefbf5 Mon Sep 17 00:00:00 2001 From: Kozar Date: Wed, 2 Oct 2024 18:36:14 +0000 Subject: [PATCH 22/35] Initializacia --- cv1/program.c | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/cv1/program.c b/cv1/program.c index f7fdf7b..0e8d7d5 100644 --- a/cv1/program.c +++ b/cv1/program.c @@ -4,7 +4,6 @@ #define MAX_INPUT_LENGTH 100 -// Define the "Hack3r scr1pt" mapping char hack3r_script_mapping[][2] = { {'0', 'o'}, {'1', 'i'}, @@ -23,23 +22,19 @@ int main() { char menu_item[MAX_INPUT_LENGTH]; int count = 0; - // Read the ingredient to be searched printf("Zadaj hladanu surovinu: "); fgets(ingredient, MAX_INPUT_LENGTH, stdin); - ingredient[strcspn(ingredient, "\n")] = 0; // Remove newline character + ingredient[strcspn(ingredient, "\n")] = 0; - printf("Zadaj jedalny listok:\n"); // Print the prompt + printf("Zadaj jedalny listok:\n"); - // Read the menu items while (1) { - printf("Zadaj jedalny listok: "); // Print the prompt for each menu item fgets(menu_item, MAX_INPUT_LENGTH, stdin); if (feof(stdin) || strlen(menu_item) == 1) { break; } - menu_item[strcspn(menu_item, "\n")] = 0; // Remove newline character + menu_item[strcspn(menu_item, "\n")] = 0; - // Apply the "Hack3r scr1pt" mapping to the name char name_hack3r[MAX_INPUT_LENGTH]; int j = 0; for (int i = 0; menu_item[i]; i++) { @@ -57,14 +52,12 @@ int main() { } name_hack3r[j] = 0; - // Search for the ingredient in the name if (strstr(name_hack3r, ingredient) != NULL) { printf("%s\n", menu_item); count++; } } - // Display the count of successful searches printf("Nacitanych %d poloziek.\n", count); return 0; From c2ccf2dfa6fee20ba2083b006ad2cc78d6bba029 Mon Sep 17 00:00:00 2001 From: Kozar Date: Wed, 2 Oct 2024 18:39:17 +0000 Subject: [PATCH 23/35] 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++; } } From d1379c84273996085cc14cd44ee4b527f30086de Mon Sep 17 00:00:00 2001 From: Kozar Date: Wed, 2 Oct 2024 18:42:16 +0000 Subject: [PATCH 24/35] Initializacia --- cv1/program.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/cv1/program.c b/cv1/program.c index 358af72..9a382f6 100644 --- a/cv1/program.c +++ b/cv1/program.c @@ -83,6 +83,10 @@ int main() { } } + if (found_count == 0) { + printf("\n"); + } + printf("Nacitanych %d poloziek.\n", count); return 0; From 882b546c876720fe353fb559ee4245512b6c460f Mon Sep 17 00:00:00 2001 From: Kozar Date: Wed, 2 Oct 2024 18:44:48 +0000 Subject: [PATCH 25/35] Initializacia --- cv1/program.c | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/cv1/program.c b/cv1/program.c index 9a382f6..bcd33f9 100644 --- a/cv1/program.c +++ b/cv1/program.c @@ -12,17 +12,18 @@ struct pizza { }; 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); + switch (l) { + case '0': return 'o'; + case '1': return 'i'; + case '2': return 'z'; + case '3': return 'e'; + case '4': return 'a'; + case '5': return 's'; + case '6': return 'b'; + case '7': return 't'; + case '8': return 'b'; + case '9': return 'q'; + default: return l; } } From d8ded9eed69d4d07d1feb6fc7443bd89d99b9327 Mon Sep 17 00:00:00 2001 From: Kozar Date: Wed, 2 Oct 2024 18:48:04 +0000 Subject: [PATCH 26/35] Initializacia --- cv1/program.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cv1/program.c b/cv1/program.c index bcd33f9..e54cd48 100644 --- a/cv1/program.c +++ b/cv1/program.c @@ -36,7 +36,7 @@ void transform_to_hacker_script(const char *src, char *dest) { int contains_normalized(const char *name, const char *search) { char transformed_name[LINESIZE], transformed_search[LINESIZE]; - +q transform_to_hacker_script(name, transformed_name); transform_to_hacker_script(search, transformed_search); From 8b64f9d216326a1c50d73b84429ecce1398573a7 Mon Sep 17 00:00:00 2001 From: Kozar Date: Wed, 2 Oct 2024 18:49:34 +0000 Subject: [PATCH 27/35] Initializacia --- cv1/program.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cv1/program.c b/cv1/program.c index e54cd48..bcd33f9 100644 --- a/cv1/program.c +++ b/cv1/program.c @@ -36,7 +36,7 @@ void transform_to_hacker_script(const char *src, char *dest) { int contains_normalized(const char *name, const char *search) { char transformed_name[LINESIZE], transformed_search[LINESIZE]; -q + transform_to_hacker_script(name, transformed_name); transform_to_hacker_script(search, transformed_search); From fdb278d71d3fe6e25eb315b3ba6648878a8ea7fb Mon Sep 17 00:00:00 2001 From: Kozar Date: Wed, 2 Oct 2024 18:56:18 +0000 Subject: [PATCH 28/35] Initializacia --- cv1/program.c | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/cv1/program.c b/cv1/program.c index bcd33f9..1ee33bb 100644 --- a/cv1/program.c +++ b/cv1/program.c @@ -36,25 +36,20 @@ void transform_to_hacker_script(const char *src, char *dest) { 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; } @@ -66,7 +61,6 @@ int main() { printf("Zadaj hladanu surovinu:\n"); if (!fgets(search, LINESIZE, stdin)) { - printf("Error reading input.\n"); return 1; } search[strcspn(search, "\n")] = '\0'; @@ -89,6 +83,5 @@ int main() { } printf("Nacitanych %d poloziek.\n", count); - return 0; -} \ No newline at end of file +} From a61de64ccd94f6fab8e437599d842568403b56b7 Mon Sep 17 00:00:00 2001 From: Kozar Date: Wed, 2 Oct 2024 18:58:16 +0000 Subject: [PATCH 29/35] Initializacia --- cv1/program.c | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/cv1/program.c b/cv1/program.c index 1ee33bb..bd2350f 100644 --- a/cv1/program.c +++ b/cv1/program.c @@ -13,23 +13,22 @@ struct pizza { char hacker_script(char l) { switch (l) { - case '0': return 'o'; - case '1': return 'i'; - case '2': return 'z'; - case '3': return 'e'; - case '4': return 'a'; - case '5': return 's'; - case '6': return 'b'; - case '7': return 't'; - case '8': return 'b'; - case '9': return 'q'; - default: return 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++ = hacker_script(tolower(*src++)); } *dest = '\0'; } From 79e0d5633eccd12e6fda10f71daab50c033b4375 Mon Sep 17 00:00:00 2001 From: Kozar Date: Wed, 2 Oct 2024 19:00:52 +0000 Subject: [PATCH 30/35] Initializacia --- cv1/program.c | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/cv1/program.c b/cv1/program.c index bd2350f..1ee33bb 100644 --- a/cv1/program.c +++ b/cv1/program.c @@ -13,22 +13,23 @@ struct pizza { char hacker_script(char l) { switch (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); + case '0': return 'o'; + case '1': return 'i'; + case '2': return 'z'; + case '3': return 'e'; + case '4': return 'a'; + case '5': return 's'; + case '6': return 'b'; + case '7': return 't'; + case '8': return 'b'; + case '9': return 'q'; + default: return l; } } void transform_to_hacker_script(const char *src, char *dest) { while (*src) { - *dest++ = hacker_script(tolower(*src++)); + *dest++ = hacker_script(*src++); } *dest = '\0'; } From efd3e34cb55a88033bcd519f283c58a0057c6f37 Mon Sep 17 00:00:00 2001 From: Kozar Date: Wed, 2 Oct 2024 19:04:28 +0000 Subject: [PATCH 31/35] Initializacia --- cv1/program.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/cv1/program.c b/cv1/program.c index 1ee33bb..b5ccdb8 100644 --- a/cv1/program.c +++ b/cv1/program.c @@ -38,6 +38,14 @@ 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); + + for (int i = 0; transformed_name[i]; i++) { + transformed_name[i] = tolower(transformed_name[i]); + } + for (int i = 0; transformed_search[i]; i++) { + transformed_search[i] = tolower(transformed_search[i]); + } + return strstr(transformed_name, transformed_search) != NULL; } From 1746f22b4e73c43b7f176d3459bb0fc2f0a614d5 Mon Sep 17 00:00:00 2001 From: Kozar Date: Wed, 2 Oct 2024 19:05:32 +0000 Subject: [PATCH 32/35] Initializacia --- cv1/program.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cv1/program.c b/cv1/program.c index b5ccdb8..dbd3329 100644 --- a/cv1/program.c +++ b/cv1/program.c @@ -73,7 +73,7 @@ int main() { } search[strcspn(search, "\n")] = '\0'; - printf("Zadaj jedalny listok:\n"); + printf("Zadaj jedalny listok:"); while (count < MENU_SIZE && read_pizza(&menu[count])) { count++; } @@ -81,7 +81,7 @@ int main() { 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); + printf("\n%s\n%.2f\n", menu[i].name, menu[i].price); found_count++; } } From 943f74a75ec92f044ad95236c8ea749021e0bdf5 Mon Sep 17 00:00:00 2001 From: Kozar Date: Wed, 2 Oct 2024 19:06:27 +0000 Subject: [PATCH 33/35] Initializacia --- cv1/program.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/cv1/program.c b/cv1/program.c index dbd3329..b03c006 100644 --- a/cv1/program.c +++ b/cv1/program.c @@ -81,14 +81,11 @@ int main() { int found_count = 0; for (int i = 0; i < count; i++) { if (contains_normalized(menu[i].name, search)) { - printf("\n%s\n%.2f\n", menu[i].name, menu[i].price); + printf("%s\n%.2f\n", menu[i].name, menu[i].price); found_count++; } } - if (found_count == 0) { - printf("\n"); - } printf("Nacitanych %d poloziek.\n", count); return 0; From 75c55a1bb92b48da251b13e752cb2191e4ce5f84 Mon Sep 17 00:00:00 2001 From: Kozar Date: Wed, 2 Oct 2024 19:07:44 +0000 Subject: [PATCH 34/35] Initializacia --- cv1/program.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cv1/program.c b/cv1/program.c index b03c006..c4d322b 100644 --- a/cv1/program.c +++ b/cv1/program.c @@ -73,7 +73,7 @@ int main() { } search[strcspn(search, "\n")] = '\0'; - printf("Zadaj jedalny listok:"); + printf("Zadaj jedalny listok:\n"); while (count < MENU_SIZE && read_pizza(&menu[count])) { count++; } From e09f5c99e3015efbf8fe09d6ee464b03f7640013 Mon Sep 17 00:00:00 2001 From: Kozar Date: Mon, 7 Oct 2024 16:55:46 +0000 Subject: [PATCH 35/35] Initializacia --- cv2/program.c | 84 +++++++++++++++++++++++++++------------------------ 1 file changed, 45 insertions(+), 39 deletions(-) diff --git a/cv2/program.c b/cv2/program.c index 2e9c03d..b3f3f81 100644 --- a/cv2/program.c +++ b/cv2/program.c @@ -1,55 +1,61 @@ #include #include #include -#include -#define LINE_SIZE 100 -#define LIST_SIZE 100 +#define MAX_NAME_LENGTH 100 +#define MAX_ITEMS 100 -struct pizza{ - char name[LINE_SIZE]; +typedef struct { + char name[MAX_NAME_LENGTH]; float price; -} +} Pizza; -int read_pizza_list(struct pizza* list){ - char buffer[LINE_SIZE] - while(fgets(buffer, LINE_SIZT, stdin) == 0){ - +int compare(const void *a, const void *b) { + Pizza *pizzaA = (Pizza *)a; + Pizza *pizzaB = (Pizza *)b; + + if (pizzaA->price != pizzaB->price) { + return (pizzaA->price > pizzaB->price) - (pizzaA->price < pizzaB->price); } + return strcasecmp(pizzaA->name, pizzaB->name); } -int read_item(struct pizza* list) { - -} - - -int main(){ - struct pizza vacerajsia = {.name = "Vajcova pizza", .price = 0.2}; - printf("Pizza: %s, cena: %f/n", vacerajsia.name, vacerajsia.price -0.1); +int read_pizza_list(Pizza *menu) { + int count = 0; + char input[MAX_NAME_LENGTH]; - // Prechadzajte vsetky miesta jedalneho listka - int counter = 0; - for (int i=0; i< LIST_SIZE; i++){ - struct pizza item; - memset(&item,0,sizeof(struct pizza)); - // Nacitajte polozku do pomocnej premennej - // Na nacitanie pouzite vlastny kod - int r = read_item(&item); - if (r){ - // Ak sa nacitanie podarilo, skopirujte polozku do pola - memcpy(&listok[i],&item,sizeof(struct pizza)); - // Spocitame uspesne nacitane polozky - counter += 1; - } - else{ - // Ak sa nacitanie nepodarilo, nasli sme poslednu polozku - // Prerusim nacitanie + // Nacitame pizza + while (count < MAX_ITEMS && fgets(input, sizeof(input), stdin)) { + input[strcspn(input, "\n")] = 0; // Odstran znak noveho riadku + + // Nacitame cenu pizze + char price_str[10]; + if (fgets(price_str, sizeof(price_str), stdin) == NULL) { break; } + + float price = atof(price_str); + if (price <= 0) { + break; + } + + strncpy(menu[count].name, input, MAX_NAME_LENGTH - 1); + menu[count].price = price; + count++; } - // Na konci budemem mat nacitany jedalny listok - // V premennej counter je pocet uspesne nacitanych poloziek - return 0; + return count; } - +int main() { + Pizza menu[MAX_ITEMS]; + + int item_count = read_pizza_list(menu); + + qsort(menu, item_count, sizeof(Pizza), compare); + + for (int i = 0; i < item_count; i++) { + printf("%s\n%.6f\n", menu[i].name, menu[i].price); + } + + return 0; +}