usaa24/cv1/program.c

101 lines
2.8 KiB
C

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define LINESIZE 100
struct MenuItem {
char dish[LINESIZE];
float price;
};
// Function to normalize a string according to "Hacker Script" rules
void normalize(char* str) {
for (int i = 0; str[i]; i++) {
str[i] = tolower(str[i]);
switch (str[i]) {
case 'o': str[i] = '0'; break;
case 'i': str[i] = '1'; break;
case 'z': str[i] = '2'; break;
case 'e': str[i] = '3'; break;
case 'a': str[i] = '4'; break;
case 's': str[i] = '5'; break;
case 'b': str[i] = '6'; break; // 'b' mapped to '6'
case 't': str[i] = '7'; break;
case 'g': str[i] = '9'; break;
case 'q': str[i] = '9'; break;
// We keep 'r', 'y', 'n', and 'd' unchanged to ensure proper matching
default: break;
}
}
}
// Function to read a menu item
int read_menu_item(struct MenuItem* item) {
if (fgets(item->dish, LINESIZE, stdin) == NULL) {
return 0; // If reading failed
}
if (strcmp(item->dish, "end\n") == 0) {
return 0; // Exit command "end"
}
item->dish[strcspn(item->dish, "\n")] = 0; // Remove newline character
if (scanf("%f", &item->price) != 1) {
getchar(); // Clear input buffer
return 0; // If reading failed
}
getchar(); // Clear input buffer
return 1; // Successful reading
}
int main(void) {
struct MenuItem menu[LINESIZE];
int item_count = 0;
char search_string[LINESIZE];
// Prompt for the ingredient to search
printf("Zadaj hladanu surovinu: ");
fgets(search_string, LINESIZE, stdin);
search_string[strcspn(search_string, "\n")] = 0; // Remove newline character
// Normalize the search string
normalize(search_string); // Normalize the search string
printf("Zadaj jedalny listok:\n");
// Loop to read menu items
while (item_count < LINESIZE) {
struct MenuItem item;
if (!read_menu_item(&item)) {
break; // Exit loop on read error or "end"
}
// Normalize the dish name
normalize(item.dish);
menu[item_count++] = item; // Add item to menu
}
// Search for and print found dishes
int found = 0;
for (int i = 0; i < item_count; i++) {
// Perform a direct search for the normalized dish against the normalized search string
if (strstr(menu[i].dish, search_string) != NULL) { // Check if search_string is in dish
printf("%s\n%.2f\n", menu[i].dish, menu[i].price);
found = 1; // At least one dish found
}
}
// Print message if search was unsuccessful
if (!found) {
printf("Zadana surovina nebola najdena.\n");
}
printf("Nacitanych %d poloziek.\n", item_count);
return 0;
}