Advertisement
Guest User

Untitled

a guest
Feb 18th, 2020
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.01 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, total_sum;
  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.     } else {
  59.         printf("It looks like there are some problems \n");
  60.     }
  61. }
  62.  
  63. int main() {
  64.     FILE *fp;
  65.     FILE *fp1;
  66.     fp = fopen("data.txt", "r+");
  67.     fp1 = fopen("warehouse.txt", "w+");
  68.     if (fp != NULL && fp1 != NULL) {
  69.         //printf("ok\n");
  70.     } else {
  71.         printf("It looks like there are some problems \n");
  72.         return 0;
  73.     }
  74.     double data[3], flag_for_discounts = 0, flag_of_order = 0;
  75.     fscanf(fp, "%lf %lf %lf", &data[0], &data[1], &data[2]);
  76.     Order my_order;
  77.     my_order.sum_of_products = 0;
  78.     my_order.price_for_one = 0;
  79.     my_order.amount = 0;
  80.     char command[100];
  81.     while (1) {
  82.         printf("%s", "Enter command(contacts,make_an_order,quit,count_total_price_of_purchase,show_the_busket) \n ");
  83.         scanf("%s", command);
  84.         if (strcmp(command, "quit") == 0) {
  85.             puts("Thank you! Goodbye");
  86.             break;
  87.         } else if (strcmp(command, "contacts") == 0) {
  88.             reference();
  89.         } else if (strcmp(command, "make_an_order") == 0) {
  90.             fp1 = fopen("warehouse.txt", "a+");
  91.             if (fp1 == NULL) {
  92.                 printf("It looks like there are some problems \n");
  93.                 return 0;
  94.             }
  95.             char s[125];
  96.             puts("Enter name of product (mandarin,peaches,grape)");
  97.             scanf(" %s", my_order.name);
  98.             if (strcmp(my_order.name, "mandarin") == 0) {
  99.                 my_order.price_for_one = data[0];
  100.                 //flag_of_order = 1;
  101.             } else if (strcmp(my_order.name, "peaches") == 0) {
  102.                 my_order.price_for_one = data[1];
  103.                // flag_of_order = 1;
  104.             } else if (strcmp(my_order.name, "grape") == 0) {
  105.                 my_order.price_for_one = data[2];
  106.                //flag_of_order = 1;
  107.             } else {
  108.                 printf("%s", "There is no such product!\n");
  109.                 continue;
  110.             }
  111.             fprintf(fp1, "Name of product: %s  ", my_order.name);
  112.             puts("Enter amount of product");
  113.             scanf("%s", s);
  114.             double tmp =0;
  115.             tmp = from_str_to_double(s);
  116.             if (tmp > 0) {
  117.                 fprintf(fp1, "Amount of %s:  %.4lf   ", my_order.name, tmp);
  118.                 fprintf(fp1, "Price for one %s:  %.4lf\n", my_order.name, my_order.price_for_one);
  119.                 flag_of_order = 1;
  120.                 fclose(fp1);
  121.             }
  122.             my_order.sum_of_products += tmp * my_order.price_for_one;
  123.             if (my_order.sum_of_products > 100) {
  124.                 my_order.sum_of_products *= 0.9;
  125.                 flag_for_discounts = 1;
  126.             }
  127.  
  128.             my_order.amount += tmp;
  129.  
  130.         } else
  131.         if (strcmp(command, "count_total_price_of_purchase") == 0) {
  132. //count_total(my_order,data);
  133.             if (flag_of_order) {
  134.                 char answer[125];
  135.  
  136.                 printf("%s   %lf\n", "Sum_of_products : ", my_order.sum_of_products);
  137.                 puts("Do you need delivery? yes /no \n");
  138.                 scanf("%s", answer);
  139.                 if (strcmp(answer, "yes") == 0) {
  140.                     my_order.sum_of_delivery = delivery(my_order);
  141.                     printf("%s  %lf\n", "The delivery cost is: ", my_order.sum_of_delivery);
  142.                 }
  143.                 if (flag_for_discounts) {
  144.                     printf("%s", "You've got a discount: 10%\n");
  145.                 }
  146.                 printf("%s  %lf\n", "The total amount is: ", my_order.sum_of_delivery + my_order.sum_of_products);
  147.             my_order.sum_of_delivery = 0;
  148.             } else {
  149.                 printf("%s", "Make an order, please! \n");
  150.             }
  151.         } else
  152.         if (strcmp(command, "show_the_busket") == 0) {
  153.             if(flag_of_order) {
  154.                 fp1 = fopen("warehouse.txt", "r");
  155.                 if (fp1 != NULL) {
  156.                 } else {
  157.                     printf("It looks like there are some problems \n");
  158.                     return 0;
  159.                 }
  160.                 while (!feof(fp1)) {
  161.                     char s[125];
  162.                     fgets(s, 100, fp1);
  163.                     if (!feof(fp1))
  164.                         printf("%s \n ", s);
  165.                 }
  166.             } else{
  167.                 printf("%s", "There is no products\n");
  168.             }
  169.         } else{
  170.             printf("%s","Unknown command\n");
  171.         }
  172.  
  173.     }
  174.     fclose(fp);
  175.     fclose(fp1);
  176.     return 0;
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement