Add program.c

This commit is contained in:
Marat Izmailov 2024-10-02 13:49:18 +00:00
commit d951b51444

80
program.c Normal file
View File

@ -0,0 +1,80 @@
#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') return c + 32; // Convert uppercase to lowercase
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'; // Single mapping for 'b'
case 't': return '7';
case '8': return '8'; // Added mapping for '8'
case 'q': return '9';
default: return c; // Return unchanged for other characters
}
}
void normalize_string(const char* input, char* output) {
for (int i = 0; input[i] != '\0'; i++) {
output[i] = hacker_script(input[i]);
}
output[strlen(input)] = '\0'; // Null-terminate
}
int read_pizza(struct pizza* item) {
char line[LINESIZE];
if (fgets(line, sizeof(line), stdin) == NULL) return 0; // End of input
normalize_string(line, item->name); // Normalize the name
if (fgets(line, sizeof(line), stdin) == NULL) return 0; // Read price
item->prize = strtof(line, NULL); // Convert to float
return (item->prize > 0) ? 1 : 0; // Return success or failure
}
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]; // Renamed to avoid conflict
printf("Zadaj hladanu surovinu:\n");
fgets(search_term, sizeof(search_term), stdin);
printf("Zadaj jedalny listok:\n");
while (item_count < MENU_SIZE && read_pizza(&menu[item_count])) {
item_count++;
}
// Print matching items
for (int i = 0; i < item_count; i++) {
if (find_string(menu[i].name, search_term)) { // Use the new function name
printf("%s\n%.2f\n", menu[i].name, menu[i].prize);
}
}
printf("Nacitanych %d poloziek.\n", item_count);
return 0;
}