#include <bits/stdc++.h> using namespace std; //for time I made a class class DepartureTime { int hh; int mm; int ss; public: DepartureTime() { hh =0; mm = 0; ss=0; } DepartureTime(int hr, int min, int sec) { hh = hr; mm = min; ss = sec; } //return as int int getTime() { return hh*10000 + mm*100 + ss; } //return as string to display string toString() { string a =to_string(hh) + " : " +to_string(mm) +" : " + to_string(ss); return a; } }; //strucy struct planes { int planeID; int routeID; DepartureTime time; // hr:mm:ss in 24 hour format planes* plane; //self referential structure }; //fill planeid and route id void function1(planes array[], int size) { for(int i=0; i<size; i++) { //rand gives a randome number, take mod by 900 to get a number less than 900 and add 100 to get a number from // 0 t0 1000 int v = rand() % 900 + 100; int v2 = rand() %1000 + 1000; array[i].planeID = v; array[i].routeID = v2; } }; //initialise with random time void function2(planes array[], int size) { for(int i=0; i<size; i++) { int hr = rand() %24; int min = rand() %60; int sec = rand() %60; DepartureTime t(hr, min, sec); array[i].time = t; } } //display void function3(planes array[], int size) { cout<<"Plane ID\t Route ID\t Departure DepartureTime\n"; for(int i =0; i<size; i++) { cout<<array[i].planeID<<"\t\t"<<array[i].routeID<<"\t\t"<<array[i].time.toString()<<endl; } } //sort in descending using selection sort void function4(planes array[], int size) { for(int i=0; i<size; i++) { int min =i; for(int j=i+1; j<size; j++) { if(array[i].time.getTime() < array[j].time.getTime()) { planes temp = array[i]; array[i] = array[j]; array[j] = temp; } } } } void function5(stack<planes>* s) { //last element of array will be removed, which is done by size-- itself (*s).pop(); } void function6(queue<planes>* q, planes array[], int size) { (*q).pop(); //also remove from first element of array for(int i=1; i<size; i++) { array[i-1] = array[i]; } } int main() { planes array[15]; int size =15; function1(array, size); function2(array, size); function4(array, size); function3(array, size); //implement stack and queue // in stack last in first out, use stack to take off plane with earliest departure //in queue first in first out, use stack to take off plane with last scheduled time stack<planes> s; for(int i=0; i<size; i++) { //no planes struct exist before 0 if(i != 0) array[i].plane = &(array[i-1]); s.push(array[i]); } queue<planes> q; for(int i=0; i<size; i++) { q.push(array[i]); } //in function 5 we pass stack by address to show the changes in main fucntion and decrease size to reduce plane by 1 function5(&s); size--; cout<<endl; cout<<"After a plane takes off from schedule\n"; cout<<endl; //DISPLAy again function3(array, size); cout<<endl; cout<<"After taking off the last schedules plane\n"; cout<<endl; function6(&q, array, size); size--; cout<<endl; //DISPLAy again function3(array, size); }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)