Advertisement
Guest User

Untitled

a guest
Feb 17th, 2020
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.46 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;
  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.             } 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.             my_order.amount = from_str_to_double(s);
  115.             if(my_order.amount != -1){
  116.             fprintf(fp1, "Amount of %s:  %lf   ", my_order.name, my_order.amount);
  117.             fprintf(fp1, "Price for one %s:  %.4lf\n", my_order.name, my_order.price_for_one);
  118.             fclose(fp1);
  119.             }
  120.  
  121.         }
  122.         if (strcmp(command, "count_total_price_of_purchase") == 0) {
  123. //count_total(my_order,data);
  124.             char answer[125];
  125.             my_order.sum_of_products += my_order.amount * my_order.price_for_one;
  126.             if (my_order.sum_of_products > 100) {
  127.                 my_order.sum_of_products *= 0.9;
  128.                 flag_for_discounts = 1;
  129.             }
  130.             printf("%s   %lf\n","Sum_of_products : " ,my_order.sum_of_products);
  131.             puts("Do you need delivery? yes /no \n");
  132.             scanf("%s", answer);
  133.             if (strcmp(answer, "yes") == 0) {
  134.                 my_order.sum_of_delivery = delivery(my_order);
  135.                 printf("%s  %lf\n", "The delivery cost is: ", my_order.sum_of_delivery);
  136.             }
  137.             if (flag_for_discounts){
  138.                 printf("%s","You've got a discount: 10%\n");
  139.             }
  140.             printf("%s  %lf\n", "The total amount is: ", my_order.sum_of_delivery+my_order.sum_of_products);
  141.         }
  142.         if (strcmp(command, "show_the_busket") == 0) {
  143.  
  144.             double sum_;
  145.  
  146.             fp1 = fopen("warehouse.txt", "r");
  147.             if (fp1 != NULL) {
  148.                 printf("ok\n");
  149.             } else {
  150.                 printf("It looks like there are some problems \n");
  151.                 return 0;
  152.             }
  153.             while (!feof(fp1)) {
  154.                 char s[125];
  155.                 //fscanf(fp1, "%100s", s);
  156.                 fgets(s, 100, fp1);
  157.                 if (!feof(fp1))
  158.                     printf("%s \n ", s);
  159.             }
  160.         }
  161.  
  162.     }
  163.     fclose(fp);
  164.     fclose(fp1);
  165.     return 0;
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement