117 lines
		
	
	
		
			2.8 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			117 lines
		
	
	
		
			2.8 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
| #include <stdio.h>
 | |
| #include <string.h>
 | |
| #include <stdlib.h>
 | |
| #include <ctype.h>
 | |
| 
 | |
| #define LINESIZE 100
 | |
| #define MAX_PIZZAS 100
 | |
| 
 | |
| // Structure to store pizza menu item
 | |
| struct pizza {
 | |
|     float price;
 | |
|     char name[LINESIZE];
 | |
| };
 | |
| 
 | |
| // Function to convert a character to its Hacker Script form
 | |
| char hacker_script(char c) {
 | |
|     char numbers[] = "0123456789";
 | |
|     char letters[] = "oizeasbtbq";
 | |
|     
 | |
|     // Convert to lowercase first
 | |
|     c = tolower(c);
 | |
|     
 | |
|     // Check for special characters
 | |
|     for (int i = 0; i < 10; i++) {
 | |
|         if (c == numbers[i]) {
 | |
|             return letters[i];
 | |
|         }
 | |
|     }
 | |
|     return c;
 | |
| }
 | |
| 
 | |
| // Function to normalize a string by applying hacker script rules
 | |
| void normalize_string(char *str) {
 | |
|     for (int i = 0; str[i]; i++) {
 | |
|         str[i] = hacker_script(str[i]);
 | |
|     }
 | |
| }
 | |
| 
 | |
| // Function to read one pizza item from input
 | |
| int read_pizza(struct pizza *item) {
 | |
|     char line1[LINESIZE], line2[LINESIZE];
 | |
|     
 | |
|     // Read name
 | |
|     if (fgets(line1, LINESIZE, stdin) == NULL) {
 | |
|         return 0;  // Failed to read
 | |
|     }
 | |
|     
 | |
|     // Remove newline character
 | |
|     line1[strcspn(line1, "\n")] = '\0';
 | |
| 
 | |
|     // Read price
 | |
|     if (fgets(line2, LINESIZE, stdin) == NULL) {
 | |
|         return 0;  // Failed to read
 | |
|     }
 | |
|     
 | |
|     float value = strtof(line2, NULL);
 | |
|     if (value == 0.0F) {
 | |
|         return 0;  // Invalid price
 | |
|     }
 | |
|     
 | |
|     // Copy the read values
 | |
|     strcpy(item->name, line1);
 | |
|     item->price = value;
 | |
|     return 1;
 | |
| }
 | |
| 
 | |
| // Function to search if a normalized needle exists in a normalized heap
 | |
| int search_string(const char *heap, const char *needle) {
 | |
|     char norm_heap[LINESIZE], norm_needle[LINESIZE];
 | |
|     
 | |
|     // Make copies to normalize
 | |
|     strcpy(norm_heap, heap);
 | |
|     strcpy(norm_needle, needle);
 | |
|     
 | |
|     // Normalize both strings
 | |
|     normalize_string(norm_heap);
 | |
|     normalize_string(norm_needle);
 | |
|     
 | |
|     // Use strstr to find the normalized needle in the heap
 | |
|     return strstr(norm_heap, norm_needle) != NULL;
 | |
| }
 | |
| 
 | |
| int main() {
 | |
|     struct pizza menu[MAX_PIZZAS];
 | |
|     char search[LINESIZE];
 | |
|     int count = 0;
 | |
| 
 | |
|     // Read search string
 | |
|     printf("Enter the searched ingredient:\n");
 | |
|     fgets(search, LINESIZE, stdin);
 | |
|     search[strcspn(search, "\n")] = '\0';  // Remove newline character
 | |
| 
 | |
|     // Read menu
 | |
|     printf("Enter the menu:\n");
 | |
|     while (read_pizza(&menu[count])) {
 | |
|         count++;
 | |
|         if (count >= MAX_PIZZAS) {
 | |
|             break;  // Limit reached
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     // Display valid dishes containing the searched ingredient
 | |
|     int found = 0;
 | |
|     for (int i = 0; i < count; i++) {
 | |
|         if (search_string(menu[i].name, search)) {
 | |
|             printf("%s\n", menu[i].name);
 | |
|             printf("%.2f\n", menu[i].price);
 | |
|             found++;
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     // Display total items read
 | |
|     printf("A total of %d items were loaded.\n", count);
 | |
| 
 | |
|     return 0;
 | |
| }
 |