usaa24/cv1/program.c
2024-10-01 12:22:47 +02:00

113 lines
2.4 KiB
C

#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*));
strcpy(res,input);
for (int i = 0; i < inplen; i++)
{
switch (input[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;
case '\n': res[i] = '\0'; break;
default: res[i] = tolower(res[i]); break;
}
}
res[inplen] = '\0';
return res;
}
int* search(char* find, int itemsc, struct pizza* menu, int* outC)
{
int* res = (int*)malloc(itemsc*sizeof(int*));
memset(res,0,itemsc);
char* findtlw = convert(find);
for (int i = 0; i < itemsc; i++)
{
char* conved = convert(menu[i].name);
if(strstr(conved,findtlw) != NULL)
{
res[*outC] = i;
(*outC)++;
}
free(conved);
}
free(findtlw);
return res;
}
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 sitemc = 0;
int* items = search(find,itemc,menu,&sitemc);
for (int i = 0; i < sitemc; i++)
{
printf("%s\n%.2f\n", menu[i].name, menu[i].price);
}
printf("Nacitanych %d poloziek.\n", itemc);
for (int i = 0; i < itemc; i++)
{
free(menu[i].name);
}
free(items);
return 0;
}