Update cv1/program.c

This commit is contained in:
Yurii Chechur 2024-09-30 16:41:25 +00:00
parent 1002f59cc0
commit 2c96efe9ae

View File

@ -1,6 +1,7 @@
#include <string.h> #include <string.h>
#include <stdio.h> #include <stdio.h>
void replace(char* DishName); void replace(char* DishName);
struct MenuItem { struct MenuItem {
@ -13,6 +14,7 @@ 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},
@ -20,36 +22,30 @@ 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];
strcpy(modifiedDish, menu[i].dish); replace(menu[i].dish);
replace(modifiedDish); // Модифікація страви для виведення printf("Chcete to to: %s, Cena: %.2f\n", menu[i].dish, menu[i].price);
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");
} }
@ -57,17 +53,25 @@ 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;
} }
} }
} }
} }