The CSC326 parking garage contains 2 lanes, each capable of holding up to 10 car
ID: 3762441 • Letter: T
Question
The CSC326 parking garage contains 2 lanes, each capable of holding up to 10 cars. There is only a single entrace/exit to the garage at one end of the lanes. If a customer arrives to pick up a car which is not nearest the exit, all cars blocking the car's path are moved into the other lane. If more cars still must be moved out of the way, they go into the street. When the customer's car is driven out, all cars in the street must be put back into the garage.
Write a C++ program that reads input from either a file(that you create). Each line in the file contain two fields seperated by a blank: a code(A = an arriving car, or D = a car wishes to depart) and a license plate number (this could be a string). Cars are assumed to arrive and depart in the order specified by the input. The program should print a message whenever a car arrives or departs.
When a car arrives, the message should specify whether or not there is room in the garage for the car. If there is no room, the car leaves without entering. When a car departs, the message should include the number of times the car had to be moved out of the wway so that other cars could depart. Each move from one lane to the other counts as 1: each move to the street counts as 1: each move form the stree to a lane counts as 1: Don't forget to check for screwy things~ e.g someone wanting a car that's not in the garage, trying to park a car but both lanes are full, trying to park a car when only one lane is full, etc.
Your program should define objects from a 'lane' class to represent the 2 lanes and the street. The lane class will contain three stack objects one for each lane and the street. Represent the cars in each lane using a car class. The car class can contain variables that hold its license plate, and the number of times it's moved. You'll need methods for manipulating the cars in the lanes e.g. search for a car in a lane, move a car from a lane to somewhere, and perhaps higher-level methods like arrive and depart and methods to handle your output. This in NOT a complete list of methods needed, so feel free to experiment and expand the class definitions as much as you like.
The question is about stack and not queue.
Explanation / Answer
With stack: #include <iostream> #include <fstream> #include <string> #include <cassert> using namespace std; struct car { char code; string license; int hour; int min; }; class stack { public: static const int CAPACITY = 6; typedef car STACK_TYPE; ///MODIFIED from int to car stack(); // constructor void push(STACK_TYPE); // add element to the stack STACK_TYPE pop(); // return top element bool is_empty(); // is stack empty? bool is_full(); private: int top; STACK_TYPE items[CAPACITY]; }; stack::stack() { top=-1;//nothing on stack initially } void stack::push(STACK_TYPE value) { assert(top !=CAPACITY-1); top++; items[top]=value; } stack::STACK_TYPE stack::pop() { STACK_TYPE value; assert(top>=0); value= items[top]; top--; return(value); } bool stack::is_empty() { if (top== -1) return true; else return false; } bool stack::is_full() { if (top==CAPACITY-1) return true; else return false; } void arriving(string &a, int &b, int &c) //prints arriving car's license & time { cout<<"car has been parked in the garage."<<endl; cout<<"license plate number: "<<a <<endl; cout<<"current time:" << b<<":"; if (c>=10) cout<<c<<endl; else cout<<"0"<<c<<endl; cout <<"*********************"<<endl; } void departing(string &a, int&b, int &c)//prints departing car's license & time { cout<<"License: "<<a<<endl; cout<<"Time of departure: " <<b<<":"; if (c>=10) cout<<c<<endl; else cout<<"0"<<c<<endl; } void time_parked(int &a, int &b, int &c, int &d)//prints time parked & charges { int hour_diff; int min_diff; double charges; if (d<b)//if departing minutes is less than arriving minutes { c--; } hour_diff = c-a; min_diff = d-b; charges = 7.5*2*hour_diff; if (min_diff>30) { charges = charges + 7.5*2; } else if (min_diff>0) { charges = charges + 7.5; } cout<<"Time Parked: "<<hour_diff<<":"; if (min_diff>=10) cout<<min_diff<<endl; else cout<<"0"<<min_diff<<endl; cout<<"Total Charges: $"<<charges<<endl; } void print_garage(); stack garage; ///MADE GLOBAL stack street; ///MADE GLOBAL int main () { // stack garage; // stack street; car cars[100]; car temp; int new_hour; int new_min; int x=0; fstream textfile; textfile.open("data4.txt"); //textfile>>cars[x].code; while(!textfile.eof()) { textfile>>cars[x].code; ///MOVED DOWN textfile>> cars[x].license; if (cars[x].code =='A') ///MODIFIED from "A" to 'A' { if (garage.is_full()) { cout<<"Garage is full."<<endl; cout<<"License: "<<cars[x].license<<endl; //MODIFIED from license to cars[x].license } else { textfile>>cars[x].hour; textfile>>cars[x].min; arriving(cars[x].license, cars[x].hour, cars[x].min); garage.push(cars[x]); } } if (cars[x].code=='D') ///MODIFIED from "D" to 'D' { textfile>>new_hour; textfile>>new_min; while (!garage.is_empty()) { temp = garage.pop(); street.push(temp); while (temp.license!=cars[x].license) {//backing cars from garage to street until finding the matching license car if(!garage.is_empty()) { temp = garage.pop(); street.push(temp); } } if (temp.license!=cars[x].license) { cout<<"Car not found"<<endl; cout<<"License: "<<cars[x].license; } else { departing(cars[x].license, new_hour, new_min); time_parked(cars[x].hour, cars[x].min, new_hour, new_min); street.pop(); } } while(!street.is_empty()) { temp = street.pop(); garage.push(temp); } print_garage(); } } system("pause"); } void print_garage() { car temp; while (!garage.is_empty()) { temp = garage.pop(); cout<<temp.license<<endl; street.push(temp); } while(!street.is_empty()) { temp = street.pop(); garage.push(temp); } } With queue: #include <iostream> #include <fstream> using namespace std; const int maxqueue = 5; class queue_type { public: void clear_queue(); bool empty_queue(); bool full_queue(); void insert_queue(int numb); void delete_queue(int& numb); int queue[6]; int front, rear; }; queue_type a, b, c, a2, b2, c2; //============== int main() { int x = 0; int g = 0; //counter for exit output int q = 0; //temp value for popping queue char n; //n is arrive, depart, or exit; while x is numb number a.clear_queue(); b.clear_queue(); c.clear_queue(); a2.clear_queue(); b2.clear_queue(); c2.clear_queue(); // WELCOME CODE do { cout << "Welcome to Scratch Yard Inc. Press a for arrival, d for departure, or q to quit " << endl; cout << "Enter selection letter : "; cin >> n; if ((!(n=='q')) && (!(n=='Q'))) { cout << " Enter a license number : "; cin >> x; } // ARRIVAL CODE while ((n=='a') || (n=='A')) { if (!(a.full_queue())) { a.insert_queue(x); cout << "Vehicle has been parked in Scratch-em-up." << endl; } else if (!(b.full_queue())) { b.insert_queue(x); cout << "Scratch-em-up is full. Vehicle has been parked in Knock-em-dead." << endl; } else if (!(c.full_queue())) { c.insert_queue(x); cout << "Both garages are full. Vehicle has been parked on the street." << endl; } else { cout << "All garages and the street are full." << endl << endl; } }//End while // DEPARTURE CODE if ((n=='d') || (n=='D')) //n is decision, x is plate number { while (!(a.empty_queue())) { a.delete_queue(q); a2.insert_queue(q); }//end while while (!(b.empty_queue())) { b.delete_queue(q); b2.insert_queue(q); }//end while while (!(c.empty_queue())) { c.delete_queue(q); c2.insert_queue(q); }//end while while (!(a2.empty_queue())) //A queue filling { a2.delete_queue(q); if (x == q) { cout << "Car with plate " << x << " has left Scratch-em-up." << endl; } if (!(x == q)) { a.insert_queue(q); } }//end while while (!(b2.empty_queue())) //B queue filling { b2.delete_queue(q); if (x == q) { cout << "Car with plate " << x << " has left Knock-em-dead." << endl; } if (!(x == q)) { if (!(a.full_queue())) { a.insert_queue(q); } if ((a.full_queue()) && (!(b.full_queue()))) { b.insert_queue(q); } } } while (!(c2.empty_queue())) //C queue filling { c2.delete_queue(q); if (x == q) { cout << "Car with plate " << x << " has left the street." << endl; } if (!(x == q)) { if (!(b.full_queue())) { b.insert_queue(q); } if ((b.full_queue()) && (!(c.full_queue()))) { c.insert_queue(q); } } }//end while } }//end do while ((!(n == 'q')) && (!(n == 'Q'))); // below is Scratch-em-up exit output if ((n == 'Q') || (n == 'q')) { while (!(a.empty_queue())) { a.delete_queue(q); g++; } if (!(g == 0)) { cout << " You are leaving the program with " << g << " cars in Scratch-em-up." << endl; g = 0; } else { cout << " You are leaving the program with no cars in either of the garages, or on the street." << endl; } } // below is knock-em-dead exit output if (((n == 'Q') || (n == 'q')) && (!(b.empty_queue()))) { while (!(b.empty_queue())) { b.delete_queue(q); g++; } cout << "You are leaving the program with " << g << " cars Knock-em-dead." << endl; g = 0; } // below is the street exit output if (((n == 'X') || (n == 'x')) && (!(c.empty_queue()))) { while (!(c.empty_queue())) { c.delete_queue(q); g++; } cout << "You are leaving the program with " << g << " cars on the street." << endl; g = 0; } return 0; } //=============== void queue_type::clear_queue() { front = maxqueue; rear = maxqueue; } //=============== bool queue_type::empty_queue() { if (rear == front) return true; else return false; } //=============== bool queue_type:: full_queue() { int querear; if (rear == maxqueue) querear = 0; else querear = rear + 1; if (querear == front) return true; else return false; } //================ void queue_type::insert_queue(int numb) { if (rear == maxqueue) rear = 0; else rear = rear + 1; queue[rear] = numb; } //================= void queue_type::delete_queue(int& numb) { if (front == maxqueue) front = 0; else front = front + 1; numb = queue[front]; } //================= With dequeue: #include <stdio.h> #include <conio.h> #include <alloc.h> #include <string.h> #define TOP 1 #define BOT 2 struct node { char plate [15] ; struct node *link ; } *front[5], *rear[5] ; char plate[15], temp[15] ; int i ; void add_dq ( struct node**, struct node**, int, char* ) ; char* del_dq ( struct node**, struct node**, int ) ; void push ( struct node**, char* ) ; char* pop ( struct node** ) ; void q_display ( struct node * ) ; void main( ) { char ad ; int s, lane = -1, min, lc ; clrscr( ); while ( 1 ) { for ( i = 0 ; i < 5 ; i++ ) { printf( "lane %d: ", i ) ; q_display ( front[i] ) ; } printf( " Arrival/Departure/Quit? ( A/D/Q ): " ) ; ad = getch( ) ; if ( toupper ( ad ) == 'Q' ) exit ( 1 ) ; printf ( " Enter license plate num:" ) ; gets ( plate ) ; ad = toupper ( ad ) ; if ( ad == 'A' ) /* arrival of car */ { lane = -1 ; /* assume no lane is available */ min = 10 ; for ( i = 0 ; i < 5 ; i++ ) { s = count ( front[i] ) ; if ( s < min ) { min = s ; lane = i ; } } if ( lane == -1 ) printf ( " No room available" ) ; else { add_dq ( &front[ lane ], &rear[ lane ], BOT, plate ) ; printf ( " park car at lane %d slot %d ", lane, s ) ; } } else { if ( ad == 'D' ) /* departure of car */ { for ( i = 0 ; i < 5 ; ++i ) { s = search ( front[i], plate ) ; if ( s != -1 ) { lane = i ; break ; } } if ( i == 5 ) printf ( " no such car!! " ) ; else { printf ( " car found at lane %d slot %d ", lane, s ) ; del_dq ( &front[ lane ], &rear[ lane ], s ) ; } } elseif ( ad == 'Q' ) exit ( 1 ) ; } } } /* adds a new element at the end of queue */ void add_dq ( struct node **f, struct node **r, int tb, char *p ) { struct node *q ; /* create new node */ q = ( struct node * ) malloc ( sizeof ( struct node ) ) ; strcpy ( q -> plate, p ) ; q -> link = NULL ; /* if the queue is empty */ if ( *f == NULL ) *f = q ; else { if ( tb == BOT ) ( *r ) -> link = q ; else { q -> link = *f ; *f = q ; return ; } } *r = q ; } char* del_dq ( struct node **f, struct node **r, int n ) { struct node *q, *top = NULL ; /* if queue is empty */ if ( *f == NULL ) printf ( "queue is empty" ) ; else { if ( n == 0 ) { strcpy ( temp, ( *f ) -> plate ) ; q = *f ; *f = ( *f ) -> link ; free ( q ) ; return temp ; } /* locate node */ for ( i = 0 ; i < n ; i++ ) { /* drive out cars */ push ( &top, ( *f ) -> plate ) ; /* delete the node */ q = *f ; *f = q -> link ; free ( q ) ; } /* delete the nth node */ q = *f ; *f = q -> link ; free ( q ) ; for ( i = 0 ; i < n ; i++ ) { strcpy ( temp, pop ( &top ) ) ; /* add the node */ add_dq ( f, r, TOP, temp ) ; } } } int count ( struct node *q ) { int c = 0 ; /* traverse the entire linked list */ while ( q!= NULL ) { q = q -> link ; c++ ; } return c ; } int search ( struct node *q, char *p ) { int s = -1, c = 0 ; while ( q != NULL ) { if ( strcmp ( p, q -> plate ) == 0 ) { s = c ; break ; } else { q = q -> link ; c++ ; } } return ( s ) ; } /* adds a new element to the top of stack */ void push ( struct node **s, char* item ) { struct node *q ; q = ( struct node* ) malloc ( sizeof ( struct node ) ) ; strcpy ( q -> plate, item ) ; q -> link = *s ; *s = q ; } /* removes an element from top of stack */ char* pop ( struct node **s ) { struct node *q ; /* if stack is empty */ if ( *s == NULL ) { return NULL ; } else { q = *s ; strcpy ( temp, q -> plate ) ; *s = q -> link ; free ( q ) ; return ( temp ) ; } } void q_display ( struct node *q ) { while( q != NULL ) { printf ( "%s", q -> plate ) ; q = q -> link ; } printf ( " " ) ; }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.