usaa24/cv1/program.c

82 lines
2.6 KiB
C
Raw Normal View History

2024-10-01 13:36:19 +00:00
#include <stdio.h>
2024-10-01 21:47:00 +00:00
#include <string.h>
#include <ctype.h>
2024-10-01 22:00:31 +00:00
2024-10-01 21:47:00 +00:00
#define MAX_DISHES 100
#define MAX_NAME_LEN 100
2024-10-01 22:00:31 +00:00
2024-10-01 21:47:00 +00:00
void decodovane_slovo(char *text) {
for (int i = 0; text[i]; i++) {
switch (text[i]) {
case '0': text[i] = 'o'; break;
case '1': text[i] = 'i'; break;
case '2': text[i] = 'z'; break;
case '3': text[i] = 'e'; break;
case '4': text[i] = 'a'; break;
case '5': text[i] = 's'; break;
case '6': text[i] = 'b'; break;
case '7': text[i] = 't'; break;
case '8': text[i] = 'b'; break;
case '9': text[i] = 'q'; break;
default: text[i] = tolower(text[i]); break;
}
}
}
2024-10-01 22:00:31 +00:00
2024-10-01 13:36:19 +00:00
int main() {
2024-10-01 21:47:00 +00:00
char ingredient[MAX_NAME_LEN];
char dishes[MAX_DISHES][2][MAX_NAME_LEN];
int count = 0;
2024-10-01 21:57:29 +00:00
printf("Zadaj hladanu surovinu: ");
2024-10-01 22:03:29 +00:00
if (fgets(ingredient, MAX_NAME_LEN, stdin) == NULL) {
fprintf(stderr, "Ошибка чтения ингредиента.\n");
return 1; // Обработка ошибки
}
2024-10-01 21:47:00 +00:00
ingredient[strcspn(ingredient, "\n")] = '\0';
2024-10-01 22:00:31 +00:00
printf("Zadaj jedalny listok:\n");
2024-10-01 21:47:00 +00:00
while (1) {
char name[MAX_NAME_LEN], price[MAX_NAME_LEN];
2024-10-01 22:03:29 +00:00
if (fgets(name, MAX_NAME_LEN, stdin) == NULL) {
return 0; // Обработка ошибки
}
2024-10-01 21:47:00 +00:00
name[strcspn(name, "\n")] = '\0';
if (strlen(name) == 0) {
break;
}
2024-10-01 22:00:31 +00:00
2024-10-01 22:03:29 +00:00
if (fgets(price, MAX_NAME_LEN, stdin) == NULL) {
return 0; // Обработка ошибки
}
2024-10-01 21:47:00 +00:00
price[strcspn(price, "\n")] = '\0';
2024-10-01 22:00:31 +00:00
// Использование strncpy для безопасности
strncpy(dishes[count][0], name, MAX_NAME_LEN - 1);
dishes[count][0][MAX_NAME_LEN - 1] = '\0'; // Обеспечение завершенности строки
strncpy(dishes[count][1], price, MAX_NAME_LEN - 1);
dishes[count][1][MAX_NAME_LEN - 1] = '\0'; // Обеспечение завершенности строки
2024-10-01 21:47:00 +00:00
count++;
}
int matches = 0;
for (int i = 0; i < count; i++) {
char decoded_name[MAX_NAME_LEN];
2024-10-01 21:57:29 +00:00
strncpy(decoded_name, dishes[i][0], MAX_NAME_LEN - 1);
2024-10-01 22:00:31 +00:00
decoded_name[MAX_NAME_LEN - 1] = '\0'; // Гарантия завершения строки
2024-10-01 21:47:00 +00:00
decodovane_slovo(decoded_name);
if (strstr(decoded_name, ingredient)) {
2024-10-01 22:00:31 +00:00
printf("%s\n%s\n", dishes[i][0], dishes[i][1]);
2024-10-01 21:47:00 +00:00
matches++;
}
}
2024-10-01 22:00:31 +00:00
printf("%s%d%s", "Nacitanych ", count, " poloziek.\n");
2024-10-01 13:36:19 +00:00
return 0;
2024-10-01 22:00:31 +00:00
}