From fe2d410fa02b7cff0838cb97b372e5c4aa8a8eae Mon Sep 17 00:00:00 2001 From: Denis Landa Date: Fri, 3 Oct 2025 07:55:48 +0000 Subject: [PATCH] Add du1/program.c --- du1/program.c | 116 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 du1/program.c diff --git a/du1/program.c b/du1/program.c new file mode 100644 index 0000000..6082c5e --- /dev/null +++ b/du1/program.c @@ -0,0 +1,116 @@ +#include +#include +#include +#include + +#define LINESIZE 100 +#define MAX_ITEMS 100 + +struct pizza { + float prize; + char name[LINESIZE]; +}; + +char hacker_script(char c) { + if (isupper(c)) { + return tolower(c); + } + + char numbers[] = "0123456789"; + char letters[] = "oizeasbtbq"; + + for (int i = 0; i < 10; i++) { + if (c == numbers[i]) { + return letters[i]; + } + } + + return c; +} + +int search_string(const char* heap, const char* needle) { + int heap_len = strlen(heap); + int needle_len = strlen(needle); + + if (needle_len > heap_len) { + return -1; + } + + for (int i = 0; i <= heap_len - needle_len; i++) { + int found = 1; + + for (int j = 0; j < needle_len; j++) { + char h_char = hacker_script(heap[i + j]); + char n_char = hacker_script(needle[j]); + + if (h_char != n_char) { + found = 0; + break; + } + } + + if (found) { + return i; + } + } + + return -1; +} + +int read_pizza(struct pizza* item, FILE* stream) { + char line[LINESIZE]; + char price_line[LINESIZE]; + + if (fgets(line, LINESIZE, stream) == NULL) { + return 0; + } + + line[strcspn(line, "\n")] = 0; + + if (fgets(price_line, LINESIZE, stream) == NULL) { + return 0; + } + + char* endptr; + float value = strtof(price_line, &endptr); + + if (endptr == price_line || value == 0.0f) { + return 0; + } + + strcpy(item->name, line); + item->prize = value; + + return 1; +} + +int main() { + char search_term[LINESIZE]; + struct pizza items[MAX_ITEMS]; + int item_count = 0; + + printf("Zadaj hladanu surovinu:\n"); + if (fgets(search_term, LINESIZE, stdin) == NULL) { + printf("Chyba pri čítaní hľadaného reťazca\n"); + return 1; + } + + search_term[strcspn(search_term, "\n")] = 0; + + printf("Zadaj jedalny listok:\n"); + + while (item_count < MAX_ITEMS && read_pizza(&items[item_count], stdin)) { + item_count++; + } + + for (int i = 0; i < item_count; i++) { + if (search_string(items[i].name, search_term) != -1) { + printf("%s\n", items[i].name); + printf("%.2f\n", items[i].prize); + } + } + + printf("Nacitanych %d poloziek.\n", item_count); + + return 0; +} \ No newline at end of file