Advertisement
Guest User

Untitled

a guest
Feb 17th, 2020
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.64 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <limits.h>
  5.  
  6. typedef struct {
  7.     char name[125];
  8.     double amount, sum_of_products, sum_of_delivery, price_for_one;
  9. } Order;
  10.  
  11. void reference() {
  12.     printf("%s", "Минск, Дзержинского, 19, ст метро «Грушевка», магазин eFruit. LOTS987654321\n");
  13. }
  14.  
  15. double from_str_to_double(char s[125]) {
  16.     if (s[0] == '-') {
  17.         printf("%s", "Are you stupid??? It's a SHOP!!! Should be positive! EVERETHING\n");
  18.         return -1;
  19.     }
  20.     int i = 0, sum = 0;
  21.     while (s[i] != '\0') {
  22.         if (s[i] <= '9' && s[i] >= '0'&& sum<INT_MAX/100) {
  23.             sum = sum * 10 + (s[i] - '0');
  24.             i++;
  25.         } else {
  26.             printf("%s", "Are you stupid??? Should be NUMBER!\n");
  27.             return -1;
  28.         }
  29.     }
  30.     return sum;
  31. }
  32.  
  33. double delivery(Order order) {
  34.     double res = 0;
  35.     if (order.amount < 5) {
  36.         res = 1;
  37.     } else if (order.amount >= 5 && order.amount <= 20) {
  38.         res = 3;
  39.     } else {
  40.         res = 10 + (order.amount - 20) * 2;
  41.     }
  42.     return res;
  43. }
  44.  
  45. void count_total(Order my_order, double data[3]) {
  46.     if (strcmp(my_order.name, "mandarin") == 0) {
  47.         my_order.sum_of_products += my_order.amount * data[0];
  48.     } else if (strcmp(my_order.name, "peaches") == 0) {
  49.         my_order.sum_of_products += my_order.amount * data[1];
  50.     } else {
  51.         my_order.sum_of_products += my_order.amount * data[2];
  52.     }
  53. }
  54.  
  55. void for_file(FILE *f, char name[125]) {
  56.     f = fopen("warehouse.txt", "w+");
  57.     if (f != NULL) {
  58.         printf("ok\n");
  59.     } else {
  60.         printf("It looks like there are some problems \n");
  61.     }
  62. }
  63.  
  64. int main() {
  65.     FILE *fp;
  66.     FILE *fp1;
  67.     fp = fopen("data.txt", "r+");
  68.     fp1 = fopen("warehouse.txt", "w+");
  69.     if (fp != NULL && fp1 != NULL) {
  70.         //printf("ok\n");
  71.     } else {
  72.         printf("It looks like there are some problems \n");
  73.         return 0;
  74.     }
  75.     double data[3],flag_for_discounts = 0, flag_of_order = 0;
  76.     fscanf(fp, "%lf %lf %lf", &data[0], &data[1], &data[2]);
  77.     Order my_order;
  78.     my_order.sum_of_products = 0;
  79.     my_order.price_for_one = 0;
  80.     my_order.amount = 0;
  81.     char command[100];
  82.     while (1) {
  83.         printf("%s", "Enter command(contacts,make_an_order,quit,count_total_price_of_purchase,show_the_busket) \n ");
  84.         scanf("%s", command);
  85.         if (strcmp(command, "quit") == 0) {
  86.             puts("Thank you! Goodbye");
  87.             break;
  88.         }
  89.         if (strcmp(command, "contacts") == 0) {
  90.             reference();
  91.         }
  92.         if (strcmp(command, "make_an_order") == 0) {
  93.             fp1 = fopen("warehouse.txt", "a+");
  94.             if (fp1 == NULL) {
  95.                 printf("It looks like there are some problems \n");
  96.                 return 0;
  97.             }
  98.             char s[125];
  99.             puts("Enter name of product (mandarin,peaches,grape)");
  100.             scanf(" %s", my_order.name);
  101.             if (strcmp(my_order.name, "mandarin") == 0) {
  102.                 my_order.price_for_one = data[0];
  103.             } else if (strcmp(my_order.name, "peaches") == 0) {
  104.                 my_order.price_for_one = data[1];
  105.             } else if(strcmp(my_order.name, "grape") == 0){
  106.                 my_order.price_for_one = data[2];
  107.                 flag_of_order = 1;
  108.             } else{
  109.                 printf("%s", "There is no such product!\n");
  110.                 continue;
  111.             }
  112.             fprintf(fp1, "Name of product: %s  ", my_order.name);
  113.             puts("Enter amount of product");
  114.             scanf("%s", s);
  115.             my_order.amount = from_str_to_double(s);
  116.             if(my_order.amount != -1){
  117.             fprintf(fp1, "Amount of %s:  %lf   ", my_order.name, my_order.amount);
  118.             fprintf(fp1, "Price for one %s:  %.4lf\n", my_order.name, my_order.price_for_one);
  119.             fclose(fp1);
  120.             }
  121.  
  122.         }
  123.         if (strcmp(command, "count_total_price_of_purchase") == 0) {
  124. //count_total(my_order,data);
  125. if(flag_of_order){
  126.  
  127.             char answer[125];
  128.             my_order.sum_of_products += my_order.amount * my_order.price_for_one;
  129.             if (my_order.sum_of_products > 100) {
  130.                 my_order.sum_of_products *= 0.9;
  131.                 flag_for_discounts = 1;
  132.             }
  133.             printf("%s   %lf\n","Sum_of_products : " ,my_order.sum_of_products);
  134.             puts("Do you need delivery? yes /no \n");
  135.             scanf("%s", answer);
  136.             if (strcmp(answer, "yes") == 0) {
  137.                 if(flag_of_order){
  138.                 my_order.sum_of_delivery = delivery(my_order);
  139.                 printf("%s  %lf\n", "The delivery cost is: ", my_order.sum_of_delivery);
  140.             }
  141.             if (flag_for_discounts){
  142.                 printf("%s","You've got a discount: 10%\n");
  143.             }
  144.             printf("%s  %lf\n", "The total amount is: ", my_order.sum_of_delivery+my_order.sum_of_products);
  145.         }
  146. } else{
  147.     printf("%s","Make an order, please! \n");
  148. }
  149.         }
  150.         if (strcmp(command, "show_the_busket") == 0) {
  151.  
  152.             double sum_;
  153.  
  154.             fp1 = fopen("warehouse.txt", "r");
  155.             if (fp1 != NULL) {
  156.                 printf("ok\n");
  157.             } else {
  158.                 printf("It looks like there are some problems \n");
  159.                 return 0;
  160.             }
  161.             while (!feof(fp1)) {
  162.                 char s[125];
  163.                 //fscanf(fp1, "%100s", s);
  164.                 fgets(s, 100, fp1);
  165.                 if (!feof(fp1))
  166.                     printf("%s \n ", s);
  167.             }
  168.         }
  169.  
  170.     }
  171.     fclose(fp);
  172.     fclose(fp1);
  173.     return 0;
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement