Advertisement
Guest User

Лимонад

a guest
Aug 21st, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.96 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. typedef enum cloudiness { sunny, patly, cloudy, rain, heavy_rain } cloudiness;
  6. typedef struct comp_data {
  7.     enum cloudiness clouds;
  8.     int temp,
  9.         price_glass_buy,
  10.         price_flyer;
  11. } comp_data;
  12. typedef struct user_data {
  13.     int num_glass,
  14.         num_flyer,
  15.         price_glass_sell;
  16. } user_data;
  17.  
  18. comp_data static g_compData = { 0,0,0,0 };
  19. user_data static g_userData = { 0,0,0 };
  20.              int g_credit = 300;
  21.  
  22. void processing();
  23.     void сompData_calculation();
  24.     void output_сompData_calculation();
  25.     void input();
  26.         int getInt(int* pn);
  27.         int input_check_money();
  28.     void sell_info();
  29.         int glass_sold_num_calculation();
  30.             int glass_demand_calculation();
  31.     void end_day();
  32.         int credit_calculation();
  33.         void extra_credit();
  34. void end_game();
  35.  
  36. int main() {
  37.     const int days_num = 21;
  38.     for (int i = 0; i < days_num; i++) {
  39.         processing();
  40.     }
  41.     end_game();
  42. }
  43.  
  44. void processing() {
  45.     //Расчёт первоначальных данных
  46.     сompData_calculation();
  47.     //Вывод первоначальных данных
  48.     output_сompData_calculation();
  49.     //Считывание пользовательских данных
  50.     input();
  51.     //Расчёт продаж
  52.     sell_info();
  53.     //Расчёт баланса в завершении дня
  54.     end_day();
  55. }
  56.  
  57. void сompData_calculation() {
  58.     g_compData.clouds = rand() * time(NULL) % 2; //ранд бывает отрицательным
  59.     g_compData.temp = rand() * time(NULL) % 20 + 10;
  60.     g_compData.price_flyer = rand() * time(NULL) % 20 + 10;
  61.     g_compData.price_glass_buy = rand() * time(NULL) % 10 + 5;
  62. }
  63.  
  64. void output_сompData_calculation() {
  65.     printf("\nToday weather cast: clouds: %d\nTemperature: %d\nPrice for 1 flyer: %d\nPrice 1 glass: %d",
  66.         g_compData.clouds, g_compData.temp, g_compData.price_flyer, g_compData.price_glass_buy); //отдельный вывод облачности в строку, а не цифрой
  67. }
  68.  
  69. void input() {
  70.     puts("\nPlease, write the number of glasses :");
  71.     int error_input = 0;
  72.     while (!error_input)
  73.         error_input = getInt(&g_userData.num_glass);
  74.     error_input = 0;
  75.     while (!error_input)
  76.         error_input = input_check_money();//тут оно умирало в цикле, т.к. error_input не менялось
  77.    
  78.     puts("\nNumber of flyers :");
  79.     error_input = 0;
  80.     while (!error_input)
  81.         error_input = getInt(&g_userData.num_flyer);
  82.     error_input = 0;
  83.     while (!error_input)
  84.         error_input = input_check_money();
  85.  
  86.     puts("\n And price:");
  87.     error_input = 0;
  88.     while (!error_input)
  89.         error_input = getInt(&g_userData.price_glass_sell);
  90. }
  91.  
  92. int getInt(int* pn) {
  93.     const char *errmsg = "";
  94.     int n;
  95.     do {
  96.         puts(errmsg);
  97.         errmsg = "Repeat input";
  98.         n = scanf_s("%d", pn);
  99.         if (n < 0) // добавить проверку на отрицательность
  100.             return 0;
  101.         scanf_s("%*[^\n]");//Что это?
  102.         scanf_s("%*c");
  103.     } while (n == 0);
  104.     return 1;
  105. }
  106.  
  107. int input_check_money() {
  108.     if (g_credit >= g_compData.price_flyer * g_userData.num_flyer+ g_compData.price_glass_buy * g_userData.num_glass)
  109.         return 1;
  110.     else
  111.         return 0;
  112. }
  113.  
  114. void sell_info() {
  115.     printf("\nYou have sold %d glasses and have earned %d$",
  116.             glass_sold_num_calculation(), glass_sold_num_calculation()* g_userData.price_glass_sell); //ф-ия считается три раза (ещё один в credit_calc)
  117. }
  118.  
  119. int glass_sold_num_calculation() {
  120.     return round(glass_demand_calculation()*(0.5+0.05* g_userData.num_flyer));
  121. }
  122.  
  123. int glass_demand_calculation() {
  124.     return g_userData.num_glass * (4 - g_compData.clouds) / (30 - g_compData.temp);
  125. }
  126.  
  127. void end_day() {
  128.     if (!credit_calculation())
  129.         extra_credit();
  130. }
  131. int credit_calculation() {
  132.     g_credit = g_credit
  133.               - g_compData.price_glass_buy * g_userData.num_glass
  134.               + glass_sold_num_calculation() * g_userData.price_glass_sell;
  135.     if (g_credit > 0)
  136.         return 1;
  137.     else
  138.         return 0;
  139. }
  140. void extra_credit() {
  141.     return 0;
  142. } //дописать
  143. void end_game() {
  144.     return 0;
  145. } //дописать
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement