usaa24/cv1/program.c

84 lines
2.0 KiB
C

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define LINESIZE 100
#define MENU_SIZE 100
struct pizza {
float prize;
char name[LINESIZE];
};
char hacker_script(char c) {
if (c >= 'A' && c <= 'Z') c += 32;
switch (c) {
case 'o': return '0';
case 'i': return '1';
case 'z': return '2';
case 'e': return '3';
case 'a': return '4';
case 's': return '5';
case 'b': return '6';
case 't': return '7';
case '8': return '8';
case 'q': return '9';
default: return c;
}
}
void normalize_string(const char* input, char* output) {
int i = 0;
while (input[i] != '\0' && input[i] != '\n') {
output[i] = hacker_script(input[i]);
i++;
}
output[i] = '\0';
}
int read_pizza(struct pizza* item) {
char line[LINESIZE];
if (fgets(line, sizeof(line), stdin) == NULL) return 0;
normalize_string(line, item->name);
if (fgets(line, sizeof(line), stdin) == NULL) return 0;
item->prize = strtof(line, NULL);
return (item->prize > 0) ? 1 : 0;
}
int find_string(const char* heap, const char* needle) {
char normalized_heap[LINESIZE];
char normalized_needle[LINESIZE];
normalize_string(heap, normalized_heap);
normalize_string(needle, normalized_needle);
return strstr(normalized_heap, normalized_needle) != NULL;
}
int main() {
struct pizza menu[MENU_SIZE];
int item_count = 0;
char search_term[LINESIZE];
printf("Zadaj hladanu surovinu:\n");
fgets(search_term, sizeof(search_term), stdin);
search_term[strcspn(search_term, "\n")] = '\0';
printf("Zadaj jedalny listok:\n");
while (item_count < MENU_SIZE && read_pizza(&menu[item_count])) {
item_count++;
}
for (int i = 0; i < item_count; i++) {
if (find_string(menu[i].name, search_term)) {
printf("%s\n%.2f\n", menu[i].name, menu[i].prize);
}
}
printf("Nacitanych %d poloziek.\n", item_count);
return 0;
}