From c9050ce5f47c95b61339c58795ef224ed74f2552 Mon Sep 17 00:00:00 2001 From: Daniel Ryzhuk Date: Mon, 5 Oct 2020 09:11:50 +0000 Subject: [PATCH] Upload files to 'cv1' file commit --- cv1/program.c | 98 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 cv1/program.c diff --git a/cv1/program.c b/cv1/program.c new file mode 100644 index 0000000..81325b9 --- /dev/null +++ b/cv1/program.c @@ -0,0 +1,98 @@ +#include +#include +#include +#define LINE_LEN 100 + +typedef struct { + char name[LINE_LEN]; + float price; +} pizza; + + +int get_line(char *name); +int get_pizza(pizza *p_item); +char decode(char t); +int contain_substr(const char* menu_name, const char* target_name); + + + +int main() { + printf("Zadaj hladanu surovinu:\n"); + char target_name[LINE_LEN]; + if (!get_line(target_name)) { + exit(EXIT_FAILURE); + } + + printf("Zadaj jedalny listok:\n"); + pizza items[LINE_LEN]; + int count = 0; + while(get_pizza(items + count)) { + // printf("%s\n", items[count].name); + count++; + } + + for(int i = 0; i < count; i++) { + if (contain_substr(items[i].name, target_name)) { + printf("%s\n", items[i].name); + printf("%.2f\n", items[i].price); + } + } + printf("Nacitanych %d poloziek.\n", count); + return 0; +} + +int get_line(char *name) +{ + memset(name, 0, LINE_LEN); + char* r = fgets(name, LINE_LEN, stdin); + if (r == NULL || name[1] == 0) { + return 0; + } + int n = strlen(name); + if (name[n - 1] == '\n') + name[n - 1] = 0; + return 1; +} + +int get_pizza(pizza *p_item) +{ + int res = get_line(p_item->name); + char price_string[LINE_LEN]; + res += get_line(price_string); + if (res != 2) return 0; + sscanf(price_string, "%f", &(p_item->price)); + return 1; +} + +char decode(char t) +{ + if(t >= 'A' && t <= 'Z') { + return t - 'A' + 'a'; + } + char numbers[] = "0123456789"; + char letters[] = "oizeasbtbq"; + for (int i = 0; i < 10; i++){ + if (t == numbers[i]){ + return letters[i]; + } + } + return t; +} + +int contain_substr(const char* menu_name, const char* target_name) +{ + int len_str = strlen(menu_name); + int len_sub = strlen(target_name); + for(int i = 0; i <= len_str - len_sub; i++) { + int flag = 1; + for(int j = 0; flag == 1 && j < len_sub; j++) { + if (decode(menu_name[i + j]) != decode(target_name[j])) { + flag = 0; + } + } + if (flag) { + return 1; + } + } + return 0; +}