Initializacia

This commit is contained in:
Kozar 2024-10-02 18:26:34 +00:00
parent 839307eea0
commit df6e7666dc

View File

@ -1,89 +1,69 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h> #include <string.h>
#include <ctype.h>
#define LINESIZE 100 #define MAX_INPUT_LENGTH 100
#define MENU_SIZE 100
struct pizza { // Define the "Hack3r scr1pt" mapping
char name[LINESIZE]; char hack3r_script_mapping[][2] = {
float price; {'0', 'o'},
{'1', 'i'},
{'2', 'z'},
{'3', 'e'},
{'4', 'a'},
{'5', 's'},
{'6', 'b'},
{'7', 't'},
{'8', 'b'},
{'9', 'q'}
}; };
char hacker_script(char l) {
switch (tolower(l)) {
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 'q': return '9';
default: return tolower(l);
}
}
void transform_to_hacker_script(const char *src, char *dest) {
while (*src) {
*dest++ = hacker_script(*src++);
}
*dest = '\0';
}
int contains_normalized(const char *name, const char *search) {
char transformed_name[LINESIZE], transformed_search[LINESIZE];
transform_to_hacker_script(name, transformed_name);
transform_to_hacker_script(search, transformed_search);
return strstr(transformed_name, transformed_search) != NULL;
}
int read_pizza(struct pizza *item) {
char line[LINESIZE];
if (!fgets(item->name, LINESIZE, stdin)) {
return 0;
}
item->name[strcspn(item->name, "\n")] = '\0';
if (!fgets(line, LINESIZE, stdin)) {
return 0;
}
item->price = strtof(line, NULL);
return 1;
}
int main() { int main() {
struct pizza menu[MENU_SIZE]; char ingredient[MAX_INPUT_LENGTH];
char search[LINESIZE]; char menu_item[MAX_INPUT_LENGTH];
int count = 0; int count = 0;
printf("Zadaj hladanu surovinu:\n"); // Read the ingredient to be searched
if (!fgets(search, LINESIZE, stdin)) { printf("Zadaj hladanu surovinu: ");
printf("Error reading input.\n"); fgets(ingredient, MAX_INPUT_LENGTH, stdin);
return 1; ingredient[strcspn(ingredient, "\n")] = 0; // Remove newline character
}
search[strcspn(search, "\n")] = '\0';
printf("Zadaj jedalny listok:\n"); // Read the menu items
while (count < MENU_SIZE && read_pizza(&menu[count])) { while (1) {
count++; printf("Zadaj jedalny listok: ");
} fgets(menu_item, MAX_INPUT_LENGTH, stdin);
if (feof(stdin) || strlen(menu_item) == 1) {
break;
}
menu_item[strcspn(menu_item, "\n")] = 0; // Remove newline character
int found_count = 0; // Apply the "Hack3r scr1pt" mapping to the name
for (int i = 0; i < 3; i++) { char name_hack3r[MAX_INPUT_LENGTH];
if (contains_normalized(menu[i].name, search)) { int j = 0;
printf("%s\n%.2f\n", menu[i].name, menu[i].price); for (int i = 0; menu_item[i]; i++) {
found_count++; int found = 0;
for (int k = 0; k < 10; k++) {
if (menu_item[i] == hack3r_script_mapping[k][0]) {
name_hack3r[j++] = hack3r_script_mapping[k][1];
found = 1;
break;
}
}
if (!found) {
name_hack3r[j++] = tolower(menu_item[i]);
}
}
name_hack3r[j] = 0;
// Search for the ingredient in the name
if (strstr(name_hack3r, ingredient) != NULL) {
printf("%s\n", menu_item);
count++;
} }
} }
// Display the count of successful searches
printf("Nacitanych %d poloziek.\n", count); printf("Nacitanych %d poloziek.\n", count);
return 0; return 0;
} }