Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 19.77 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 TRUE 1
  9. #define FULL_QUEUE 1
  10. #define SUCCESS 0
  11. #define VECTOR_LEN 100
  12. #define EPS 10E-4
  13.  
  14. //(0 + rand() % (10 - 0));
  15.  
  16. typedef struct
  17. {
  18.     double *p_in;
  19.     double *p_out;
  20.     double *low_border;
  21.     double *high_border;
  22.     int is_full;
  23.     int is_working;
  24. } vector_descriptor_t;
  25.  
  26. // REWORK: PLUSS ALL ELEMENTS, EXCEPT P_OUT; CHECK IF THEY IN MACHINE LATER
  27. void elements_wait(vector_descriptor_t vector_descriptor_a,
  28.                    vector_descriptor_t vector_descriptor_b, double time)  // INCORRECT WORK
  29. {
  30.     for(double *ptr = vector_descriptor_a.low_border; ptr <= vector_descriptor_a.high_border; ptr++)
  31.     {
  32.         if (ptr != vector_descriptor_a.p_out &&
  33.             ptr !=vector_descriptor_b.p_out)
  34.             (*ptr) += time;
  35.     }
  36.     /*
  37.     if (vector_descriptor.is_full)
  38.         for(double *ptr = vector_descriptor.low_border; ptr <= vector_descriptor.high_border; ptr++)
  39.             *ptr += time;
  40.     else
  41.     {
  42.         if (vector_descriptor.p_in < vector_descriptor.p_out)
  43.             for(double *ptr = vector_descriptor.p_in; ptr != vector_descriptor.p_out; ptr++)
  44.             {
  45.                 *ptr += time;
  46.                 if (ptr == vector_descriptor.high_border)
  47.                     ptr = vector_descriptor.low_border - 1;
  48.             }
  49.         else
  50.             for(double *ptr = vector_descriptor.p_out; ptr != vector_descriptor.p_in; ptr++)
  51.             {
  52.                 *ptr += time;
  53.                 if (ptr == vector_descriptor.high_border)
  54.                     ptr = vector_descriptor.low_border - 1;
  55.             }
  56.     }
  57. */
  58.     return;
  59. }
  60.  
  61. double* vector_pop(vector_descriptor_t *vector_descriptor)
  62. {
  63.     double* out = vector_descriptor -> p_out;
  64.     if (vector_descriptor -> p_out == vector_descriptor -> high_border)
  65.         vector_descriptor -> p_out = vector_descriptor -> low_border;
  66.     else
  67.         (vector_descriptor -> p_out) += 1;
  68.  
  69.     if (vector_descriptor -> is_full)
  70.         (vector_descriptor -> is_full) = 0;
  71.  
  72.     return out;
  73. }
  74.  
  75. void vector_push(vector_descriptor_t *vector_descriptor, double* element)
  76. {
  77.     if (!(vector_descriptor -> is_full))
  78.     {
  79.         (vector_descriptor -> p_in) = element;
  80.         if ((vector_descriptor -> p_in) == (vector_descriptor -> high_border))
  81.             vector_descriptor -> p_in = vector_descriptor -> low_border;
  82.         else
  83.             (vector_descriptor -> p_in)++;
  84.     }
  85.  
  86.     if ((vector_descriptor -> p_in) == (vector_descriptor -> p_out))
  87.         (vector_descriptor -> is_full) = 1;
  88.  
  89.     return;
  90. }
  91.  
  92. long int queue_len(vector_descriptor_t vector_descriptor)
  93. {
  94.     if (vector_descriptor.is_full)
  95.         return (vector_descriptor.high_border - vector_descriptor.low_border) + 1;
  96.  
  97.     if (vector_descriptor.p_in == vector_descriptor.p_out)
  98.         return 0;
  99.  
  100.     long int cnt = 0;
  101.     for(double *ptr = vector_descriptor.p_out; ptr != vector_descriptor.p_in; ptr++)
  102.     {
  103.         cnt++;
  104.         if (ptr == vector_descriptor.high_border)
  105.             ptr = vector_descriptor.low_border - 1;
  106.     }
  107.  
  108.     return cnt;
  109. }
  110.  
  111. void vector_imitate_queue(vector_descriptor_t vector_descriptor_a, vector_descriptor_t vector_descriptor_b,
  112.                      double min_process_a, double max_process_a,
  113.                      double min_process_b, double max_process_b,
  114.                      double *time_a, double *time_b, double *time_b_waiting,
  115.                      size_t *cnt_a_work, double p)
  116. {
  117.     int cnt_b_out = 0;
  118.     double time_a_work = (max_process_a - min_process_a) * ((rand() % 100) / 100.0) + min_process_a;
  119.     //(*cnt_a_work)++;
  120.     double time_b_work = 0;
  121.     vector_descriptor_a.is_working = 1;
  122.     while(cnt_b_out != 1000)
  123.     {
  124.         if (vector_descriptor_a.is_working)  // A working
  125.         {
  126.             if (vector_descriptor_b.is_working)  // Both apparats are working right now
  127.             {
  128.                 double time_min = MIN(time_a_work, time_b_work);
  129.                 time_a_work -= time_min;
  130.                 time_b_work -= time_min;
  131.                 elements_wait(vector_descriptor_a, vector_descriptor_b, time_min);
  132.                 (*time_a) += time_min;
  133.                 (*time_b) += time_min;
  134.                 if (time_a_work < EPS)
  135.                 {
  136.                     vector_descriptor_a.is_working = 0;
  137.                     if(((rand() % 100) / 100.0) > p)
  138.                         vector_push(&vector_descriptor_b, vector_pop(&vector_descriptor_a));
  139.                     else
  140.                         vector_push(&vector_descriptor_a, vector_pop(&vector_descriptor_a));
  141.                 }
  142.  
  143.                 if (time_b_work < EPS)
  144.                 {
  145.                     cnt_b_out++;
  146.                     vector_descriptor_b.is_working = 0;
  147.                     vector_push(&vector_descriptor_a, vector_pop(&vector_descriptor_b));
  148.                 }
  149.  
  150.  
  151.                 if ((time_a_work < EPS) && (vector_descriptor_a.p_in != vector_descriptor_a.p_out || vector_descriptor_a.is_full))
  152.                 {
  153.                     (*cnt_a_work)++;
  154.                     vector_descriptor_a.is_working = 1;
  155.                     time_a_work = (max_process_a - min_process_a) * ((rand() % 100) / 100.0) + min_process_a;
  156.                 }
  157.  
  158.                 if ((time_b_work < EPS) && (vector_descriptor_b.p_in != vector_descriptor_b.p_out || vector_descriptor_b.is_full))
  159.                 {
  160.                     vector_descriptor_b.is_working = 1;
  161.                     time_b_work = (max_process_b - min_process_b) * ((rand() % 100) / 100.0) + min_process_b;
  162.                 }
  163.             }
  164.             else  // B is NOT working right now
  165.             {
  166.                 (*time_a) += time_a_work;
  167.                 (*time_b_waiting) += time_a_work;
  168.                 elements_wait(vector_descriptor_a, vector_descriptor_b, time_a_work);
  169.                 if (vector_descriptor_b.p_in != vector_descriptor_b.p_out || vector_descriptor_b.is_full)
  170.                     (*vector_descriptor_b.p_out) += time_a_work;
  171.  
  172.                 time_a_work = 0;
  173.                 if (time_a_work < EPS)
  174.                 {
  175.                     vector_descriptor_a.is_working = 0;
  176.                     if(((rand() % 100) / 100.0) > p)
  177.                         vector_push(&vector_descriptor_b, vector_pop(&vector_descriptor_a));
  178.                     else
  179.                         vector_push(&vector_descriptor_a, vector_pop(&vector_descriptor_a));
  180.                 }
  181.  
  182.                 if ((time_a_work < EPS) && (vector_descriptor_a.p_in != vector_descriptor_a.p_out || vector_descriptor_a.is_full))
  183.                 {
  184.                     (*cnt_a_work)++;
  185.                     vector_descriptor_a.is_working = 1;
  186.                     time_a_work = (max_process_a - min_process_a) * ((rand() % 100) / 100.0) + min_process_a;
  187.                 }
  188.  
  189.                 if ((time_b_work < EPS) && (vector_descriptor_b.p_in != vector_descriptor_b.p_out || vector_descriptor_b.is_full))
  190.                 {
  191.                     vector_descriptor_b.is_working = 1;
  192.                     time_b_work = (max_process_b - min_process_b) * ((rand() % 100) / 100.0) + min_process_b;
  193.                 }
  194.  
  195.             }
  196.         }
  197.         else // A don`t work
  198.         {
  199.             if (vector_descriptor_b.is_working)  // ONLY B works right now
  200.             {
  201.                 (*time_b) += time_b_work;
  202.                 elements_wait(vector_descriptor_a, vector_descriptor_b, time_b_work);
  203.                 if (vector_descriptor_a.p_in != vector_descriptor_a.p_out || vector_descriptor_a.is_full)
  204.                     (*vector_descriptor_a.p_out) += time_b_work;
  205.  
  206.                 time_b_work = 0;
  207.                 if (time_b_work < EPS)
  208.                 {
  209.                     cnt_b_out++;
  210.                     vector_descriptor_b.is_working = 0;
  211.                     vector_push(&vector_descriptor_a, vector_pop(&vector_descriptor_b));
  212.                 }
  213.  
  214.  
  215.                 if ((time_a_work < EPS) && (vector_descriptor_a.p_in != vector_descriptor_a.p_out || vector_descriptor_a.is_full))
  216.                 {
  217.                     (*cnt_a_work)++;
  218.                     vector_descriptor_a.is_working = 1;
  219.                     time_a_work = (max_process_a - min_process_a) * ((rand() % 100) / 100.0) + min_process_a;
  220.                 }
  221.  
  222.                 if ((time_b_work < EPS) && (vector_descriptor_b.p_in != vector_descriptor_b.p_out || vector_descriptor_b.is_full))
  223.                 {
  224.                     vector_descriptor_b.is_working = 1;
  225.                     time_b_work = (max_process_b - min_process_b) * ((rand() % 100) / 100.0) + min_process_b;
  226.                 }
  227.             }
  228.             else // NOTHING is working at the moment
  229.             {
  230.                 if ((time_a_work < EPS) && (vector_descriptor_a.p_in != vector_descriptor_a.p_out || vector_descriptor_a.is_full))
  231.                 {
  232.                     (*cnt_a_work)++;
  233.                     vector_descriptor_a.is_working = 1;
  234.                     time_a_work = (max_process_a - min_process_a) * ((rand() % 100) / 100.0) + min_process_a;
  235.                 }
  236.  
  237.                 if ((time_b_work < EPS) && (vector_descriptor_b.p_in != vector_descriptor_b.p_out || vector_descriptor_b.is_full))
  238.                 {
  239.                     vector_descriptor_b.is_working = 1;
  240.                     time_b_work = (max_process_b - min_process_b) * ((rand() % 100) / 100.0) + min_process_b;
  241.                 }
  242.             }
  243.         }
  244.         if (cnt_b_out == 1)
  245.         {
  246.             printf("Current length of queue A: %li\n", queue_len(vector_descriptor_a));
  247.             printf("Current length of queue B: %li\n", queue_len(vector_descriptor_b));
  248.         }
  249.  
  250.         if (!(cnt_b_out % 100) && cnt_b_out != 0)
  251.         {
  252.             printf("Current length of queue A: %li\n", queue_len(vector_descriptor_a));
  253.             printf("Current length of queue B: %li\n", queue_len(vector_descriptor_b));
  254.         }
  255.     }
  256.  
  257.     return;
  258. }
  259.  
  260.  
  261. int main(void)
  262. {
  263.     srand(time(NULL));
  264.     //int choice;
  265.  
  266.     float min_process_a = 0;
  267.     float max_process_a = 6;
  268.     float min_process_b = 1;
  269.     float max_process_b = 8;
  270.     long int queue_size = 100;
  271.     double p = 0.7;
  272.     char s[16];
  273.     s[0] = '\0';
  274.  
  275.     while (TRUE)
  276.     {
  277.         printf("\n");
  278.         printf(" _ __ ___   ___ _ __  _   _ \n");
  279.         printf("| '_ ` _ \\ / _ \\ '_ \\| | | |\n");
  280.         printf("| | | | | |  __/ | | | |_| |\n");
  281.         printf("|_| |_| |_|\\___|_| |_|\\____|\n");
  282.  
  283.         printf("\n1: Imitate queue on vector");
  284.         printf("\n2: Imitate queue on list");
  285.         printf("\n0: EXIT\n");
  286.         printf("Choice: ");
  287.         fgets(s, 15, stdin);
  288.         if (strcmp(s, "0\n") == 0)
  289.             break;
  290.  
  291.  
  292.         if (strcmp(s, "1\n") == 0)
  293.         {
  294.             double *waiting_time;
  295.             waiting_time = calloc(queue_size, sizeof(double));
  296.             vector_descriptor_t queue_a;
  297.             queue_a.p_in = waiting_time;
  298.             queue_a.p_out = waiting_time;
  299.             queue_a.low_border = waiting_time;
  300.             queue_a.high_border = &(waiting_time[queue_size - 1]);
  301.             queue_a.is_full = 1;
  302.             queue_a.is_working = 0;
  303.  
  304.             vector_descriptor_t queue_b;
  305.             queue_b.p_in = waiting_time;
  306.             queue_b.p_out = waiting_time;
  307.             queue_b.low_border = waiting_time;
  308.             queue_b.high_border = &(waiting_time[queue_size - 1]);
  309.             queue_b.is_full = 0;
  310.             queue_b.is_working = 0;
  311.  
  312.             double time_a = 0;
  313.             double time_b = 0;
  314.             double time_b_waiting = 0;
  315.             size_t cnt_a_work = 0;
  316.  
  317.             vector_imitate_queue(queue_a, queue_b,
  318.                                  min_process_a, max_process_a,
  319.                                  min_process_b, max_process_b,
  320.                                  &time_a, &time_b, &time_b_waiting, &cnt_a_work, p);
  321.  
  322.             double sum_time_wait = 0;
  323.             for (int i = 0; i < queue_size; i++)
  324.                 sum_time_wait += waiting_time[i];
  325.  
  326.             printf("Apparat a worked for %lf (seconds)\n", time_a);
  327.             printf("Apparat b worked for %lf (seconds)\n", time_b);
  328.             printf("Apparat b waited for %lf (seconds)\n", time_b_waiting);
  329.             printf("Apparat a worked %li times\n", (long int) cnt_a_work);
  330.             printf("Average waiting time %lf", sum_time_wait / queue_size);
  331.  
  332.             free(waiting_time);
  333.             waiting_time = NULL;
  334.  
  335.         }
  336.     }
  337.     return 0;
  338. }
  339.  
  340. /*
  341. void vector_push(vector_descriptor_t *vector_descriptor)
  342. {
  343.     if (vector_descriptor -> p_in == vector_descriptor -> high_border)
  344.         vector_descriptor -> p_in = vector_descriptor -> low_border;
  345.     else
  346.         (vector_descriptor -> p_in)++;
  347. }
  348.  
  349. int vector_pop(vector_descriptor_t *vector_descriptor)
  350. {
  351.     if (vector_descriptor -> p_in == vector_descriptor -> p_out)
  352.         return FULL_QUEUE;
  353.     if (vector_descriptor -> p_out == vector_descriptor -> high_border)
  354.         vector_descriptor -> p_out = vector_descriptor -> low_border;
  355.     else
  356.         (vector_descriptor -> p_out)++;
  357.     return SUCCESS;
  358. }
  359.  
  360.  
  361. void vector_imitate_queue(vector_descriptor_t vector_descriptor_a,
  362.                           vector_descriptor_t vector_descriptor_b,
  363.                           float min_process_a, float max_process_a,
  364.                           float min_process_b, float max_process_b,
  365.                           double *time_a, double *time_b, double *time_b_waiting)
  366. {
  367.     min_process_a =+ 0.001; // if min_proces == 0 we will have troubles
  368.     int cnt_b_out = 0;
  369.  
  370.     double time_a_work = 0;
  371.     double time_b_work = 0;
  372.     time_a_work += (max_process_a - min_process_a) * ((rand() % 100) / 100.0) + min_process_a;
  373.     while (cnt_b_out != 100)
  374.     {
  375.         //printf("%lf ", (double) ((rand() % 100) / 100.0));
  376.         if (fabs(time_a_work) > EPS)
  377.         {
  378.             if (fabs(time_b_work) > EPS)
  379.             {
  380.                 double time_min = MIN(time_a_work, time_b_work);
  381.                 (*time_a) += time_min;
  382.                 (*time_b) += time_min;
  383.                 time_a_work -= time_min;
  384.                 time_b_work -= time_min;
  385.                 if (fabs(time_a_work) < EPS)
  386.                 {
  387.                     if(((rand() % 100)) > 70)
  388.                     {
  389.                         vector_pop(&vector_descriptor_a);
  390.                         vector_push(&vector_descriptor_b);
  391.                     }
  392.                     else
  393.                     {
  394.                         vector_pop(&vector_descriptor_a);
  395.                         vector_push(&vector_descriptor_a);
  396.                     }
  397.  
  398.                 //    if (vector_descriptor_a.p_in != vector_descriptor_a.p_out)
  399.                   //      time_a_work = (max_process_a - min_process_a) * ((rand() % 100) / 100.0) + min_process_a;
  400.                 }
  401.                 if (fabs(time_b_work) < EPS)
  402.                 {
  403.                     cnt_b_out++;
  404.                     if (!vector_pop(&vector_descriptor_b))
  405.                         vector_push(&vector_descriptor_a);
  406.                 }
  407.  
  408.                 if (fabs(time_a_work) < EPS)
  409.                     if (vector_descriptor_a.p_in != vector_descriptor_a.p_out)
  410.                         time_a_work = (max_process_a - min_process_a) * ((rand() % 100) / 100.0) + min_process_a;
  411.  
  412.                 if (fabs(time_b_work) < EPS)
  413.                     if (vector_descriptor_b.p_in != vector_descriptor_b.p_out)
  414.                         time_b_work = (max_process_b - min_process_b) * ((rand() % 100) / 100.0) + min_process_b;
  415.             }
  416.             else
  417.             {
  418.                 *time_b_waiting += time_a_work;
  419.                 *time_a += time_a_work;
  420.                 time_a_work = 0;
  421.                 if(((rand() % 100)) > 70)
  422.                 {
  423.                     vector_pop(&vector_descriptor_a);
  424.                     vector_push(&vector_descriptor_b);
  425.                 }
  426.                 else
  427.                 {
  428.                     vector_pop(&vector_descriptor_a);
  429.                     vector_push(&vector_descriptor_a);
  430.                 }
  431.  
  432.                 if (vector_descriptor_a.p_in != vector_descriptor_a.p_out)
  433.                     time_a_work = (max_process_a - min_process_a) * ((rand() % 100) / 100.0) + min_process_a;
  434.                 if (vector_descriptor_b.p_in != vector_descriptor_b.p_out)
  435.                     time_b_work = (max_process_b - min_process_b) * ((rand() % 100) / 100.0) + min_process_b;
  436.             }
  437.         }
  438.         else
  439.         {
  440.             if (fabs(time_b_work) > EPS)
  441.             {
  442.                 if (!vector_pop(&vector_descriptor_b))
  443.                 {
  444.                     cnt_b_out++;
  445.                     *time_b += time_b_work;
  446.                     time_b_work = 0;
  447.                     vector_push(&vector_descriptor_a);
  448.                     time_b_work = (max_process_b - min_process_b) * ((rand() % 100) / 100.0) + min_process_b;
  449.                 }
  450.             }
  451.             else
  452.             {
  453.                 ;
  454.             }
  455.         }
  456.     }
  457.  
  458.     return;
  459. }
  460.  
  461.  
  462. int main(void)
  463. {
  464.     srand(time(NULL));
  465.     //int choice;
  466.  
  467.     float min_process_a = 0;
  468.     float max_process_a = 6;
  469.     float min_process_b = 1;
  470.     float max_process_b = 8;
  471.     char s[16];
  472.     s[0] = '\0';
  473.  
  474.     while (TRUE)
  475.     {
  476.         printf("\n");
  477.         printf(" _ __ ___   ___ _ __  _   _ \n");
  478.         printf("| '_ ` _ \\ / _ \\ '_ \\| | | |\n");
  479.         printf("| | | | | |  __/ | | | |_| |\n");
  480.         printf("|_| |_| |_|\\___|_| |_|\\____|\n");
  481.  
  482.         printf("\n1: Imitate queue on vecotor");
  483.         printf("\n2: Imitate queue on list");
  484.         printf("\n0: EXIT\n");
  485.         printf("Choice: ");
  486.         fgets(s, 15, stdin);
  487.         if (strcmp(s, "0\n") == 0)
  488.             break;
  489.  
  490.  
  491.         if (strcmp(s, "1\n") == 0)
  492.         {
  493.             double time_sum_a = 0;
  494.             double time_sum_b = 0;
  495.             double time_sum_diff = 0;
  496.             for(int i = 0; i < 100; i++)
  497.             {
  498.                 int queue_a [VECTOR_LEN];
  499.                 //random_vector(queue_a, VECTOR_LEN);
  500.                 // tbh this is not necessary, but why not?
  501.  
  502.                 vector_descriptor_t vector_descriptor_a;
  503.                 vector_descriptor_a.p_out = queue_a;
  504.                 vector_descriptor_a.p_in = &(queue_a[99]);
  505.                 vector_descriptor_a.low_border = queue_a;
  506.                 vector_descriptor_a.high_border = &(queue_a[99]);
  507.  
  508.                 int queue_b [VECTOR_LEN];
  509.                 vector_descriptor_t vector_descriptor_b;
  510.                 vector_descriptor_b.p_out = queue_b;
  511.                 vector_descriptor_b.p_in = queue_b;
  512.                 vector_descriptor_b.low_border = queue_b;
  513.                 vector_descriptor_b.high_border = &(queue_b[99]);
  514.  
  515.                 double time_a = 0;
  516.                 double time_b = 0;
  517.                 double time_b_waiting = 0;
  518.  
  519.                 vector_imitate_queue(vector_descriptor_a, vector_descriptor_b,
  520.                                      min_process_a, max_process_a,
  521.                                      min_process_b, max_process_b,
  522.                                      &time_a, &time_b, &time_b_waiting);
  523.  
  524.                 printf("time a = %lf\n", time_a);
  525.                 printf("time b = %lf\n", time_b);
  526.                 printf("\apparat b waiting time = %lf\n", time_b_waiting);
  527.                 time_sum_a += time_a;
  528.                 time_sum_b += time_b;
  529.                 time_sum_diff += time_b_waiting;
  530.             }
  531.             printf("time a average = %lf\n", time_sum_a / 100.0);
  532.             printf("time b average = %lf\n", time_sum_b / 100.0);
  533.             printf("\apparat b waiting time average = %lf\n", time_sum_diff / 100.0);
  534.         }
  535.  
  536.         else if (strcmp(s, "2\n") == 0)
  537.         {
  538.             printf("LIST QUEUE");
  539.         }
  540.  
  541.         else
  542.             printf("\nWrong choice!");
  543.  
  544.     }
  545.  
  546.     return 0;
  547. }
  548. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement