usaa24/cv1/program.c

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