usaa21/cv1/program.c
2021-10-07 20:19:49 +02:00

121 lines
3.3 KiB
C
Executable File

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct item {
char name[50];
float price;
};
struct backup_item {
char name[50];
float price;
};
#define NUMBER_OF_MEALS 20
int main(){
char sign[] = {'o','i','z','e','a','s','b','t','b','q'};
char secret[]={'0','1','2','3','4','5','6','7','8','9'};
char input_search[50];
//initialize input_search
struct item menu[NUMBER_OF_MEALS];
struct backup_item backup_menu[NUMBER_OF_MEALS];
//initialize menu
//for(int i = 0; i < NUMBER_OF_MEALS; i++){
memset(menu, 0,sizeof(struct item)*NUMBER_OF_MEALS);
memset(backup_menu, 0,sizeof(struct item)*NUMBER_OF_MEALS);
//printf("%s\n",menu->name);
//menu[i] = {'c','c'};
//}
printf("Zadaj hladanu surovinu:\n");
fgets (input_search, 20, stdin);
printf("Zadaj jedalny listok:\n");
char temp[10];
//initialize temp
int meal_counter = 0;
//load menu
char* ptr = NULL;
while(1){
ptr = fgets (menu[meal_counter].name, 20, stdin);
if(ptr == NULL){
break;
}
if(menu[meal_counter].name[0] == '\n'){
break;
}
fgets (temp, 20, stdin);
menu[meal_counter].price = strtof(temp, NULL);
meal_counter++;
}
//make copy of menu
for (int i = 0 ; i < meal_counter; i++){
strcpy(backup_menu[i].name, menu[i].name);
backup_menu[i].price = menu[i].price;
}
/*optimize backup_menu*/
//each item.name
for(int i = 0 ; i < meal_counter; i++){
//each item.name letter
for(int j = 0; j < strlen(backup_menu[i].name)-1; j++){
//each letter in secret[]
for (int k = 0; k < 10; k++){
//compare secret letter with item.name letters
if(backup_menu[i].name[j] == secret[k]){
//swap letter in item.name with sign[] if match
backup_menu[i].name[j] = sign[k];
}
}
}
}
int input_search_size = strlen(input_search);
int match_counter = 1;
int new_start = 0;
//find match in menu
for(int i = 0; i < meal_counter; i++){
new_start = 0;
match_counter = 1;
//loop through each item name
for(int j = 0; j < strlen(backup_menu[i].name); j++){
//if some word starts with first letter of input
if(backup_menu[i].name[j] == input_search[0]){
new_start = j;
//loop through each letter in input_search
for(int k = 0; k < input_search_size; k++){
if(backup_menu[i].name[new_start] == input_search[k]){
match_counter++;
}
new_start++;
}
if(match_counter == input_search_size){
printf("%s%.2f\n",menu[i].name,menu[i].price);
break;
}
}
}
}
printf("Nacitanych %d poloziek.\n",meal_counter);
return 0;
}