Update cv1/program.c

This commit is contained in:
Yurii Chechur 2024-09-30 12:35:14 +00:00
parent c53b20e10b
commit 1002f59cc0

View File

@ -1,7 +1,6 @@
#include <string.h> #include <string.h>
#include <stdio.h> #include <stdio.h>
void replace(char* DishName); void replace(char* DishName);
struct MenuItem { struct MenuItem {
@ -14,7 +13,6 @@ char answer1[100];
int main(void) { int main(void) {
struct MenuItem menu[] = { struct MenuItem menu[] = {
{"Pizza", "Margarita", 4.4}, {"Pizza", "Margarita", 4.4},
{"Pizza", "Cucumber", 4.4}, {"Pizza", "Cucumber", 4.4},
@ -22,30 +20,36 @@ int main(void) {
{"Pizza", "Paperoni", 4.4} {"Pizza", "Paperoni", 4.4}
}; };
// Очікуване виведення для введення
printf("Zadaj hladanu surovinu: "); printf("Zadaj hladanu surovinu: ");
fgets(answer1, 100, stdin); fgets(answer1, 100, stdin);
if (answer1[strlen(answer1) - 1] == '\n') { if (answer1[strlen(answer1) - 1] == '\n') {
answer1[strlen(answer1) - 1] = '\0'; answer1[strlen(answer1) - 1] = '\0';
} }
// Вивести повідомлення про завантаження меню
int MenuSize = sizeof(menu) / sizeof(menu[0]); int MenuSize = sizeof(menu) / sizeof(menu[0]);
printf("Zadaj jedalny listok:\n");
for (size_t i = 0; i < MenuSize; i++) {
printf("%s\n%.2f\n", menu[i].dish, menu[i].price);
}
printf("Nacitanych %d poloziek.\n", MenuSize);
int found = 0; int found = 0;
// Пошук страв за введеною сировиною
for (size_t i = 0; i < MenuSize; i++) { for (size_t i = 0; i < MenuSize; i++) {
if (strstr(menu[i].dish, answer1) != NULL) { if (strstr(menu[i].dish, answer1) != NULL) {
found = 1; found = 1;
char modifiedDish[50];
replace(menu[i].dish); strcpy(modifiedDish, menu[i].dish);
printf("Chcete to to: %s, Cena: %.2f\n", menu[i].dish, menu[i].price); replace(modifiedDish); // Модифікація страви для виведення
printf("Chcete to to: %s, Cena: %.2f\n", modifiedDish, menu[i].price);
} }
} }
if (!found) { if (!found) {
printf("Zadana surovina nebola najdena.\n"); printf("Zadana surovina nebola najdena.\n");
} }
@ -53,24 +57,17 @@ int main(void) {
return 0; return 0;
} }
void replace(char* DishName) { void replace(char* DishName) {
char original[] = "aAeEiIoOsSzZ"; char original[] = "aAeEiIoOsSzZ";
char replacement[] = "443311005522"; char replacement[] = "443311005522";
int l = strlen(DishName); int l = strlen(DishName);
for (int i = 0; i < l; i++) { for (int i = 0; i < l; i++) {
for (int j = 0; j < strlen(original); j++) { for (int j = 0; j < strlen(original); j++) {
if (DishName[i] == original[j]) { if (DishName[i] == original[j]) {
DishName[i] = replacement[j]; DishName[i] = replacement[j];
break; break;
} }
} }
} }
} }