Update cv1/program.c
This commit is contained in:
		
							parent
							
								
									f5d8c7855b
								
							
						
					
					
						commit
						b47fc10664
					
				
							
								
								
									
										144
									
								
								cv1/program.c
									
									
									
									
									
								
							
							
						
						
									
										144
									
								
								cv1/program.c
									
									
									
									
									
								
							| @ -1,96 +1,116 @@ | ||||
| #include <stdio.h> | ||||
| #include <string.h> | ||||
| #include <stdlib.h> | ||||
| #include <ctype.h> | ||||
| 
 | ||||
| #define LINESIZE 100 | ||||
| #define MENU_SIZE 100 | ||||
| #define MAX_PIZZAS 100 | ||||
| 
 | ||||
| // Structure to store pizza menu item
 | ||||
| struct pizza { | ||||
|     float prize; | ||||
|     float price; | ||||
|     char name[LINESIZE]; | ||||
| }; | ||||
| 
 | ||||
| // Function to convert a character to its Hacker Script form
 | ||||
| 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; | ||||
|     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]); | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| void normalize_string(const char* input, char* output) { | ||||
|     int i = 0; | ||||
|     while (input[i] != '\0' && input[i] != '\n') {  // Убираем '\n'
 | ||||
|         output[i] = hacker_script(input[i]); | ||||
|         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
 | ||||
|     } | ||||
|     output[i] = '\0';  // Завершаем строку
 | ||||
|      | ||||
|     // 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; | ||||
| } | ||||
| 
 | ||||
| int read_pizza(struct pizza* item) { | ||||
|     char line[LINESIZE]; | ||||
|     if (fgets(line, sizeof(line), stdin) == NULL) return 0; | ||||
| // 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]; | ||||
|      | ||||
|     normalize_string(line, item->name); // Преобразуем название
 | ||||
|     // Make copies to normalize
 | ||||
|     strcpy(norm_heap, heap); | ||||
|     strcpy(norm_needle, needle); | ||||
|      | ||||
|     if (fgets(line, sizeof(line), stdin) == NULL) return 0; | ||||
|     item->prize = strtof(line, NULL); // Преобразуем цену
 | ||||
|     // Normalize both strings
 | ||||
|     normalize_string(norm_heap); | ||||
|     normalize_string(norm_needle); | ||||
|      | ||||
|     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; | ||||
|     // Use strstr to find the normalized needle in the heap
 | ||||
|     return strstr(norm_heap, norm_needle) != NULL; | ||||
| } | ||||
| 
 | ||||
| int main() { | ||||
|     struct pizza menu[MENU_SIZE]; | ||||
|     int item_count = 0; | ||||
|     struct pizza menu[MAX_PIZZAS]; | ||||
|     char search[LINESIZE]; | ||||
|     int count = 0; | ||||
| 
 | ||||
|     char search_term[LINESIZE]; | ||||
|     printf("Zadaj hladanu surovinu:\n"); // Первое сообщение
 | ||||
|     fgets(search_term, sizeof(search_term), stdin); | ||||
|     // Read search string
 | ||||
|     printf("Enter the searched ingredient:\n"); | ||||
|     fgets(search, LINESIZE, stdin); | ||||
|     search[strcspn(search, "\n")] = '\0';  // Remove newline character
 | ||||
| 
 | ||||
|     // Удаляем '\n' из поискового запроса
 | ||||
|     search_term[strcspn(search_term, "\n")] = '\0';  | ||||
| 
 | ||||
|     printf("Zadaj jedalny listok:\n"); // Второе сообщение
 | ||||
|     while (item_count < MENU_SIZE && read_pizza(&menu[item_count])) { | ||||
|         item_count++; | ||||
|     // 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 < item_count; i++) { | ||||
|         if (find_string(menu[i].name, search_term)) { | ||||
|             printf("%s\n%.2f\n", menu[i].name, menu[i].prize); | ||||
|             found = 1; // Найдено совпадение
 | ||||
|     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++; | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     // Если совпадений нет, ничего не выводится, иначе выводим количество
 | ||||
|     if (found) { | ||||
|         printf("Nacitanych %d poloziek.\n", item_count); | ||||
|     } else { | ||||
|         // Выводим только количество, если совпадений нет
 | ||||
|         printf("Nacitanych %d poloziek.\n", item_count); | ||||
|     } | ||||
|     // Display total items read
 | ||||
|     printf("A total of %d items were loaded.\n", count); | ||||
| 
 | ||||
|     return 0; | ||||
| } | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user