Advertisement
Guest User

lab_05_trash...

a guest
Nov 22nd, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 51.94 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <string.h>
  5. #include <math.h>
  6.  
  7. #define MIN(a, b)(a < b ? a : b)
  8. #define MAX(a, b)(a < b ? b : a)
  9. #define TRUE 1
  10. #define FULL_QUEUE 1
  11. #define SUCCESS 0
  12. #define VECTOR_LEN 100
  13. #define EPS 10E-4
  14.  
  15. //(0 + rand() % (10 - 0));
  16.  
  17. typedef struct
  18. {
  19.     double *p_in;
  20.     double *p_out;
  21.     double *low_border;
  22.     double *high_border;
  23.     int is_full;
  24.     int is_working;
  25. } vector_descriptor_t;
  26.  
  27. typedef struct list_descriptor
  28. {
  29.     double val;
  30.     struct list_descriptor *next;
  31. } list_descriptor_t;
  32.  
  33. // REWORK: PLUSS ALL ELEMENTS, EXCEPT P_OUT; CHECK IF THEY IN MACHINE LATER
  34. void elements_wait(vector_descriptor_t vector_descriptor_a,
  35.                    vector_descriptor_t vector_descriptor_b, double time)  // INCORRECT WORK
  36. {
  37.     for(double *ptr = vector_descriptor_a.low_border; ptr <= vector_descriptor_a.high_border; ptr++)
  38.     {
  39.         if (ptr != vector_descriptor_a.p_out &&
  40.             ptr !=vector_descriptor_b.p_out)
  41.             (*ptr) += time;
  42.     }
  43.     /*
  44.     if (vector_descriptor.is_full)
  45.         for(double *ptr = vector_descriptor.low_border; ptr <= vector_descriptor.high_border; ptr++)
  46.             *ptr += time;
  47.     else
  48.     {
  49.         if (vector_descriptor.p_in < vector_descriptor.p_out)
  50.             for(double *ptr = vector_descriptor.p_in; ptr != vector_descriptor.p_out; ptr++)
  51.             {
  52.                 *ptr += time;
  53.                 if (ptr == vector_descriptor.high_border)
  54.                     ptr = vector_descriptor.low_border - 1;
  55.             }
  56.         else
  57.             for(double *ptr = vector_descriptor.p_out; ptr != vector_descriptor.p_in; ptr++)
  58.             {
  59.                 *ptr += time;
  60.                 if (ptr == vector_descriptor.high_border)
  61.                     ptr = vector_descriptor.low_border - 1;
  62.             }
  63.     }
  64. */
  65.     return;
  66. }
  67.  
  68.  
  69. void vector_pop(vector_descriptor_t *vector_descriptor)
  70. {
  71.     if (vector_descriptor -> p_out == vector_descriptor -> high_border)
  72.         vector_descriptor -> p_out = vector_descriptor -> low_border;
  73.     else
  74.         (vector_descriptor -> p_out) += 1;
  75.  
  76.     if (vector_descriptor -> is_full)
  77.         (vector_descriptor -> is_full) = 0;
  78.  
  79. }
  80.  
  81. void vector_push(vector_descriptor_t *vector_descriptor)
  82. {
  83.     if (!(vector_descriptor -> is_full))
  84.     {
  85.         if ((vector_descriptor -> p_in) == (vector_descriptor -> high_border))
  86.             vector_descriptor -> p_in = vector_descriptor -> low_border;
  87.         else
  88.             (vector_descriptor -> p_in)++;
  89.  
  90.  
  91.         if ((vector_descriptor -> p_in) == (vector_descriptor -> p_out))
  92.             (vector_descriptor -> is_full) = 1;
  93.     }
  94.  
  95.     return;
  96. }
  97.  
  98. /*
  99. double* vector_pop(vector_descriptor_t *vector_descriptor)
  100. {
  101.     double* out = vector_descriptor -> p_out;
  102.     if (vector_descriptor -> p_out == vector_descriptor -> high_border)
  103.         vector_descriptor -> p_out = vector_descriptor -> low_border;
  104.     else
  105.         (vector_descriptor -> p_out) += 1;
  106.  
  107.     if (vector_descriptor -> is_full)
  108.         (vector_descriptor -> is_full) = 0;
  109.  
  110.     return out;
  111. }
  112.  
  113. void vector_push(vector_descriptor_t *vector_descriptor, double* element)
  114. {
  115.     if (!(vector_descriptor -> is_full))
  116.     {
  117.         (vector_descriptor -> p_in) = element;
  118.         if ((vector_descriptor -> p_in) == (vector_descriptor -> high_border))
  119.             vector_descriptor -> p_in = vector_descriptor -> low_border;
  120.         else
  121.             (vector_descriptor -> p_in)++;
  122.     }
  123.  
  124.     if ((vector_descriptor -> p_in) == (vector_descriptor -> p_out))
  125.         (vector_descriptor -> is_full) = 1;
  126.  
  127.     return;
  128. }
  129. */
  130. long int queue_len(vector_descriptor_t vector_descriptor)
  131. {
  132.     if (vector_descriptor.is_full)
  133.         return (vector_descriptor.high_border - vector_descriptor.low_border) + 1;
  134.  
  135.     if (vector_descriptor.p_in == vector_descriptor.p_out)
  136.         return 0;
  137.  
  138.     long int cnt = 0;
  139.     for(double *ptr = vector_descriptor.p_out; ptr != vector_descriptor.p_in; ptr++)
  140.     {
  141.         cnt++;
  142.         if (ptr == vector_descriptor.high_border)
  143.             ptr = vector_descriptor.low_border - 1;
  144.     }
  145.  
  146.     return cnt;
  147. }
  148.  
  149. void vector_imitate_queue(vector_descriptor_t vector_descriptor_a, vector_descriptor_t vector_descriptor_b,
  150.                      double min_process_a, double max_process_a,
  151.                      double min_process_b, double max_process_b,
  152.                      double *time_a, double *time_b, double *time_b_waiting, double *time_a_waiting,
  153.                      size_t *cnt_a_work, double p, long int elements_exit, size_t *append_in_b, int is_print)
  154. {
  155.     /*
  156.     double decr = (max_process_a - min_process_a) / 4;
  157.     max_process_a -= decr;
  158.     min_process_a += decr;
  159.     decr = (max_process_b - min_process_b) / 4;
  160.     max_process_b -= decr;
  161.     min_process_b += decr;
  162.     */
  163.     int cnt_b_out = 0;
  164.     double time_a_work = (max_process_a - min_process_a) * ((rand() % 100) / 100.0) + min_process_a;
  165.     //(*cnt_a_work)++;
  166.     double time_b_work = 0;
  167.     vector_descriptor_a.is_working = 1;
  168.     while(cnt_b_out != elements_exit)
  169.     {
  170.         if (vector_descriptor_a.is_working)  // A working
  171.         {
  172.             if (vector_descriptor_b.is_working)  // Both apparats are working right now
  173.             {
  174.                 double time_min = MIN(time_a_work, time_b_work);
  175.                 time_a_work -= time_min;
  176.                 time_b_work -= time_min;
  177.                 elements_wait(vector_descriptor_a, vector_descriptor_b, time_min);
  178.                 (*time_a) += time_min;
  179.                 (*time_b) += time_min;
  180.                 if (time_a_work < EPS)
  181.                 {
  182.                     vector_descriptor_a.is_working = 0;
  183.                     if(((rand() % 100) / 100.0) > p)
  184.                     {
  185.                         (*append_in_b)++;
  186.                         vector_pop(&vector_descriptor_a);
  187.                         vector_push(&vector_descriptor_b);
  188.                         //vector_push(&vector_descriptor_b, vector_pop(&vector_descriptor_a));
  189.                     }
  190.                     else
  191.                     {
  192.                         vector_pop(&vector_descriptor_a);
  193.                         vector_push(&vector_descriptor_a);
  194.                         //vector_push(&vector_descriptor_a, vector_pop(&vector_descriptor_a));
  195.                     }
  196.                 }
  197.  
  198.                 if (time_b_work < EPS)
  199.                 {
  200.                     cnt_b_out++;
  201.                     vector_descriptor_b.is_working = 0;
  202.                     vector_pop(&vector_descriptor_b);
  203.                     vector_push(&vector_descriptor_a);
  204.                     //vector_push(&vector_descriptor_a, vector_pop(&vector_descriptor_b));
  205.                 }
  206.  
  207.  
  208.                 if ((time_a_work < EPS) && (vector_descriptor_a.p_in != vector_descriptor_a.p_out || vector_descriptor_a.is_full))
  209.                 {
  210.                     (*cnt_a_work)++;
  211.                     vector_descriptor_a.is_working = 1;
  212.                     time_a_work = (max_process_a - min_process_a) * ((rand() % 100) / 100.0) + min_process_a;
  213.                 }
  214.  
  215.                 if ((time_b_work < EPS) && (vector_descriptor_b.p_in != vector_descriptor_b.p_out || vector_descriptor_b.is_full))
  216.                 {
  217.                     vector_descriptor_b.is_working = 1;
  218.                     time_b_work = (max_process_b - min_process_b) * ((rand() % 100) / 100.0) + min_process_b;
  219.                 }
  220.             }
  221.             else  // B is NOT working right now
  222.             {
  223.                 (*time_a) += time_a_work;
  224.                 (*time_b_waiting) += time_a_work;
  225.                 elements_wait(vector_descriptor_a, vector_descriptor_b, time_a_work);
  226.                 if (vector_descriptor_b.p_in != vector_descriptor_b.p_out || vector_descriptor_b.is_full)
  227.                     (*vector_descriptor_b.p_out) += time_a_work;
  228.  
  229.                 time_a_work = 0;
  230.                 if (time_a_work < EPS)
  231.                 {
  232.                     vector_descriptor_a.is_working = 0;
  233.                     if(((rand() % 100) / 100.0) > p)
  234.                     {
  235.                         (*append_in_b)++;
  236.                         vector_pop(&vector_descriptor_a);
  237.                         vector_push(&vector_descriptor_b);
  238.                         //vector_push(&vector_descriptor_b, vector_pop(&vector_descriptor_a));
  239.                     }
  240.                     else
  241.                     {
  242.                         vector_pop(&vector_descriptor_a);
  243.                         vector_push(&vector_descriptor_a);
  244.                         //vector_push(&vector_descriptor_a, vector_pop(&vector_descriptor_a));
  245.                     }
  246.                 }
  247.  
  248.                 if ((time_a_work < EPS) && (vector_descriptor_a.p_in != vector_descriptor_a.p_out || vector_descriptor_a.is_full))
  249.                 {
  250.                     (*cnt_a_work)++;
  251.                     vector_descriptor_a.is_working = 1;
  252.                     time_a_work = (max_process_a - min_process_a) * ((rand() % 100) / 100.0) + min_process_a;
  253.                 }
  254.  
  255.                 if ((time_b_work < EPS) && (vector_descriptor_b.p_in != vector_descriptor_b.p_out || vector_descriptor_b.is_full))
  256.                 {
  257.                     vector_descriptor_b.is_working = 1;
  258.                     time_b_work = (max_process_b - min_process_b) * ((rand() % 100) / 100.0) + min_process_b;
  259.                 }
  260.  
  261.             }
  262.         }
  263.         else // A don`t work
  264.         {
  265.             if (vector_descriptor_b.is_working)  // ONLY B works right now
  266.             {
  267.                 (*time_b) += time_b_work;
  268.                 (*time_a_waiting) += time_b_work;
  269.                 elements_wait(vector_descriptor_a, vector_descriptor_b, time_b_work);
  270.                 if (vector_descriptor_a.p_in != vector_descriptor_a.p_out || vector_descriptor_a.is_full)
  271.                     (*vector_descriptor_a.p_out) += time_b_work;
  272.  
  273.                 time_b_work = 0;
  274.                 if (time_b_work < EPS)
  275.                 {
  276.                     cnt_b_out++;
  277.                     vector_descriptor_b.is_working = 0;
  278.                     vector_pop(&vector_descriptor_b);
  279.                     vector_push(&vector_descriptor_a);
  280.                     //vector_push(&vector_descriptor_a, vector_pop(&vector_descriptor_b));
  281.                 }
  282.  
  283.  
  284.                 if ((time_a_work < EPS) && (vector_descriptor_a.p_in != vector_descriptor_a.p_out || vector_descriptor_a.is_full))
  285.                 {
  286.                     (*cnt_a_work)++;
  287.                     vector_descriptor_a.is_working = 1;
  288.                     time_a_work = (max_process_a - min_process_a) * ((rand() % 100) / 100.0) + min_process_a;
  289.                 }
  290.  
  291.                 if ((time_b_work < EPS) && (vector_descriptor_b.p_in != vector_descriptor_b.p_out || vector_descriptor_b.is_full))
  292.                 {
  293.                     vector_descriptor_b.is_working = 1;
  294.                     time_b_work = (max_process_b - min_process_b) * ((rand() % 100) / 100.0) + min_process_b;
  295.                 }
  296.             }
  297.             else // NOTHING is working at the moment
  298.             {
  299.                 if ((time_a_work < EPS) && (vector_descriptor_a.p_in != vector_descriptor_a.p_out || vector_descriptor_a.is_full))
  300.                 {
  301.                     (*cnt_a_work)++;
  302.                     vector_descriptor_a.is_working = 1;
  303.                     time_a_work = (max_process_a - min_process_a) * ((rand() % 100) / 100.0) + min_process_a;
  304.                 }
  305.  
  306.                 if ((time_b_work < EPS) && (vector_descriptor_b.p_in != vector_descriptor_b.p_out || vector_descriptor_b.is_full))
  307.                 {
  308.                     vector_descriptor_b.is_working = 1;
  309.                     time_b_work = (max_process_b - min_process_b) * ((rand() % 100) / 100.0) + min_process_b;
  310.                 }
  311.             }
  312.         }
  313.         /*
  314.         if (cnt_b_out == 1)
  315.         {
  316.             printf("Current length of queue A: %li\n", queue_len(vector_descriptor_a));
  317.             printf("Current length of queue B: %li\n", queue_len(vector_descriptor_b));
  318.         }*/
  319.  
  320.         if (!(cnt_b_out % 100) && cnt_b_out != 0 && is_print)
  321.         {
  322.             printf("Current length of queue A: %li\n", queue_len(vector_descriptor_a));
  323.             printf("Current length of queue B: %li\n", queue_len(vector_descriptor_b));
  324.             double sum_time_wait = 0;
  325.             for (double *ptr = vector_descriptor_a.low_border; ptr <= vector_descriptor_a.high_border; ptr++)
  326.                 sum_time_wait += *ptr;
  327.             if (sum_time_wait / (vector_descriptor_a.high_border - vector_descriptor_a.low_border) > EPS)
  328.             printf("Average waiting time %lf\n",
  329.                    sum_time_wait / (vector_descriptor_a.high_border - vector_descriptor_a.low_border));
  330.             else
  331.                 printf("Elements almost don`t stand in queue\n");
  332.             printf("-----------------------------------------------\n");
  333.         }
  334.     }
  335.  
  336.     return;
  337. }
  338.  
  339.  
  340. void elements_list_wait(list_descriptor_t *list_descriptor_a, list_descriptor_t *list_descriptor_b,
  341.                         double time_min)
  342. {
  343.     for (list_descriptor_t *ptr = list_descriptor_a; ptr != NULL; ptr = ptr -> next)
  344.     {
  345.         ptr -> val += time_min;
  346.     }
  347.  
  348.     for (list_descriptor_t *ptr = list_descriptor_b; ptr != NULL; ptr = ptr -> next)
  349.     {
  350.         ptr -> val += time_min;
  351.     }
  352.  
  353.     return;
  354. }
  355.  
  356. list_descriptor_t *list_pop(list_descriptor_t **list_descriptor)
  357. {
  358.     list_descriptor_t *ret = *list_descriptor;
  359.     *list_descriptor = (*list_descriptor)-> next;
  360.     return ret;
  361. }
  362.  
  363. list_descriptor_t *list_push(list_descriptor_t *list_descriptor, list_descriptor_t *list_append)
  364. {
  365.     list_append -> next = NULL;
  366.     if (list_descriptor == NULL)
  367.     {
  368.         list_descriptor = list_append;
  369.         return list_descriptor;
  370.     }
  371.  
  372.     list_descriptor_t *ptr = list_descriptor;
  373.     while ((ptr -> next) != NULL)
  374.         ptr = ptr -> next;
  375.  
  376.     ptr -> next = list_append;
  377.     return list_descriptor;
  378. }
  379.  
  380. long int queue_list_len(list_descriptor_t *list_descriptor)
  381. {
  382.     long int cnt = 0;
  383.     while (list_descriptor != NULL)
  384.     {
  385.         cnt++;
  386.         list_descriptor = list_descriptor -> next;
  387.     }
  388.  
  389.     return cnt;
  390. }
  391.  
  392. void list_imitate_queue(list_descriptor_t *list_descriptor_a, list_descriptor_t *list_descriptor_b,
  393.                      double min_process_a, double max_process_a,
  394.                      double min_process_b, double max_process_b,
  395.                      double *time_a, double *time_b, double *time_b_waiting, double *time_a_waiting,
  396.                      size_t *cnt_a_work, double p, long int elements_exit, size_t *append_in_b , long int queue_size,
  397.                      double *sum_time_wait, int is_print)
  398. {
  399.     int cnt_b_out = 0;
  400.     double time_a_work = (max_process_a - min_process_a) * ((rand() % 100) / 100.0) + min_process_a;
  401.     //(*cnt_a_work)++;
  402.     double time_b_work = 0;
  403.     int a_is_working = 1;
  404.     int b_is_working = 0;
  405.     while(cnt_b_out != elements_exit)
  406.     {
  407.         if (a_is_working)  // A working
  408.         {
  409.             if (b_is_working)  // Both apparats are working right now
  410.             {
  411.                 double time_min = MIN(time_a_work, time_b_work);
  412.                 time_a_work -= time_min;
  413.                 time_b_work -= time_min;
  414.                 elements_list_wait(list_descriptor_a, list_descriptor_b, time_min);
  415.                 (*time_a) += time_min;
  416.                 (*time_b) += time_min;
  417.                 if (time_a_work < EPS)
  418.                 {
  419.                     a_is_working = 0;
  420.                     if(((rand() % 100) / 100.0) > p)
  421.                     {
  422.                         (*append_in_b)++;
  423.                         list_descriptor_b = list_push(list_descriptor_b, list_pop(&list_descriptor_a));
  424.                         //vector_push(&vector_descriptor_b, vector_pop(&vector_descriptor_a));
  425.                     }
  426.                     else
  427.                     {
  428.                         //vector_pop(&vector_descriptor_a);
  429.                         //vector_push(&vector_descriptor_a);
  430.                         list_descriptor_a = list_push(list_descriptor_a, list_pop(&list_descriptor_a));
  431.                         //vector_push(&vector_descriptor_a, vector_pop(&vector_descriptor_a));
  432.                     }
  433.                 }
  434.  
  435.                 if (time_b_work < EPS)
  436.                 {
  437.                     cnt_b_out++;
  438.                     b_is_working = 0;
  439.                     list_descriptor_a = list_push(list_descriptor_a, list_pop(&list_descriptor_b));
  440.                     //vector_pop(&vector_descriptor_b);
  441.                     //vector_push(&vector_descriptor_a);
  442.                     //vector_push(&vector_descriptor_a, vector_pop(&vector_descriptor_b));
  443.                 }
  444.  
  445.  
  446.                 if ((time_a_work < EPS) && (list_descriptor_a != NULL))
  447.                 {
  448.                     (*cnt_a_work)++;
  449.                     a_is_working= 1;
  450.                     time_a_work = (max_process_a - min_process_a) * ((rand() % 100) / 100.0) + min_process_a;
  451.                 }
  452.  
  453.                 if ((time_b_work < EPS) && (list_descriptor_b != NULL))
  454.                 {
  455.                     b_is_working = 1;
  456.                     time_b_work = (max_process_b - min_process_b) * ((rand() % 100) / 100.0) + min_process_b;
  457.                 }
  458.             }
  459.             else  // B is NOT working right now
  460.             {
  461.                 (*time_a) += time_a_work;
  462.                 (*time_b_waiting) += time_a_work;
  463.                 elements_list_wait(list_descriptor_a, list_descriptor_b, time_a_work);
  464.                 //if (vector_descriptor_b.p_in != vector_descriptor_b.p_out || vector_descriptor_b.is_full)
  465.                   //  (*vector_descriptor_b.p_out) += time_a_work;
  466.  
  467.                 time_a_work = 0;
  468.                 if (time_a_work < EPS)
  469.                 {
  470.                     a_is_working= 0;
  471.                     if(((rand() % 100) / 100.0) > p)
  472.                     {
  473.                         (*append_in_b)++;
  474.                         list_descriptor_b = list_push(list_descriptor_b, list_pop(&list_descriptor_a));
  475.                         //vector_pop(&vector_descriptor_a);
  476.                         //vector_push(&vector_descriptor_b);
  477.                         //vector_push(&vector_descriptor_b, vector_pop(&vector_descriptor_a));
  478.                     }
  479.                     else
  480.                     {
  481.                         list_descriptor_a = list_push(list_descriptor_a, list_pop(&list_descriptor_a));
  482.                         //vector_pop(&vector_descriptor_a);
  483.                         //vector_push(&vector_descriptor_a);
  484.                         //vector_push(&vector_descriptor_a, vector_pop(&vector_descriptor_a));
  485.                     }
  486.                 }
  487.  
  488.                 if ((time_a_work < EPS) && (list_descriptor_a != NULL))
  489.                 {
  490.                     (*cnt_a_work)++;
  491.                     a_is_working= 1;
  492.                     time_a_work = (max_process_a - min_process_a) * ((rand() % 100) / 100.0) + min_process_a;
  493.                 }
  494.  
  495.                 if ((time_b_work < EPS) && (list_descriptor_b != NULL))
  496.                 {
  497.                     b_is_working = 1;
  498.                     time_b_work = (max_process_b - min_process_b) * ((rand() % 100) / 100.0) + min_process_b;
  499.                 }
  500.  
  501.             }
  502.         }
  503.         else // A don`t work
  504.         {
  505.             if (b_is_working)  // ONLY B works right now
  506.             {
  507.                 (*time_b) += time_b_work;
  508.                 (*time_a_waiting) += time_b_work;
  509.                 elements_list_wait(list_descriptor_a, list_descriptor_b, time_a_work);
  510.                 //elements_wait(vector_descriptor_a, vector_descriptor_b, time_b_work);
  511.                 //if (vector_descriptor_a.p_in != vector_descriptor_a.p_out || vector_descriptor_a.is_full)
  512.                   //  (*vector_descriptor_a.p_out) += time_b_work;
  513.  
  514.                 time_b_work = 0;
  515.                 if (time_b_work < EPS)
  516.                 {
  517.                     cnt_b_out++;
  518.                     b_is_working = 0;
  519.                     list_descriptor_a = list_push(list_descriptor_a, list_pop(&list_descriptor_b));
  520.                     //vector_pop(&vector_descriptor_b);
  521.                     //vector_push(&vector_descriptor_a);
  522.                     //vector_push(&vector_descriptor_a, vector_pop(&vector_descriptor_b));
  523.                 }
  524.  
  525.  
  526.                 if ((time_a_work < EPS) && (list_descriptor_a != NULL))
  527.                 {
  528.                     (*cnt_a_work)++;
  529.                     a_is_working= 1;
  530.                     time_a_work = (max_process_a - min_process_a) * ((rand() % 100) / 100.0) + min_process_a;
  531.                 }
  532.  
  533.                 if ((time_b_work < EPS) && (list_descriptor_b != NULL))
  534.                 {
  535.                     b_is_working = 1;
  536.                     time_b_work = (max_process_b - min_process_b) * ((rand() % 100) / 100.0) + min_process_b;
  537.                 }
  538.             }
  539.             else // NOTHING is working at the moment
  540.             {
  541.                 if ((time_a_work < EPS) && (list_descriptor_a != NULL))
  542.                 {
  543.                     (*cnt_a_work)++;
  544.                     a_is_working= 1;
  545.                     time_a_work = (max_process_a - min_process_a) * ((rand() % 100) / 100.0) + min_process_a;
  546.                 }
  547.  
  548.                 if ((time_b_work < EPS) && (list_descriptor_b != NULL))
  549.                 {
  550.                     b_is_working = 1;
  551.                     time_b_work = (max_process_b - min_process_b) * ((rand() % 100) / 100.0) + min_process_b;
  552.                 }
  553.             }
  554.         }
  555.  
  556.         if (!(cnt_b_out % 100) && cnt_b_out != 0 && is_print)
  557.         {
  558.             printf("Current length of queue A: %li\n", queue_list_len(list_descriptor_a));
  559.             printf("Current length of queue B: %li\n", queue_list_len(list_descriptor_b));
  560.             double sum_time = 0;
  561.             for (list_descriptor_t *ptr = list_descriptor_a; ptr != NULL; ptr = ptr -> next)
  562.                 sum_time += ptr -> val;
  563.  
  564.             for (list_descriptor_t *ptr = list_descriptor_b; ptr != NULL; ptr = ptr -> next)
  565.                 sum_time += ptr -> val;
  566.  
  567.             if (sum_time / (queue_size) > EPS)
  568.             printf("Average waiting time %lf\n",
  569.                    sum_time / (queue_size));
  570.             else
  571.                 printf("Elements almost don`t stand in queue\n");
  572.             printf("-----------------------------------------------\n");
  573.         }
  574.     }
  575.  
  576.     for (list_descriptor_t *ptr = list_descriptor_a; ptr != NULL; ptr = ptr -> next)
  577.         (*sum_time_wait) += ptr -> val;
  578.  
  579.     for (list_descriptor_t *ptr = list_descriptor_b; ptr != NULL; ptr = ptr -> next)
  580.         (*sum_time_wait) += ptr -> val;
  581.  
  582.     return;
  583. }
  584.  
  585. list_descriptor_t *full_list(list_descriptor_t *queue_a, long int queue_size)
  586. {
  587.     queue_a = malloc(sizeof(list_descriptor_t));
  588.     if (queue_a == NULL)
  589.         return NULL;
  590.  
  591.     queue_a -> val = 0;
  592.     queue_a -> next = NULL;
  593.  
  594.     list_descriptor_t *ptr = queue_a;
  595.     for (int i = 1; i < queue_size; i++)
  596.     {
  597.         ptr -> next = malloc(sizeof(list_descriptor_t));
  598.         if ((ptr -> next) == NULL)
  599.             return NULL;
  600.  
  601.         (ptr -> next) -> val = 0;
  602.         (ptr -> next) -> next = NULL;
  603.         ptr = (ptr -> next);
  604.         ptr -> val = 0;
  605.     }
  606.  
  607.     return queue_a;
  608. }
  609.  
  610. void print_list(list_descriptor_t *queue_a)
  611. {
  612.     int i = 0;
  613.     while (queue_a != NULL)
  614.     {
  615.         i++;
  616.         printf("%lf", queue_a -> val);
  617.         queue_a  = queue_a -> next;
  618.  
  619.     }
  620.     printf("AMOUNT IS %i", i);
  621. }
  622.  
  623. int main(void)
  624. {
  625.     srand(time(NULL));
  626.     //int choice;
  627.  
  628.     float min_process_a = 0;
  629.     float max_process_a = 6;
  630.     float min_process_b = 1;
  631.     float max_process_b = 8;
  632.     long int queue_size = 100;
  633.     long int elements_exit = 1000;
  634.     double p = 0.7;
  635.     char s[16];
  636.     s[0] = '\0';
  637.  
  638.     while (TRUE)
  639.     {
  640.         printf("\n");
  641.         printf(" _ __ ___   ___ _ __  _   _ \n");
  642.         printf("| '_ ` _ \\ / _ \\ '_ \\| | | |\n");
  643.         printf("| | | | | |  __/ | | | |_| |\n");
  644.         printf("|_| |_| |_|\\___|_| |_|\\____|\n");
  645.  
  646.         printf("Initial parameters:\n");
  647.         printf("Minimum process time for first machine %f\n", min_process_a);
  648.         printf("Maximum process time for first machine %f\n", max_process_a);
  649.         printf("Minimum process time for second machine %f\n", min_process_b);
  650.         printf("Maximum process time for second machine %f\n", max_process_b);
  651.         printf("Amount of elements in first queue %li\n", queue_size);
  652.         printf("Amount of elements that the second machine needs to complete the process %li\n", elements_exit);
  653.         printf("P %lf\n", p);
  654.         printf("\n1: Imitate queue on vector");
  655.         printf("\n2: Imitate queue on list");
  656.         printf("\n3: Change time work for first machine");
  657.         printf("\n4: Change time work for second machine");
  658.         printf("\n5: Change amount of elements in queue");
  659.         printf("\n6: Change amount of elements that the second machine needs to complete the process");
  660.         printf("\n7: Change p");
  661.         printf("\n0: EXIT\n");
  662.         printf("Choice: ");
  663.         fgets(s, 15, stdin);
  664.         if (strcmp(s, "0\n") == 0)
  665.             break;
  666.  
  667.  
  668.         if (strcmp(s, "1\n") == 0)
  669.         {
  670.             double wait_b_queue_predict = (max_process_a + min_process_a) / 2;
  671.             wait_b_queue_predict *= 1000;
  672.             wait_b_queue_predict /= ((1 - p) * 1000);
  673.             wait_b_queue_predict *= elements_exit;
  674.  
  675.             // check in phys notebook
  676.             //double work_a_queue_predict = (max_process_a + min_process_a) / 2;
  677.  
  678.             //double wait_b_work_predict = elements_exit * (max_process_b + min_process_b) / 2;
  679.             double *waiting_time;
  680.             waiting_time = calloc(queue_size, sizeof(double));
  681.             vector_descriptor_t queue_a;
  682.             queue_a.p_in = waiting_time;
  683.             queue_a.p_out = waiting_time;
  684.             queue_a.low_border = waiting_time;
  685.             queue_a.high_border = &(waiting_time[queue_size - 1]);
  686.             queue_a.is_full = 1;
  687.             queue_a.is_working = 0;
  688.  
  689.             vector_descriptor_t queue_b;
  690.             queue_b.p_in = waiting_time;
  691.             queue_b.p_out = waiting_time;
  692.             queue_b.low_border = waiting_time;
  693.             queue_b.high_border = &(waiting_time[queue_size - 1]);
  694.             queue_b.is_full = 0;
  695.             queue_b.is_working = 0;
  696.  
  697.             double time_a = 0;
  698.             double time_b = 0;
  699.             double time_b_waiting = 0;
  700.             double time_a_waiting = 0;
  701.             size_t cnt_a_work = 0;
  702.             size_t append_in_b = 0;
  703.  
  704.             vector_imitate_queue(queue_a, queue_b,
  705.                                  min_process_a, max_process_a,
  706.                                  min_process_b, max_process_b,
  707.                                  &time_a, &time_b, &time_b_waiting,
  708.                                  &time_a_waiting, &cnt_a_work, p, elements_exit, &append_in_b, 1);
  709.  
  710.             double sum_time_wait = 0;
  711.             for (int i = 0; i < queue_size; i++)
  712.                 sum_time_wait += waiting_time[i];
  713.  
  714.             printf("Apparat a worked for %lf (t.u.)\n", time_a);
  715.             if (fabs(time_a_waiting) > EPS)
  716.                 printf("Apparat a waited for %lf (t.u.)\n", time_a_waiting);
  717.             else
  718.                 printf("Apparat a worked all time\n");
  719.  
  720.             printf("Apparat b worked for %lf (t.u.)\n", time_b);
  721.             if (fabs(time_b_waiting) > EPS)
  722.                 printf("Apparat b waited for %lf (t.u.)\n", time_b_waiting);
  723.             else
  724.                 printf("Apparat b worked all time\n");
  725.  
  726.             printf("Apparat a worked %li times\n", (long int) cnt_a_work);
  727.             printf("Average waiting time %lf\n\n", sum_time_wait / queue_size);
  728.  
  729.             wait_b_queue_predict = (max_process_a + min_process_a) / 2;
  730.             wait_b_queue_predict /= (1 - p);
  731.  
  732.             double model_time_predict;
  733.             if (wait_b_queue_predict > (max_process_b + min_process_b) / 2)
  734.                 model_time_predict = wait_b_queue_predict;
  735.             else
  736.                 model_time_predict = (max_process_b + min_process_b) / 2;
  737.  
  738.             double wait_a_queue_predict = (max_process_a + min_process_a) / 2;
  739.             wait_a_queue_predict /= p;
  740.             wait_a_queue_predict -= (max_process_a + min_process_a) / 2;
  741.             double tmp_wait_a = (max_process_a + min_process_a) / 2;
  742.             tmp_wait_a /= (1 - p);
  743.             tmp_wait_a += (max_process_b + min_process_b) / 2;
  744.  
  745.             //printf("\n\nWAITING ROOM %lf %lf\n\n", wait_a_queue_predict, tmp_wait_a);
  746.  
  747.             wait_a_queue_predict = wait_a_queue_predict < tmp_wait_a ? wait_a_queue_predict : tmp_wait_a;
  748.             //wait_a_queue_predict *= queue_size;
  749.             //wait_a_queue_predict -= queue_size * (max_process_a + min_process_a) / 2;
  750.  
  751.  
  752.             double work_a_queue_predict = (max_process_a + min_process_a) / 2;
  753.  
  754.             //printf("\n\nPREDICT WAIT %lf\n\n", wait_a_queue_predict);
  755.             //printf("\n\nPREDICT WORK %lf\n\n", work_a_queue_predict);
  756.  
  757.             double simulate_a_predict = MAX(wait_a_queue_predict, work_a_queue_predict);
  758.             printf("\n\nPREDICT SIMULATE A %lf\n\n", simulate_a_predict);
  759.  
  760.             double elems_check = (time_a + time_a_waiting) / model_time_predict;
  761.             printf("Predicted amount of elements that entered in second queue: %lf\n", elems_check);
  762.             printf("Simulated amount of elements that entered in second queue %li\n", (long int) append_in_b);
  763.             printf("infelicity is: %lf %%\n\n", ((MAX(elems_check, append_in_b) - MIN(elems_check, append_in_b))
  764.                                           / MIN(elems_check, append_in_b) * 100));
  765.  
  766.             printf("Second apparat worked 1000 times, and waited %lf t.u.\n", time_b_waiting);
  767.             double time_b_pred = elements_exit * (max_process_b + min_process_b) / 2 + time_b_waiting;
  768.             printf("Work time must be %lf\n", time_b_pred);
  769.             printf("But simulation time work is %lf\n", time_b + time_b_waiting);
  770.             printf("infelicity is: %lf %%\n\n", ((MAX(time_b_pred, time_b + time_b_waiting)
  771.                                                   - MIN(time_b_pred, time_b + time_b_waiting))
  772.                                           / MIN(time_b_pred, time_b + time_b_waiting) * 100));
  773.  
  774.  
  775.             free(waiting_time);
  776.             waiting_time = NULL;
  777.             system("pause");
  778.  
  779.         }
  780.  
  781.         if (strcmp(s, "2\n") == 0)
  782.         {
  783.             double wait_b_queue_predict = (max_process_a + min_process_a) / 2;
  784.             wait_b_queue_predict *= 1000;
  785.             wait_b_queue_predict /= ((1 - p) * 1000);
  786.             wait_b_queue_predict *= elements_exit;
  787.  
  788.             //double wait_b_work_predict = elements_exit * (max_process_b + min_process_b) / 2;
  789.  
  790.             list_descriptor_t *queue_a = NULL;
  791.  
  792.             queue_a = full_list(queue_a, queue_size);
  793.             //print_list(queue_a);
  794.  
  795.             list_descriptor_t *queue_b = NULL;
  796.  
  797.             double time_a = 0;
  798.             double time_b = 0;
  799.             double time_b_waiting = 0;
  800.             double time_a_waiting = 0;
  801.             size_t cnt_a_work = 0;
  802.             size_t append_in_b = 0;
  803.             double sum_time_wait = 0;
  804.  
  805.             list_imitate_queue(queue_a, queue_b,
  806.                                  min_process_a, max_process_a,
  807.                                  min_process_b, max_process_b,
  808.                                  &time_a, &time_b, &time_b_waiting,
  809.                                  &time_a_waiting, &cnt_a_work, p, elements_exit, &append_in_b, queue_size,
  810.                                  &sum_time_wait, 1);
  811.  
  812.  
  813.  
  814.  
  815.             printf("Apparat a worked for %lf (t.u.)\n", time_a);
  816.             if (fabs(time_a_waiting) > EPS)
  817.                 printf("Apparat a waited for %lf (t.u.)\n", time_a_waiting);
  818.             else
  819.                 printf("Apparat a worked all time\n");
  820.  
  821.             printf("Apparat b worked for %lf (t.u.)\n", time_b);
  822.             if (fabs(time_b_waiting) > EPS)
  823.                 printf("Apparat b waited for %lf (t.u.)\n", time_b_waiting);
  824.             else
  825.                 printf("Apparat b worked all time\n");
  826.  
  827.             printf("Apparat a worked %li times\n", (long int) cnt_a_work);
  828.             printf("Average waiting time %lf\n\n", sum_time_wait / queue_size);
  829.  
  830.             wait_b_queue_predict = (max_process_a + min_process_a) / 2;
  831.             wait_b_queue_predict /= (1 - p);
  832.  
  833.             double model_time_predict;
  834.             if (wait_b_queue_predict > (max_process_b + min_process_b) / 2)
  835.                 model_time_predict = wait_b_queue_predict;
  836.             else
  837.                 model_time_predict = (max_process_b + min_process_b) / 2;
  838.  
  839.  
  840.             double elems_check = (time_a + time_a_waiting) / model_time_predict;
  841.             printf("Predicted amount of elements that entered in second queue: %lf\n", elems_check);
  842.             printf("Simulated amount of elements that entered in second queue %li\n", (long int) append_in_b);
  843.             printf("infelicity is: %lf %%\n\n", ((MAX(elems_check, append_in_b) - MIN(elems_check, append_in_b))
  844.                                           / MIN(elems_check, append_in_b) * 100));
  845.  
  846.             printf("Second apparat worked 1000 times, and waited %lf t.u.\n", time_b_waiting);
  847.             double time_b_pred = elements_exit * (max_process_b + min_process_b) / 2 + time_b_waiting;
  848.             printf("Work time must be %lf\n", time_b_pred);
  849.             printf("But simulation time work is %lf\n", time_b + time_b_waiting);
  850.             printf("infelicity is: %lf %%\n\n", ((MAX(time_b_pred, time_b + time_b_waiting)
  851.                                                   - MIN(time_b_pred, time_b + time_b_waiting))
  852.                                           / MIN(time_b_pred, time_b + time_b_waiting) * 100));
  853.  
  854.  
  855.             for (list_descriptor_t *ptr = queue_a; ptr != NULL;)
  856.             {
  857.                 list_descriptor_t *next = ptr -> next;
  858.                 free(ptr);
  859.                 ptr = NULL;
  860.                 ptr = next;
  861.             }
  862.  
  863.             system("pause");
  864.  
  865.         }
  866.  
  867.         if (strcmp(s, "3\n") == 0)
  868.         {
  869.             double tmp;
  870.             printf("Please, input minimum process time for first machine: ");
  871.             if (scanf("%lf", &tmp) != 1 || tmp < 0)
  872.             {
  873.                 printf("Wrong time!\n");
  874.                 system("pause");
  875.                 break;
  876.             }
  877.             min_process_a = tmp;
  878.  
  879.             printf("Please, input maximum process time for first machine: ");
  880.             if (scanf("%lf", &tmp) != 1 || tmp < 0 || tmp < min_process_a)
  881.             {
  882.                 printf("Wrong time!\n");
  883.                 system("pause");
  884.                 continue;
  885.             }
  886.             max_process_a = tmp;
  887.         }
  888.  
  889.  
  890.         if (strcmp(s, "4\n") == 0)
  891.         {
  892.             double tmp;
  893.             printf("Please, input minimum process time for first machine: ");
  894.             if (scanf("%lf", &tmp) != 1 || tmp < 0)
  895.             {
  896.                 printf("Wrong time!\n");
  897.                 system("pause");
  898.                 continue;
  899.             }
  900.             min_process_b = tmp;
  901.  
  902.             printf("Please, input maximum process time for first machine: ");
  903.             if (scanf("%lf", &tmp) != 1 || tmp < 0 || tmp < min_process_b)
  904.             {
  905.                 printf("Wrong time!\n");
  906.                 system("pause");
  907.                 continue;
  908.             }
  909.             max_process_b = tmp;
  910.         }
  911.  
  912.         if (strcmp(s, "5\n") == 0)
  913.         {
  914.             long int tmp;
  915.             printf("Please, input amount of elements in first queue: ");
  916.             if (scanf("%li", &tmp) != 1 || tmp <= 0)
  917.             {
  918.                 printf("Wrong amount!\n");
  919.                 system("pause");
  920.                 continue;
  921.             }
  922.  
  923.             queue_size = tmp;
  924.         }
  925.  
  926.         if (strcmp(s, "6\n") == 0)
  927.         {
  928.             long int tmp;
  929.             printf("Please, input amount of elements that the second machine needs to complete the process: ");
  930.             if (scanf("%li", &tmp) != 1 || tmp <= 0)
  931.             {
  932.                 printf("Wrong amount!\n");
  933.                 system("pause");
  934.                 continue;
  935.             }
  936.  
  937.             elements_exit = tmp;
  938.         }
  939.  
  940.         if (strcmp(s, "7\n") == 0)
  941.         {
  942.             double tmp;
  943.             printf("Please, input new P: ");
  944.             if (scanf("%lf", &tmp) != 1 || tmp <= 0 || tmp >= 1)
  945.             {
  946.                 printf("Wrong P!\n");
  947.                 system("pause");
  948.                 continue;
  949.             }
  950.  
  951.             p = tmp;
  952.         }
  953.  
  954.         if (strcmp(s, "8\n") == 0)
  955.         {
  956.             clock_t time_list = clock();
  957.             for(int i = 0; i < 100; i++)
  958.             {
  959.                 double wait_b_queue_predict = (max_process_a + min_process_a) / 2;
  960.                 wait_b_queue_predict *= 1000;
  961.                 wait_b_queue_predict /= ((1 - p) * 1000);
  962.                 wait_b_queue_predict *= elements_exit;
  963.  
  964.                 //double wait_b_work_predict = elements_exit * (max_process_b + min_process_b) / 2;
  965.  
  966.                 list_descriptor_t *queue_a = NULL;
  967.  
  968.                 queue_a = full_list(queue_a, queue_size);
  969.                 //print_list(queue_a);
  970.  
  971.                 list_descriptor_t *queue_b = NULL;
  972.  
  973.                 double time_a = 0;
  974.                 double time_b = 0;
  975.                 double time_b_waiting = 0;
  976.                 double time_a_waiting = 0;
  977.                 size_t cnt_a_work = 0;
  978.                 size_t append_in_b = 0;
  979.                 double sum_time_wait = 0;
  980.  
  981.                 list_imitate_queue(queue_a, queue_b,
  982.                                      min_process_a, max_process_a,
  983.                                      min_process_b, max_process_b,
  984.                                      &time_a, &time_b, &time_b_waiting,
  985.                                      &time_a_waiting, &cnt_a_work, p, elements_exit, &append_in_b, queue_size,
  986.                                      &sum_time_wait, 0);
  987.  
  988.  
  989.  
  990.  
  991.  
  992. /*
  993.                // printf("Apparat a worked %li times\n", (long int) cnt_a_work);
  994.              //   printf("Average waiting time %lf\n\n", sum_time_wait / queue_size);
  995.  
  996.                 wait_b_queue_predict = (max_process_a + min_process_a) / 2;
  997.                 wait_b_queue_predict /= (1 - p);
  998.  
  999.               //  double model_time_predict;
  1000.                 //if (wait_b_queue_predict > (max_process_b + min_process_b) / 2)
  1001.                     model_time_predict = wait_b_queue_predict;
  1002.                 else
  1003.                     model_time_predict = (max_process_b + min_process_b) / 2;
  1004.  
  1005.  
  1006.                 //double elems_check = (time_a + time_a_waiting) / model_time_predict;
  1007.                // printf("Predicted amount of elements that entered in second queue: %lf\n", elems_check);
  1008.                 //printf("Simulated amount of elements that entered in second queue %li\n", (long int) append_in_b);
  1009.                 //printf("infelicity is: %lf %%\n\n", ((MAX(elems_check, append_in_b) - MIN(elems_check, append_in_b))
  1010. //                                              / MIN(elems_check, append_in_b) * 100));
  1011.  
  1012.   //              printf("Second apparat worked 1000 times, and waited %lf t.u.\n", time_b_waiting);
  1013.                 double time_b_pred = elements_exit * (max_process_b + min_process_b) / 2 + time_b_waiting;
  1014.     //            printf("Work time must be %lf\n", time_b_pred);
  1015.       //          printf("But simulation time work is %lf\n", time_b + time_b_waiting);
  1016.         //        printf("infelicity is: %lf %%\n\n", ((MAX(time_b_pred, time_b + time_b_waiting)
  1017.                                                       - MIN(time_b_pred, time_b + time_b_waiting))
  1018.                                               / MIN(time_b_pred, time_b + time_b_waiting) * 100));
  1019.  
  1020. */
  1021.                 for (list_descriptor_t *ptr = queue_a; ptr != NULL;)
  1022.                 {
  1023.                     list_descriptor_t *next = ptr -> next;
  1024.                     free(ptr);
  1025.                     ptr = NULL;
  1026.                     ptr = next;
  1027.                 }
  1028.             }
  1029.             time_list = clock() - time_list;
  1030.  
  1031.             clock_t time_vector = clock();
  1032.             for(int i = 0; i < 100; i++)
  1033.             {
  1034.                 double wait_b_queue_predict = (max_process_a + min_process_a) / 2;
  1035.                 wait_b_queue_predict *= 1000;
  1036.                 wait_b_queue_predict /= ((1 - p) * 1000);
  1037.                 wait_b_queue_predict *= elements_exit;
  1038.  
  1039.                 //double wait_b_work_predict = elements_exit * (max_process_b + min_process_b) / 2;
  1040.                 double *waiting_time;
  1041.                 waiting_time = calloc(queue_size, sizeof(double));
  1042.                 vector_descriptor_t queue_a;
  1043.                 queue_a.p_in = waiting_time;
  1044.                 queue_a.p_out = waiting_time;
  1045.                 queue_a.low_border = waiting_time;
  1046.                 queue_a.high_border = &(waiting_time[queue_size - 1]);
  1047.                 queue_a.is_full = 1;
  1048.                 queue_a.is_working = 0;
  1049.  
  1050.                 vector_descriptor_t queue_b;
  1051.                 queue_b.p_in = waiting_time;
  1052.                 queue_b.p_out = waiting_time;
  1053.                 queue_b.low_border = waiting_time;
  1054.                 queue_b.high_border = &(waiting_time[queue_size - 1]);
  1055.                 queue_b.is_full = 0;
  1056.                 queue_b.is_working = 0;
  1057.  
  1058.                 double time_a = 0;
  1059.                 double time_b = 0;
  1060.                 double time_b_waiting = 0;
  1061.                 double time_a_waiting = 0;
  1062.                 size_t cnt_a_work = 0;
  1063.                 size_t append_in_b = 0;
  1064.  
  1065.                 vector_imitate_queue(queue_a, queue_b,
  1066.                                      min_process_a, max_process_a,
  1067.                                      min_process_b, max_process_b,
  1068.                                      &time_a, &time_b, &time_b_waiting,
  1069.                                      &time_a_waiting, &cnt_a_work, p, elements_exit, &append_in_b, 0);
  1070.  
  1071.                 /*double sum_time_wait = 0;
  1072.                 for (int i = 0; i < queue_size; i++)
  1073.                     sum_time_wait += waiting_time[i];
  1074.  
  1075.               //  printf("Apparat a worked for %lf (t.u.)\n", time_a);
  1076.                 if (fabs(time_a_waiting) > EPS)
  1077.                     //printf("Apparat a waited for %lf (t.u.)\n", time_a_waiting);
  1078.                 else
  1079.                     //printf("Apparat a worked all time\n");
  1080.  
  1081.                 //printf("Apparat b worked for %lf (t.u.)\n", time_b);
  1082.                 if (fabs(time_b_waiting) > EPS)
  1083.                   //  printf("Apparat b waited for %lf (t.u.)\n", time_b_waiting);
  1084.                 else
  1085.                     //printf("Apparat b worked all time\n");
  1086.  
  1087. //                printf("Apparat a worked %li times\n", (long int) cnt_a_work);
  1088.   //              printf("Average waiting time %lf\n\n", sum_time_wait / queue_size);
  1089.  
  1090.                 wait_b_queue_predict = (max_process_a + min_process_a) / 2;
  1091.                 wait_b_queue_predict /= (1 - p);
  1092.  
  1093.                 double model_time_predict;
  1094.                 if (wait_b_queue_predict > (max_process_b + min_process_b) / 2)
  1095.                     model_time_predict = wait_b_queue_predict;
  1096.                 else
  1097.                     model_time_predict = (max_process_b + min_process_b) / 2;
  1098.  
  1099.  
  1100.                 double elems_check = (time_a + time_a_waiting) / model_time_predict;
  1101.                 printf("Predicted amount of elements that entered in second queue: %lf\n", elems_check);
  1102.                 printf("Simulated amount of elements that entered in second queue %li\n", (long int) append_in_b);
  1103.                 printf("infelicity is: %lf %%\n\n", ((MAX(elems_check, append_in_b) - MIN(elems_check, append_in_b))
  1104.                                               / MIN(elems_check, append_in_b) * 100));
  1105.  
  1106.                 printf("Second apparat worked 1000 times, and waited %lf t.u.\n", time_b_waiting);
  1107.                 double time_b_pred = elements_exit * (max_process_b + min_process_b) / 2 + time_b_waiting;
  1108.                 printf("Work time must be %lf\n", time_b_pred);
  1109.                 printf("But simulation time work is %lf\n", time_b + time_b_waiting);
  1110.                 printf("infelicity is: %lf %%\n\n", ((MAX(time_b_pred, time_b + time_b_waiting)
  1111.                                                       - MIN(time_b_pred, time_b + time_b_waiting))
  1112.                                               / MIN(time_b_pred, time_b + time_b_waiting) * 100));
  1113.  
  1114. */
  1115.                 free(waiting_time);
  1116.                 waiting_time = NULL;
  1117.  
  1118.             }
  1119.  
  1120.             time_vector = clock() - time_vector;
  1121.             printf("\n\nTime for vector %li\n\n", (long int) time_vector);
  1122.             printf("\n\nTime for list %li\n\n", (long int) time_list);
  1123.  
  1124.             printf("Precentage winning: %lf %% \n\n", ((double)time_list - time_vector) / time_vector * 100);
  1125.         }
  1126.  
  1127.     }
  1128.     return 0;
  1129. }
  1130.  
  1131. /*
  1132. void vector_push(vector_descriptor_t *vector_descriptor)
  1133. {
  1134.     if (vector_descriptor -> p_in == vector_descriptor -> high_border)
  1135.         vector_descriptor -> p_in = vector_descriptor -> low_border;
  1136.     else
  1137.         (vector_descriptor -> p_in)++;
  1138. }
  1139.  
  1140. int vector_pop(vector_descriptor_t *vector_descriptor)
  1141. {
  1142.     if (vector_descriptor -> p_in == vector_descriptor -> p_out)
  1143.         return FULL_QUEUE;
  1144.     if (vector_descriptor -> p_out == vector_descriptor -> high_border)
  1145.         vector_descriptor -> p_out = vector_descriptor -> low_border;
  1146.     else
  1147.         (vector_descriptor -> p_out)++;
  1148.     return SUCCESS;
  1149. }
  1150.  
  1151.  
  1152. void vector_imitate_queue(vector_descriptor_t vector_descriptor_a,
  1153.                           vector_descriptor_t vector_descriptor_b,
  1154.                           float min_process_a, float max_process_a,
  1155.                           float min_process_b, float max_process_b,
  1156.                           double *time_a, double *time_b, double *time_b_waiting)
  1157. {
  1158.     min_process_a =+ 0.001; // if min_proces == 0 we will have troubles
  1159.     int cnt_b_out = 0;
  1160.  
  1161.     double time_a_work = 0;
  1162.     double time_b_work = 0;
  1163.     time_a_work += (max_process_a - min_process_a) * ((rand() % 100) / 100.0) + min_process_a;
  1164.     while (cnt_b_out != 100)
  1165.     {
  1166.         //printf("%lf ", (double) ((rand() % 100) / 100.0));
  1167.         if (fabs(time_a_work) > EPS)
  1168.         {
  1169.             if (fabs(time_b_work) > EPS)
  1170.             {
  1171.                 double time_min = MIN(time_a_work, time_b_work);
  1172.                 (*time_a) += time_min;
  1173.                 (*time_b) += time_min;
  1174.                 time_a_work -= time_min;
  1175.                 time_b_work -= time_min;
  1176.                 if (fabs(time_a_work) < EPS)
  1177.                 {
  1178.                     if(((rand() % 100)) > 70)
  1179.                     {
  1180.                         vector_pop(&vector_descriptor_a);
  1181.                         vector_push(&vector_descriptor_b);
  1182.                     }
  1183.                     else
  1184.                     {
  1185.                         vector_pop(&vector_descriptor_a);
  1186.                         vector_push(&vector_descriptor_a);
  1187.                     }
  1188.  
  1189.                 //    if (vector_descriptor_a.p_in != vector_descriptor_a.p_out)
  1190.                   //      time_a_work = (max_process_a - min_process_a) * ((rand() % 100) / 100.0) + min_process_a;
  1191.                 }
  1192.                 if (fabs(time_b_work) < EPS)
  1193.                 {
  1194.                     cnt_b_out++;
  1195.                     if (!vector_pop(&vector_descriptor_b))
  1196.                         vector_push(&vector_descriptor_a);
  1197.                 }
  1198.  
  1199.                 if (fabs(time_a_work) < EPS)
  1200.                     if (vector_descriptor_a.p_in != vector_descriptor_a.p_out)
  1201.                         time_a_work = (max_process_a - min_process_a) * ((rand() % 100) / 100.0) + min_process_a;
  1202.  
  1203.                 if (fabs(time_b_work) < EPS)
  1204.                     if (vector_descriptor_b.p_in != vector_descriptor_b.p_out)
  1205.                         time_b_work = (max_process_b - min_process_b) * ((rand() % 100) / 100.0) + min_process_b;
  1206.             }
  1207.             else
  1208.             {
  1209.                 *time_b_waiting += time_a_work;
  1210.                 *time_a += time_a_work;
  1211.                 time_a_work = 0;
  1212.                 if(((rand() % 100)) > 70)
  1213.                 {
  1214.                     vector_pop(&vector_descriptor_a);
  1215.                     vector_push(&vector_descriptor_b);
  1216.                 }
  1217.                 else
  1218.                 {
  1219.                     vector_pop(&vector_descriptor_a);
  1220.                     vector_push(&vector_descriptor_a);
  1221.                 }
  1222.  
  1223.                 if (vector_descriptor_a.p_in != vector_descriptor_a.p_out)
  1224.                     time_a_work = (max_process_a - min_process_a) * ((rand() % 100) / 100.0) + min_process_a;
  1225.                 if (vector_descriptor_b.p_in != vector_descriptor_b.p_out)
  1226.                     time_b_work = (max_process_b - min_process_b) * ((rand() % 100) / 100.0) + min_process_b;
  1227.             }
  1228.         }
  1229.         else
  1230.         {
  1231.             if (fabs(time_b_work) > EPS)
  1232.             {
  1233.                 if (!vector_pop(&vector_descriptor_b))
  1234.                 {
  1235.                     cnt_b_out++;
  1236.                     *time_b += time_b_work;
  1237.                     time_b_work = 0;
  1238.                     vector_push(&vector_descriptor_a);
  1239.                     time_b_work = (max_process_b - min_process_b) * ((rand() % 100) / 100.0) + min_process_b;
  1240.                 }
  1241.             }
  1242.             else
  1243.             {
  1244.                 ;
  1245.             }
  1246.         }
  1247.     }
  1248.  
  1249.     return;
  1250. }
  1251.  
  1252.  
  1253. int main(void)
  1254. {
  1255.     srand(time(NULL));
  1256.     //int choice;
  1257.  
  1258.     float min_process_a = 0;
  1259.     float max_process_a = 6;
  1260.     float min_process_b = 1;
  1261.     float max_process_b = 8;
  1262.     char s[16];
  1263.     s[0] = '\0';
  1264.  
  1265.     while (TRUE)
  1266.     {
  1267.         printf("\n");
  1268.         printf(" _ __ ___   ___ _ __  _   _ \n");
  1269.         printf("| '_ ` _ \\ / _ \\ '_ \\| | | |\n");
  1270.         printf("| | | | | |  __/ | | | |_| |\n");
  1271.         printf("|_| |_| |_|\\___|_| |_|\\____|\n");
  1272.  
  1273.         printf("\n1: Imitate queue on vecotor");
  1274.         printf("\n2: Imitate queue on list");
  1275.         printf("\n0: EXIT\n");
  1276.         printf("Choice: ");
  1277.         fgets(s, 15, stdin);
  1278.         if (strcmp(s, "0\n") == 0)
  1279.             break;
  1280.  
  1281.  
  1282.         if (strcmp(s, "1\n") == 0)
  1283.         {
  1284.             double time_sum_a = 0;
  1285.             double time_sum_b = 0;
  1286.             double time_sum_diff = 0;
  1287.             for(int i = 0; i < 100; i++)
  1288.             {
  1289.                 int queue_a [VECTOR_LEN];
  1290.                 //random_vector(queue_a, VECTOR_LEN);
  1291.                 // tbh this is not necessary, but why not?
  1292.  
  1293.                 vector_descriptor_t vector_descriptor_a;
  1294.                 vector_descriptor_a.p_out = queue_a;
  1295.                 vector_descriptor_a.p_in = &(queue_a[99]);
  1296.                 vector_descriptor_a.low_border = queue_a;
  1297.                 vector_descriptor_a.high_border = &(queue_a[99]);
  1298.  
  1299.                 int queue_b [VECTOR_LEN];
  1300.                 vector_descriptor_t vector_descriptor_b;
  1301.                 vector_descriptor_b.p_out = queue_b;
  1302.                 vector_descriptor_b.p_in = queue_b;
  1303.                 vector_descriptor_b.low_border = queue_b;
  1304.                 vector_descriptor_b.high_border = &(queue_b[99]);
  1305.  
  1306.                 double time_a = 0;
  1307.                 double time_b = 0;
  1308.                 double time_b_waiting = 0;
  1309.  
  1310.                 vector_imitate_queue(vector_descriptor_a, vector_descriptor_b,
  1311.                                      min_process_a, max_process_a,
  1312.                                      min_process_b, max_process_b,
  1313.                                      &time_a, &time_b, &time_b_waiting);
  1314.  
  1315.                 printf("time a = %lf\n", time_a);
  1316.                 printf("time b = %lf\n", time_b);
  1317.                 printf("\apparat b waiting time = %lf\n", time_b_waiting);
  1318.                 time_sum_a += time_a;
  1319.                 time_sum_b += time_b;
  1320.                 time_sum_diff += time_b_waiting;
  1321.             }
  1322.             printf("time a average = %lf\n", time_sum_a / 100.0);
  1323.             printf("time b average = %lf\n", time_sum_b / 100.0);
  1324.             printf("\apparat b waiting time average = %lf\n", time_sum_diff / 100.0);
  1325.         }
  1326.  
  1327.         else if (strcmp(s, "2\n") == 0)
  1328.         {
  1329.             printf("LIST QUEUE");
  1330.         }
  1331.  
  1332.         else
  1333.             printf("\nWrong choice!");
  1334.  
  1335.     }
  1336.  
  1337.     return 0;
  1338. }
  1339. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement