Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- elevator_simulator_v1.c by Dragan Milicev
- Task from Alberto Valles:
- https://web.facebook.com/alberto.valles.3720
- https://web.facebook.com/photo/?fbid=641103200120496&set=gm.1679083448917165
- Try:
- Enter next floors separated by commas or 0 to exit: 3,12,5,15,0
- To start the simulation, the Elevator Simulator (ES) must prompt for a starting floor.
- After this, for each visited floor, the ES must display the floor number and the state
- of the door and the car.
- When the car gets to a destination floor, the ES must stop the car, open the door
- and prompt for the next floors.
- A floor zero input must stop the simulation.
- A multiple floor input must be a comma-separated list.
- The ES must visit requested floors by getting to the furthest floor in the current
- car direction before reversing direction to visit pending floors as necessary.
- An empty next floor input must be ignored when no floors are pending.
- Otherwise, the door must close and the car must start moving toward the next floor.
- Here is an example of interaction with the ES:
- Welcome to the Elevator Simulator by M.E.E.E. Engineering
- Starting Floor: 7
- 1 Floor 7 Door: Open Car: Stopped
- Enter next floors separated by commas or 0 to exit: 9
- Floor 8 Door: Closed Car: Moving Up
- 3 Floor 9 Door: Open Car: Stopped
- Enter next floors separated by commas or 0 to exit: 0
- Thank you for using the Elevator Simulator by M.E.E.E. Engineering.
- You can find all my C programs at Dragan Milicev's pastebin:
- https://pastebin.com/u/dmilicev
- */
- #include <stdio.h>
- #include <string.h>
- #define STR_SIZE 1024
- // function bubbleSort to sort the array in descending order
- void bubbleSort(int arr[], int n)
- {
- int i, j, temp;
- for (i = 0; i < n-1; i++)
- for (j = 0; j < n-i-1; j++) // Last i elements are already in place
- if (arr[j] < arr[j+1]) // arr[j] > arr[j+1] for ascending order
- {
- temp = arr[j];
- arr[j] = arr[j+1];
- arr[j+1] = temp;
- }
- } // bubbleSort()
- /*
- 1. Read the entire line in a buffer
- 2. Separate in tokens by “,”
- 3. For each token, call atoi(token)
- 4. Sort array in descending order by calling bubbleSort()
- 5. Return length, number of integers in array
- */
- int enter_next_floors(int array[])
- {
- int length=0;
- char buffer[STR_SIZE];
- char *aux;
- printf("\n Enter next floors separated by commas or 0 to exit: ");
- fgets(buffer,STR_SIZE-1,stdin);
- aux = strtok(buffer, ",");
- while(aux)
- {
- array[length] = atoi(aux);
- length++;
- aux = strtok(NULL, ",");
- }
- bubbleSort(array,length);
- return length;
- } // enter_next_floors()
- int main(void)
- {
- int starting_floor=7, next_floor=0;
- int floors[STR_SIZE];
- int num_of_floors=0;
- int i;
- printf("\n Welcome to the Elevator Simulator by M.E.E.E. Engineering \n");
- printf("\n Starting Floor: %d \n", starting_floor);
- printf("\n Floor %d \t Door: Open \t Car: Stopped \n", starting_floor);
- while(1)
- {
- num_of_floors = enter_next_floors(floors);
- for(i=0;i<num_of_floors;i++)
- {
- if(floors[i]==0) // Exit of Simulation
- {
- printf("\n Thank you for using the Elevator Simulator by M.E.E.E. Engineering. \n");
- return 0;
- }
- if(floors[i]<starting_floor) // Moving Down
- {
- while(floors[i]<starting_floor-1)
- {
- starting_floor--;
- printf("\n Floor %d \t Door: Closed \t Car: Moving Down \n", starting_floor);
- }
- starting_floor--;
- printf("\n Floor %d \t Door: Open \t Car: Stopped \n", starting_floor);
- }
- else if(floors[i]>starting_floor) // Moving Up
- {
- while(floors[i]>starting_floor+1)
- {
- starting_floor++;
- printf("\n Floor %d \t Door: Closed \t Car: Moving Up \n", starting_floor);
- }
- starting_floor++;
- printf("\n Floor %d \t Door: Open \t Car: Stopped \n", starting_floor);
- }
- else // floors[i] == starting_floor
- {
- printf("\n You are already on Floor: %d \n", starting_floor);
- }
- } // for(i=0;i<num_of_floors;i++)
- } // while(1)
- return 0; // Unnecessary line of code, will never be executed.
- } // main()
Add Comment
Please, Sign In to add comment