dmilicev

elevator_simulator_v1.c

Sep 1st, 2020
148
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  
  3.     elevator_simulator_v1.c     by Dragan Milicev
  4.  
  5.     Task from Alberto Valles:
  6.     https://web.facebook.com/alberto.valles.3720
  7.  
  8.     https://web.facebook.com/photo/?fbid=641103200120496&set=gm.1679083448917165
  9.  
  10. Try:
  11. Enter next floors separated by commas or 0 to exit: 3,12,5,15,0
  12.  
  13.  
  14. To start the simulation, the Elevator Simulator (ES) must prompt for a starting floor.
  15.  
  16. After this, for each visited floor, the ES must display the floor number and the state
  17. of the door and the car.
  18.  
  19. When the car gets to a destination floor, the ES must stop the car, open the door
  20. and prompt for the next floors.
  21.  
  22. A floor zero input must stop the simulation.
  23.  
  24. A multiple floor input must be a comma-separated list.
  25.  
  26. The ES must visit requested floors by getting to the furthest floor in the current
  27. car direction before reversing direction to visit pending floors as necessary.
  28.  
  29. An empty next floor input must be ignored when no floors are pending.
  30.  
  31. Otherwise, the door must close and the car must start moving toward the next floor.
  32.  
  33. Here is an example of interaction with the ES:
  34.  
  35.  
  36.  Welcome to the Elevator Simulator by M.E.E.E. Engineering
  37.  
  38.  Starting Floor: 7
  39.  
  40.  1 Floor 7       Door: Open      Car: Stopped
  41.  
  42.  Enter next floors separated by commas or 0 to exit: 9
  43.  
  44.  Floor 8         Door: Closed   Car: Moving Up
  45.  
  46.  3 Floor 9       Door: Open      Car: Stopped
  47.  
  48.  Enter next floors separated by commas or 0 to exit: 0
  49.  
  50.  Thank you for using the Elevator Simulator  by M.E.E.E. Engineering.
  51.  
  52.  
  53.  
  54.     You can find all my C programs at Dragan Milicev's pastebin:
  55.  
  56.     https://pastebin.com/u/dmilicev
  57.  
  58. */
  59.  
  60. #include <stdio.h>
  61. #include <string.h>
  62.  
  63. #define STR_SIZE 1024
  64.  
  65. // function bubbleSort to sort the array in descending order
  66. void bubbleSort(int arr[], int n)
  67. {
  68.     int i, j, temp;
  69.  
  70.     for (i = 0; i < n-1; i++)
  71.         for (j = 0; j < n-i-1; j++)     // Last i elements are already in place
  72.             if (arr[j] < arr[j+1])      // arr[j] > arr[j+1] for ascending order
  73.             {
  74.                 temp = arr[j];
  75.                 arr[j] = arr[j+1];
  76.                 arr[j+1] = temp;
  77.             }
  78. } // bubbleSort()
  79.  
  80. /*
  81. 1. Read the entire line in a buffer
  82. 2. Separate in tokens by “,”
  83. 3. For each token, call atoi(token)
  84. 4. Sort array in descending order by calling bubbleSort()
  85. 5. Return length, number of integers in array
  86. */
  87. int enter_next_floors(int array[])
  88. {
  89.     int length=0;
  90.     char buffer[STR_SIZE];
  91.     char *aux;
  92.  
  93.     printf("\n Enter next floors separated by commas or 0 to exit: ");
  94.     fgets(buffer,STR_SIZE-1,stdin);
  95.  
  96.     aux = strtok(buffer, ",");
  97.     while(aux)
  98.     {
  99.         array[length] = atoi(aux);
  100.         length++;
  101.         aux = strtok(NULL, ",");
  102.     }
  103.  
  104.     bubbleSort(array,length);
  105.  
  106.     return length;
  107. } // enter_next_floors()
  108.  
  109.  
  110. int main(void)
  111. {
  112.     int starting_floor=7, next_floor=0;
  113.     int floors[STR_SIZE];
  114.     int num_of_floors=0;
  115.     int i;
  116.  
  117.     printf("\n Welcome to the Elevator Simulator by M.E.E.E. Engineering \n");
  118.     printf("\n Starting Floor: %d \n", starting_floor);
  119.     printf("\n Floor %d \t Door: Open \t Car: Stopped \n", starting_floor);
  120.  
  121.     while(1)
  122.     {
  123.         num_of_floors = enter_next_floors(floors);
  124.  
  125.         for(i=0;i<num_of_floors;i++)
  126.         {
  127.             if(floors[i]==0)                        // Exit of Simulation
  128.             {
  129.                 printf("\n Thank you for using the Elevator Simulator by M.E.E.E. Engineering. \n");
  130.                 return 0;
  131.             }
  132.  
  133.             if(floors[i]<starting_floor)            // Moving Down
  134.             {
  135.                 while(floors[i]<starting_floor-1)
  136.                 {
  137.                     starting_floor--;
  138.                     printf("\n Floor %d \t Door: Closed \t Car: Moving Down \n", starting_floor);
  139.                 }
  140.                 starting_floor--;
  141.                 printf("\n Floor %d \t Door: Open \t Car: Stopped \n", starting_floor);
  142.             }
  143.             else if(floors[i]>starting_floor)       // Moving Up
  144.             {
  145.                 while(floors[i]>starting_floor+1)
  146.                 {
  147.                     starting_floor++;
  148.                     printf("\n Floor %d \t Door: Closed \t Car: Moving Up \n", starting_floor);
  149.                 }
  150.                 starting_floor++;
  151.                 printf("\n Floor %d \t Door: Open \t Car: Stopped \n", starting_floor);
  152.             }
  153.             else    // floors[i] == starting_floor
  154.             {
  155.                 printf("\n You are already on Floor: %d \n", starting_floor);
  156.             }
  157.  
  158.         } // for(i=0;i<num_of_floors;i++)
  159.  
  160.     } // while(1)
  161.  
  162.     return 0;   // Unnecessary line of code, will never be executed.
  163. } // main()
  164.  
RAW Paste Data Copied