From b47fc106643db76ffe7c073742827aabac5a9c14 Mon Sep 17 00:00:00 2001 From: Marat Izmailov Date: Wed, 2 Oct 2024 14:24:27 +0000 Subject: [PATCH] Update cv1/program.c --- cv1/program.c | 154 ++++++++++++++++++++++++++++---------------------- 1 file changed, 87 insertions(+), 67 deletions(-) diff --git a/cv1/program.c b/cv1/program.c index 83707eb..5fe2617 100644 --- a/cv1/program.c +++ b/cv1/program.c @@ -1,96 +1,116 @@ #include #include #include +#include #define LINESIZE 100 -#define MENU_SIZE 100 +#define MAX_PIZZAS 100 +// Structure to store pizza menu item struct pizza { - float prize; + float price; char name[LINESIZE]; }; +// Function to convert a character to its Hacker Script form char hacker_script(char c) { - if (c >= 'A' && c <= 'Z') c += 32; // Преобразование к нижнему регистру - switch (c) { - 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 '8': return '8'; - case 'q': return '9'; - default: return c; + char numbers[] = "0123456789"; + char letters[] = "oizeasbtbq"; + + // Convert to lowercase first + c = tolower(c); + + // Check for special characters + for (int i = 0; i < 10; i++) { + if (c == numbers[i]) { + return letters[i]; + } + } + return c; +} + +// Function to normalize a string by applying hacker script rules +void normalize_string(char *str) { + for (int i = 0; str[i]; i++) { + str[i] = hacker_script(str[i]); } } -void normalize_string(const char* input, char* output) { - int i = 0; - while (input[i] != '\0' && input[i] != '\n') { // Убираем '\n' - output[i] = hacker_script(input[i]); - i++; +// Function to read one pizza item from input +int read_pizza(struct pizza *item) { + char line1[LINESIZE], line2[LINESIZE]; + + // Read name + if (fgets(line1, LINESIZE, stdin) == NULL) { + return 0; // Failed to read } - output[i] = '\0'; // Завершаем строку + + // Remove newline character + line1[strcspn(line1, "\n")] = '\0'; + + // Read price + if (fgets(line2, LINESIZE, stdin) == NULL) { + return 0; // Failed to read + } + + float value = strtof(line2, NULL); + if (value == 0.0F) { + return 0; // Invalid price + } + + // Copy the read values + strcpy(item->name, line1); + item->price = value; + return 1; } -int read_pizza(struct pizza* item) { - char line[LINESIZE]; - if (fgets(line, sizeof(line), stdin) == NULL) return 0; - - normalize_string(line, item->name); // Преобразуем название - - if (fgets(line, sizeof(line), stdin) == NULL) return 0; - item->prize = strtof(line, NULL); // Преобразуем цену - - return (item->prize > 0) ? 1 : 0; // Успех или ошибка -} - -int find_string(const char* heap, const char* needle) { - char normalized_heap[LINESIZE]; - char normalized_needle[LINESIZE]; - normalize_string(heap, normalized_heap); - normalize_string(needle, normalized_needle); - - return strstr(normalized_heap, normalized_needle) != NULL; +// Function to search if a normalized needle exists in a normalized heap +int search_string(const char *heap, const char *needle) { + char norm_heap[LINESIZE], norm_needle[LINESIZE]; + + // Make copies to normalize + strcpy(norm_heap, heap); + strcpy(norm_needle, needle); + + // Normalize both strings + normalize_string(norm_heap); + normalize_string(norm_needle); + + // Use strstr to find the normalized needle in the heap + return strstr(norm_heap, norm_needle) != NULL; } int main() { - struct pizza menu[MENU_SIZE]; - int item_count = 0; + struct pizza menu[MAX_PIZZAS]; + char search[LINESIZE]; + int count = 0; - char search_term[LINESIZE]; - printf("Zadaj hladanu surovinu:\n"); // Первое сообщение - fgets(search_term, sizeof(search_term), stdin); - - // Удаляем '\n' из поискового запроса - search_term[strcspn(search_term, "\n")] = '\0'; + // Read search string + printf("Enter the searched ingredient:\n"); + fgets(search, LINESIZE, stdin); + search[strcspn(search, "\n")] = '\0'; // Remove newline character - printf("Zadaj jedalny listok:\n"); // Второе сообщение - while (item_count < MENU_SIZE && read_pizza(&menu[item_count])) { - item_count++; - } - - // Флаг для проверки, был ли найден хотя бы один элемент - int found = 0; - - // Печать подходящих элементов - for (int i = 0; i < item_count; i++) { - if (find_string(menu[i].name, search_term)) { - printf("%s\n%.2f\n", menu[i].name, menu[i].prize); - found = 1; // Найдено совпадение + // Read menu + printf("Enter the menu:\n"); + while (read_pizza(&menu[count])) { + count++; + if (count >= MAX_PIZZAS) { + break; // Limit reached } } - // Если совпадений нет, ничего не выводится, иначе выводим количество - if (found) { - printf("Nacitanych %d poloziek.\n", item_count); - } else { - // Выводим только количество, если совпадений нет - printf("Nacitanych %d poloziek.\n", item_count); + // Display valid dishes containing the searched ingredient + int found = 0; + for (int i = 0; i < count; i++) { + if (search_string(menu[i].name, search)) { + printf("%s\n", menu[i].name); + printf("%.2f\n", menu[i].price); + found++; + } } + // Display total items read + printf("A total of %d items were loaded.\n", count); + return 0; }