usaa24/cv1/program.c

101 lines
2.8 KiB
C
Raw Normal View History

2024-09-30 11:43:46 +00:00
#include <stdio.h>
2024-10-02 16:24:50 +00:00
#include <string.h>
2024-10-02 17:38:55 +00:00
#include <ctype.h>
2024-09-30 16:41:25 +00:00
2024-10-02 16:42:43 +00:00
#define LINESIZE 100
2024-09-30 11:43:46 +00:00
2024-10-02 17:38:55 +00:00
struct MenuItem {
char dish[LINESIZE];
float price;
2024-09-30 11:43:46 +00:00
};
2024-10-02 18:26:22 +00:00
// Function to normalize a string according to "Hacker Script" rules
2024-10-02 17:38:55 +00:00
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;
2024-10-02 18:46:51 +00:00
case 'd': str[i] = '6'; break; // 'b' mapped to '6'
2024-10-02 17:38:55 +00:00
case 't': str[i] = '7'; break;
2024-10-02 18:46:51 +00:00
case 'b': str[i] = '8'; break;
2024-10-02 18:26:22 +00:00
case 'q': str[i] = '9'; break;
2024-10-02 18:46:51 +00:00
// We keep 'r', 'y', 'n', and 'd' unchanged to ensure proper matching
2024-10-02 18:26:22 +00:00
default: break;
2024-10-02 17:08:37 +00:00
}
2024-09-30 11:43:46 +00:00
}
}
2024-10-02 18:26:22 +00:00
// Function to read a menu item
2024-10-02 17:38:55 +00:00
int read_menu_item(struct MenuItem* item) {
if (fgets(item->dish, LINESIZE, stdin) == NULL) {
2024-10-02 18:26:22 +00:00
return 0; // If reading failed
2024-10-02 17:08:37 +00:00
}
2024-10-02 17:38:55 +00:00
if (strcmp(item->dish, "end\n") == 0) {
2024-10-02 18:26:22 +00:00
return 0; // Exit command "end"
2024-10-02 17:08:37 +00:00
}
2024-10-02 18:26:22 +00:00
item->dish[strcspn(item->dish, "\n")] = 0; // Remove newline character
2024-10-02 17:38:55 +00:00
if (scanf("%f", &item->price) != 1) {
2024-10-02 18:26:22 +00:00
getchar(); // Clear input buffer
return 0; // If reading failed
2024-10-02 17:08:37 +00:00
}
2024-10-02 18:26:22 +00:00
getchar(); // Clear input buffer
2024-10-02 17:08:37 +00:00
2024-10-02 18:26:22 +00:00
return 1; // Successful reading
2024-10-02 17:08:37 +00:00
}
2024-10-02 17:38:55 +00:00
int main(void) {
struct MenuItem menu[LINESIZE];
int item_count = 0;
2024-09-30 16:41:25 +00:00
2024-10-02 17:38:55 +00:00
char search_string[LINESIZE];
2024-10-02 16:24:50 +00:00
2024-10-02 18:26:22 +00:00
// Prompt for the ingredient to search
2024-10-02 17:38:55 +00:00
printf("Zadaj hladanu surovinu: ");
fgets(search_string, LINESIZE, stdin);
2024-10-02 18:26:22 +00:00
search_string[strcspn(search_string, "\n")] = 0; // Remove newline character
2024-10-02 16:24:50 +00:00
2024-10-02 18:26:22 +00:00
// Normalize the search string
normalize(search_string); // Normalize the search string
2024-10-02 16:24:50 +00:00
2024-10-02 17:38:55 +00:00
printf("Zadaj jedalny listok:\n");
2024-10-02 17:08:37 +00:00
2024-10-02 18:26:22 +00:00
// Loop to read menu items
while (item_count < LINESIZE) {
2024-10-02 17:38:55 +00:00
struct MenuItem item;
if (!read_menu_item(&item)) {
2024-10-02 18:26:22 +00:00
break; // Exit loop on read error or "end"
2024-10-02 17:38:55 +00:00
}
2024-10-02 18:26:22 +00:00
// Normalize the dish name
2024-10-02 18:46:51 +00:00
normalize(item.dish);
2024-10-02 18:26:22 +00:00
menu[item_count++] = item; // Add item to menu
2024-10-02 17:38:55 +00:00
}
2024-10-02 17:08:37 +00:00
2024-10-02 18:26:22 +00:00
// Search for and print found dishes
2024-10-02 17:38:55 +00:00
int found = 0;
for (int i = 0; i < item_count; i++) {
2024-10-02 18:46:51 +00:00
// Perform a direct search for the normalized dish against the normalized search string
2024-10-02 18:26:22 +00:00
if (strstr(menu[i].dish, search_string) != NULL) { // Check if search_string is in dish
2024-10-02 17:38:55 +00:00
printf("%s\n%.2f\n", menu[i].dish, menu[i].price);
2024-10-02 18:26:22 +00:00
found = 1; // At least one dish found
2024-10-02 16:42:43 +00:00
}
}
2024-10-02 16:24:50 +00:00
2024-10-02 18:26:22 +00:00
// Print message if search was unsuccessful
2024-10-02 17:38:55 +00:00
if (!found) {
printf("Zadana surovina nebola najdena.\n");
}
printf("Nacitanych %d poloziek.\n", item_count);
2024-10-02 16:24:50 +00:00
return 0;
2024-10-02 08:31:44 +00:00
}