From 00ea04cddef8efa017b9323406eb2245676588dd Mon Sep 17 00:00:00 2001 From: Eduard Brusnigin <149200142+EduardBrusnigin@users.noreply.github.com> Date: Wed, 25 Dec 2024 22:23:46 +0000 Subject: [PATCH 1/2] Sigma kassa --- 02_Brusnigin/Lab2_Kassa/code.c | 172 +++++++++++++++++++++++++++++++++ 1 file changed, 172 insertions(+) create mode 100644 02_Brusnigin/Lab2_Kassa/code.c diff --git a/02_Brusnigin/Lab2_Kassa/code.c b/02_Brusnigin/Lab2_Kassa/code.c new file mode 100644 index 0000000..e69d22a --- /dev/null +++ b/02_Brusnigin/Lab2_Kassa/code.c @@ -0,0 +1,172 @@ +#include +#include +#include +#include + + +struct Product { + char code[5]; + char name[50]; + double kg_price; + double amount; +}; + +struct Product Warehouse[] = { + {"99919", "Banana", 199.0, 25.0}, + {"34583", "Apple", 149.99, 135.0}, + {"12345", "Cucumber", 299.0, 50.5}, + {"54322", "Tomato", 269.0, 30.0}, + {"69696", "Potato", 149.99, 50.0}, + {"12365", "Cheese", 395.0, 10.0}, + {"99999", "Pepsi", 199.99, 50.3}, + {"87425", "Carrot", 89.50, 20.1}, + {"23411", "Bread", 57.10, 25.0}, + {"93411", "Chicken", 435.50, 14.2} +}; + +struct Product Check[100]; + + +int get_list(char List[][100]) { + char string[100]; + + int i = 0; + do { + fgets(string, 100, stdin); + strcpy(List[i], string); + i++; + } + while (strlen(string) != 1); + + return i; +} + + +void add_in_check(char code[5], char name[50], double kg_price, double amount, int i) { + strcpy(Check[i].code, code); + strcpy(Check[i].name,name); + Check[i].kg_price = kg_price*amount; + Check[i].amount = amount; +} + +void print_check(int n) { + int i; double sum; + + FILE *file = fopen("check.txt", "w"); + + printf("==========CHECK==========\n"); + fputs("==========CHECK==========\n", file); + + for (i = 0; i < n; i++){ + printf(Check[i].name); + fputs(Check[i].name, file); + + printf(" "); + fputs(" ", file); + + printf("%.2f\n", Check[i].kg_price); + fprintf(file, "%.2f\n", Check[i].kg_price); + + sum += Check[i].kg_price; + } + printf("\nSum = %.2f\n", sum); + fprintf(file, "\nSum = %.2f\n", sum); + + printf("=========================\n"); + fputs("=========================", file); + + printf("\nFile with check - check.txt\n"); + + fclose(file); +} + + +int main() { + int amount_in_list; // количество записей в списке покупок + int tokens_amount; + int i, j; + int buyed = 0; + int items = sizeof(Warehouse)/sizeof(Warehouse[0]); + + char* token; + char* name; + char* amount; + + int name_flag, amount_flag; + + char List[25][100]; // список покупок + char* Assortment[10]; // ассортимент (названия) + + for (i=0; i < items; i++){ + Assortment[i] = Warehouse[i].name; + //Assortment[i][0] = tolower(Assortment[i][0]); + } + + + printf("Shopping list:\n"); + amount_in_list = get_list(List); + + + for (i=0; i < amount_in_list-1; i++){ + token = strtok(List[i], " "); + + tokens_amount = 0; + + name_flag = 0; amount_flag = 0; + + while (token != NULL) { + tokens_amount++; + + for (j=0; j= 4 && (strstr(name, Assortment[j]) || strstr(Assortment[j], name))) { + if (Warehouse[j].amount >= strtod(amount, NULL)){ + printf(Warehouse[j].name); + printf(": "); + printf(amount); + printf("*"); + printf("%.2f", Warehouse[j].kg_price); + printf("\n"); + + add_in_check(Warehouse[j].code, Warehouse[j].name, Warehouse[j].kg_price, strtod(amount, NULL), buyed); + buyed++; + } + + else { + printf(Warehouse[j].name); + printf(": "); + printf("Not enough goods in warehouse\n"); + } + + break; + } + } + } + } + + printf("\n"); + print_check(buyed); +} From 20624e2ad816d6400dd38f2c791bef566a58d020 Mon Sep 17 00:00:00 2001 From: Eduard Brusnigin <149200142+EduardBrusnigin@users.noreply.github.com> Date: Sun, 29 Dec 2024 15:09:51 +0000 Subject: [PATCH 2/2] super goida kassa --- 02_Brusnigin/Lab2_Kassa/code.c | 38 ++++++++++++++++++++++++---------- 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/02_Brusnigin/Lab2_Kassa/code.c b/02_Brusnigin/Lab2_Kassa/code.c index e69d22a..38b5ab9 100644 --- a/02_Brusnigin/Lab2_Kassa/code.c +++ b/02_Brusnigin/Lab2_Kassa/code.c @@ -11,6 +11,8 @@ struct Product { double amount; }; +typedef struct Product Product; + struct Product Warehouse[] = { {"99919", "Banana", 199.0, 25.0}, {"34583", "Apple", 149.99, 135.0}, @@ -42,15 +44,30 @@ int get_list(char List[][100]) { } -void add_in_check(char code[5], char name[50], double kg_price, double amount, int i) { - strcpy(Check[i].code, code); - strcpy(Check[i].name,name); - Check[i].kg_price = kg_price*amount; - Check[i].amount = amount; +int add_in_check(char code[5], char name[50], double kg_price, double amount, int i) { + int flag = -1; + + for (int j=0; j < sizeof(Check)/sizeof(Check[0]); j++) + if (strcmp(code, Check[j].code) == 0) + flag = j; + + if (flag != -1){ + Check[flag].kg_price += kg_price*amount; + Check[flag].amount += amount; + return 0; + } + + else { + strcpy(Check[i].code, code); + strcpy(Check[i].name,name); + Check[i].kg_price = kg_price*amount; + Check[i].amount = amount; + return 1; + } } void print_check(int n) { - int i; double sum; + int i; double sum = 0.0; FILE *file = fopen("check.txt", "w"); @@ -89,8 +106,8 @@ int main() { int items = sizeof(Warehouse)/sizeof(Warehouse[0]); char* token; - char* name; - char* amount; + char* name = ""; + char* amount = ""; int name_flag, amount_flag; @@ -151,8 +168,7 @@ int main() { printf("%.2f", Warehouse[j].kg_price); printf("\n"); - add_in_check(Warehouse[j].code, Warehouse[j].name, Warehouse[j].kg_price, strtod(amount, NULL), buyed); - buyed++; + buyed += add_in_check(Warehouse[j].code, Warehouse[j].name, Warehouse[j].kg_price, strtod(amount, NULL), buyed); } else { @@ -169,4 +185,4 @@ int main() { printf("\n"); print_check(buyed); -} +} \ No newline at end of file