Обновить cv1/program.c

This commit is contained in:
Yevhen Kozirovskyi 2024-10-01 22:00:31 +00:00
parent caa09c8456
commit 3eb785a320

View File

@ -1,8 +1,10 @@
#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]) {
@ -20,17 +22,17 @@ 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];
int count = 0; int count = 0;
printf("Zadaj hladanu surovinu: "); printf("Zadaj hladanu surovinu: ");
fgets(ingredient, MAX_NAME_LEN, stdin); fgets(ingredient, MAX_NAME_LEN, stdin);
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];
@ -40,12 +42,18 @@ int main() {
if (strlen(name) == 0) { if (strlen(name) == 0) {
break; break;
} }
// Ввод цены // Ввод цены
fgets(price, MAX_NAME_LEN, stdin); fgets(price, MAX_NAME_LEN, stdin);
price[strcspn(price, "\n")] = '\0'; price[strcspn(price, "\n")] = '\0';
strcpy(dishes[count][0], name); // Использование strncpy для безопасности
strcpy(dishes[count][1], price); 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'; // Обеспечение завершенности строки
count++; count++;
} }
@ -53,15 +61,15 @@ int main() {
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)) { if (strstr(decoded_name, ingredient)) {
printf("%s\n%s\n", dishes[i][0], dishes[i][1] ); printf("%s\n%s\n", dishes[i][0], dishes[i][1]);
matches++; matches++;
} }
} }
printf("%s%d%s","Nacitanych ",count," poloziek."); printf("%s%d%s", "Nacitanych ", count, " poloziek.\n");
return 0; return 0;
} }