Обновить cv1/program.c
This commit is contained in:
		
							parent
							
								
									72adb052bc
								
							
						
					
					
						commit
						b2d1c23953
					
				| @ -1,10 +1,8 @@ | |||||||
| #include <stdio.h> | #include <stdio.h> | ||||||
| #include <string.h> | #include <string.h> | ||||||
| #include <ctype.h> | #include <ctype.h> | ||||||
| 
 |  | ||||||
| #define MAX_DISHES 100 | #define MAX_DISHES 100 | ||||||
| #define MAX_NAME_LEN 100 | #define MAX_NAME_LEN 100 | ||||||
| 
 |  | ||||||
| void decodovane_slovo(char *text) { | void decodovane_slovo(char *text) { | ||||||
|     for (int i = 0; text[i]; i++) { |     for (int i = 0; text[i]; i++) { | ||||||
|         switch (text[i]) { |         switch (text[i]) { | ||||||
| @ -22,7 +20,6 @@ void decodovane_slovo(char *text) { | |||||||
|         } |         } | ||||||
|     } |     } | ||||||
| } | } | ||||||
| 
 |  | ||||||
| int main() { | int main() { | ||||||
|     char ingredient[MAX_NAME_LEN]; |     char ingredient[MAX_NAME_LEN]; | ||||||
|     char dishes[MAX_DISHES][2][MAX_NAME_LEN]; |     char dishes[MAX_DISHES][2][MAX_NAME_LEN]; | ||||||
| @ -30,55 +27,52 @@ int main() { | |||||||
| 
 | 
 | ||||||
|     printf("Zadaj hladanu surovinu: \n"); |     printf("Zadaj hladanu surovinu: \n"); | ||||||
|     if (fgets(ingredient, MAX_NAME_LEN, stdin) == NULL) { |     if (fgets(ingredient, MAX_NAME_LEN, stdin) == NULL) { | ||||||
|         return 0; // Обработка ошибки
 |         return 0; | ||||||
|     } |     } | ||||||
|     ingredient[strcspn(ingredient, "\n")] = '\0'; // Удаляем символ новой строки
 |     ingredient[strcspn(ingredient, "\n")] = '\0'; | ||||||
| 
 | 
 | ||||||
|     printf("Zadaj jedalny listok: \n"); |     printf("Zadaj jedalny listok: \n"); | ||||||
|     while (1) { |     while (1) { | ||||||
|         char name[MAX_NAME_LEN], price[MAX_NAME_LEN]; |         char name[MAX_NAME_LEN], price[MAX_NAME_LEN]; | ||||||
| 
 | 
 | ||||||
|         if (fgets(name, MAX_NAME_LEN, stdin) == NULL) { |         if (fgets(name, MAX_NAME_LEN, stdin) == NULL) { | ||||||
|             return 0; // Обработка ошибки
 |             return 0; | ||||||
|         } |         } | ||||||
|         name[strcspn(name, "\n")] = '\0'; // Удаляем символ новой строки
 |         name[strcspn(name, "\n")] = '\0'; | ||||||
| 
 | 
 | ||||||
|         if (strlen(name) == 0) { |         if (strlen(name) == 0) { | ||||||
|             break; |             break; | ||||||
|         } |         } | ||||||
| 
 |  | ||||||
|         if (fgets(price, MAX_NAME_LEN, stdin) == NULL) { |         if (fgets(price, MAX_NAME_LEN, stdin) == NULL) { | ||||||
|             return 0; // Обработка ошибки
 |             return 0; | ||||||
|         } |         } | ||||||
|         price[strcspn(price, "\n")] = '\0'; // Удаляем символ новой строки
 |         price[strcspn(price, "\n")] = '\0'; | ||||||
| 
 | 
 | ||||||
|         // Использование strncpy для безопасности
 |  | ||||||
|         strncpy(dishes[count][0], name, MAX_NAME_LEN - 1); |         strncpy(dishes[count][0], name, MAX_NAME_LEN - 1); | ||||||
|         dishes[count][0][MAX_NAME_LEN - 1] = '\0'; // Обеспечение завершенности строки
 |         dishes[count][0][MAX_NAME_LEN - 1] = '\0'; | ||||||
| 
 | 
 | ||||||
|         strncpy(dishes[count][1], price, MAX_NAME_LEN - 1); |         strncpy(dishes[count][1], price, MAX_NAME_LEN - 1); | ||||||
|         dishes[count][1][MAX_NAME_LEN - 1] = '\0'; // Обеспечение завершенности строки
 |         dishes[count][1][MAX_NAME_LEN - 1] = '\0'; | ||||||
| 
 | 
 | ||||||
|         count++; |         count++; | ||||||
|     } |     } | ||||||
|  |     decodovane_slovo(ingredient); | ||||||
|  | 
 | ||||||
|     for (int i = 0; i < count; i++) { |     for (int i = 0; i < count; i++) { | ||||||
|         char decoded_name[MAX_NAME_LEN]; |         char decoded_name[MAX_NAME_LEN]; | ||||||
|         strncpy(decoded_name, dishes[i][0], MAX_NAME_LEN - 1); |         strncpy(decoded_name, dishes[i][0], MAX_NAME_LEN - 1); | ||||||
|         decoded_name[MAX_NAME_LEN - 1] = '\0'; // Гарантия завершения строки
 |         decoded_name[MAX_NAME_LEN - 1] = '\0'; | ||||||
| 
 | 
 | ||||||
|         decodovane_slovo(decoded_name); |         decodovane_slovo(decoded_name); | ||||||
| 
 | 
 | ||||||
|         // Приведение к нижнему регистру для сравнения
 |         if (strstr(decoded_name, ingredient) != NULL) { | ||||||
|         char decoded_ingredient[MAX_NAME_LEN]; |             printf("%s\n%s", dishes[i][0], dishes[i][1]); | ||||||
|         strncpy(decoded_ingredient, ingredient, MAX_NAME_LEN - 1); |             if (i < count - 1) { | ||||||
|         decoded_ingredient[MAX_NAME_LEN - 1] = '\0'; // Гарантия завершения строки
 |                 printf("\n"); | ||||||
|         decodovane_slovo(decoded_ingredient); // Применяем декодирование к ингредиенту
 |             } | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
|         if (strstr(decoded_name, decoded_ingredient) != NULL) { |  | ||||||
|             printf("%s\n%s\n", dishes[i][0], dishes[i][1]); |  | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|     printf("%s%d%s", "Nacitanych ", count, " poloziek. \n"); |     printf("Nacitanych %d poloziek.\n", count); | ||||||
|     return 0; |     return 0; | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user