Initializacia

This commit is contained in:
Kozar 2024-10-02 12:28:51 +00:00
parent c2c30fbd59
commit 831b080f14

View File

@ -6,13 +6,11 @@
#define LINESIZE 100 #define LINESIZE 100
#define MENU_SiZE 100 #define MENU_SiZE 100
// Štruktúra pre položku jedálneho lístka
struct pizza{ struct pizza{
char name[LINESIZE]; char name[LINESIZE];
float price; float price;
} }
// Prepisovací algoritmus pre "Hack3r scr1pt"
char hacker_script(char l){ char hacker_script(char l){
switch(tolower(char l)){ switch(tolower(char l)){
case 'o': return '0'; case 'o': return '0';
@ -28,7 +26,6 @@ char hacker_script(char l){
} }
} }
// Function to transform a string into "Hack3r scr1pt"
void transform_to_hacker_script(const char *src, char *dest) { void transform_to_hacker_script(const char *src, char *dest) {
while (*src) { while (*src) {
*dest++ = hacker_script(*src++); *dest++ = hacker_script(*src++);
@ -36,38 +33,30 @@ void transform_to_hacker_script(const char *src, char *dest) {
*dest = '\0'; *dest = '\0';
} }
// Function to search for a normalized string in the name
int contains_normalized(const char *name, const char *search) { int contains_normalized(const char *name, const char *search) {
char transformed_name[LINESIZE], transformed_search[LINESIZE]; char transformed_name[LINESIZE], transformed_search[LINESIZE];
// Transform both strings
transform_to_hacker_script(name, transformed_name); transform_to_hacker_script(name, transformed_name);
transform_to_hacker_script(search, transformed_search); transform_to_hacker_script(search, transformed_search);
// Search for the string in the name
return strstr(transformed_name, transformed_search) != NULL; return strstr(transformed_name, transformed_search) != NULL;
} }
// Function to read a single menu item
int read_pizza(struct pizza *item) { int read_pizza(struct pizza *item) {
char line[LINESIZE]; char line[LINESIZE];
// Read the name
if (!fgets(item->name, LINESIZE, stdin)) { if (!fgets(item->name, LINESIZE, stdin)) {
return 0; // End of input return 0;
} }
// Remove newline character
item->name[strcspn(item->name, "\n")] = '\0'; item->name[strcspn(item->name, "\n")] = '\0';
// Read the price
if (!fgets(line, LINESIZE, stdin)) { if (!fgets(line, LINESIZE, stdin)) {
return 0; // Failed to read price return 0;
} }
// Convert string to float
item->price = strtof(line, NULL); item->price = strtof(line, NULL);
return 1; // Successfully read return 1;
} }
int main() { int main() {
@ -75,21 +64,18 @@ int main() {
char search[LINESIZE]; char search[LINESIZE];
int count = 0; int count = 0;
// Read the search string
printf("Zadaj hladanu surovinu:\n"); printf("Zadaj hladanu surovinu:\n");
fgets(search, LINESIZE, stdin); fgets(search, LINESIZE, stdin);
search[strcspn(search, "\n")] = '\0'; // Remove newline search[strcspn(search, "\n")] = '\0';
printf("Zadaj jedalny listok:\n"); printf("Zadaj jedalny listok:\n");
// Read the menu items
while (read_pizza(&menu[count])) { while (read_pizza(&menu[count])) {
count++; count++;
} }
printf("\nVysledky vyhladavania:\n"); printf("\nVysledky vyhladavania:\n");
// Search and print items that match the search string
int found_count = 0; int found_count = 0;
for (int i = 0; i < count; i++) { for (int i = 0; i < count; i++) {
if (contains_normalized(menu[i].name, search)) { if (contains_normalized(menu[i].name, search)) {
@ -97,8 +83,7 @@ int main() {
found_count++; found_count++;
} }
} }
// Print the total number of loaded items
printf("Nacitanych %d poloziek.\n", count);s printf("Nacitanych %d poloziek.\n", count);s
return 0; return 0;