usaa20/cv1/program.c
2020-10-05 09:11:50 +00:00

99 lines
2.1 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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;
}