usaa24/cv1/program.c

110 lines
2.3 KiB
C
Raw Normal View History

2024-09-30 20:44:04 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
struct pizza
{
char* name;
float price;
};
char* convert(char* input)
{
int inplen = strlen(input);
char* res = (char*)malloc(inplen * sizeof(char*));
memset(res,0,inplen);
for (int i = 0; i < inplen; i++)
{
switch (res[i])
{
case '0': res[i] = 'o'; break;
case '1': res[i] = 'i'; break;
case '2': res[i] = 'z'; break;
case '3': res[i] = 'e'; break;
case '4': res[i] = 'a'; break;
case '5': res[i] = 's'; break;
case '6': res[i] = 'b'; break;
case '7': res[i] = 't'; break;
case '8': res[i] = 'b'; break;
case '9': res[i] = 'q'; break;
default: res[i] = tolower(res[i]);
}
}
return res;
}
int search(char* find, int itemsc, struct pizza* menu)
{
char* findtlw = convert(find);
for (int i = 0; i < itemsc; i++)
{
char* conved = convert(menu[i].name);
if(strstr(conved,findtlw) != NULL)
{
free(conved);
free(findtlw);
return i;
}
free(conved);
}
free(findtlw);
return -1;
}
int main()
{
int itemc = 0;
char find[20];
struct pizza menu[20];
char buf[50];
float price = 0;
memset(buf, 0, 50);
memset(find, 0, 20);
printf("Zadaj hladanu surovinu:\n");
fgets(find, sizeof(find), stdin);
printf("Zadaj jedalny listok:\n");
while (fgets(buf, 50, stdin) != NULL)
{
buf[strcspn(buf, "\n")] = '\0';
if (scanf("%f", &price) != 1)
{
break;
}
getchar();
struct pizza piteml =
{
.name = (char*)malloc(strlen(buf) * sizeof(char*)),
.price = price
};
strcpy(piteml.name,buf);
menu[itemc] = piteml;
itemc++;
if (itemc >= 20)
{
break;
}
}
int item = search(find,itemc,menu);
if(item == -1)
{
return 0;
}
printf("%s\n%.2f\n", menu[item].name, menu[item].price);
printf("Nacitanych %d poloziek.", itemc);
for (int i = 0; i < itemc; i++)
{
free(menu[i].name);
}
return 0;
}