usaa24/cv1/program.c
2024-09-26 23:27:49 +02:00

80 lines
1.9 KiB
C

#include <stdio.h>
#include <string.h>
#include <ctype.h>
char convert_to_normal(char ch) {
switch (ch) {
case '0': return 'o';
case '1': return 'i';
case '2': return 'z';
case '3': return 'e';
case '4': return 'a';
case '5': return 's';
case '6': return 'b';
case '7': return 't';
case '8': return 'b';
case '9': return 'q';
default: return tolower(ch);
}
}
int contains_substring(char *food_name, char *ingredient) {
int len_food = strlen(food_name);
int len_ing = strlen(ingredient);
int i, j;
for (i = 0; i <= len_food - len_ing; i++) {
int found = 1;
for (j = 0; j < len_ing; j++) {
if (convert_to_normal(food_name[i + j]) != convert_to_normal(ingredient[j])) {
found = 0;
break;
}
}
if (found) return 1;
}
return 0;
}
void format_price(char *formatted_price, const char *price) {
double value;
sscanf(price, "%lf", &value);
sprintf(formatted_price, "%.2f", value);
}
int main() {
char ingredient[100];
char food_name[200];
char price[20];
char formatted_price[20];
int count = 0;
printf("Zadaj hladanu surovinu:\n");
if (!fgets(ingredient, sizeof(ingredient), stdin)) {
return 1;
}
ingredient[strcspn(ingredient, "\n")] = '\0';
printf("Zadaj jedalny listok:\n");
while (fgets(food_name, sizeof(food_name), stdin)) {
food_name[strcspn(food_name, "\n")] = '\0';
if (!fgets(price, sizeof(price), stdin)) {
break;
}
price[strcspn(price, "\n")] = '\0';
format_price(formatted_price, price);
if (contains_substring(food_name, ingredient)) {
printf("%s\n%s\n", food_name, formatted_price);
}
count++;
}
printf("Nacitanych %d poloziek.\n", count);
return 0;
}