The CSC326 Parking Garage contains 2 lanes, each capable of holding up to 10 car
ID: 3677578 • Letter: T
Question
The CSC326 Parking Garage contains 2 lanes, each capable of holding up to 10 cars. There is only a single entrance/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 cars' 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 a file (that you create). Each line in the file contains two fields separated 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 way 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 from the street to a lane counts as 1. Don't forget to check for conditions such as 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 an object from a 'garage' class to represent the 2 lanes and the street. The garage class will contain three stack objects one for each lane and the street. Use the dynamic array implementation of the stack. 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 is NOT a complete list of methods needed, so feel free to experiment and expand the class definitions as much as you like. You may find it easier to have a car class or structure that contains the license plate and a counter for the number of moves the car makes.
use this stack class, and need to creat Garage class
/ file Stack.h
// array stack implementation
#ifndef Stackh
#define Stackh
#include <cstdlib>
template<class PackingType>
class Packing {
// LIFO objects
public:
Packing(int MaxPackingSize = 10);
~Packing() { delete[] packing; }
bool IsEmpty() const { return top == -1; }
bool IsFull() const { return top == MaxTop; }
PackingType Top() const;
void push(const PackingType & x);
void pop();
private:
int top; // current top of stack
int MaxTop; // max value for top
PackingType * packing; // element array
};
template<class PackingType>
Stack< PackingType>::Packing(int Max PackingSize)
{
//Pre: none'
//Post: Array of size MaxStackSaize to implement stack
// Stack constructor.
MaxTop = MaxPackingSize - 1;
packing = new PackingType[MaxPackingSize];
top = -1;
}
template<class PackingType>
PackingType Packing<PackingType>::Top() const
{
//Pre: stack is not empty
// Post: Returns top element.
if (IsEmpty())
throw logic_error("Top fails: Stack is empty");// Top fails
return Packing[top];
}
template<class PackingType>
void Packing<PackingType>::push(const PackingType & x)
{
//Pre: Stack is not full
//Post: Push x to stack.
// Stack has one more element
if (IsFull()) throw logic_error("Push fails: full stack"); // Push fails
Packing[++top] = x;
}
template<class PackingType>
void Packing<PackingType>::pop()
{
//Pre: Stack is not Empty
//Post: Stack has one less element
if (IsEmpty()) {
throw logic_error("Pop fails: Stack is empty");
}; // Pop fails
top--;
}
#endif
i need to put in the file, so i need the file to prite out everything
Explanation / Answer
---> You may want to consider another class. Something like GarageEvent.
--->Each object of type GarageEvent would have two members: one to indicate what time the event happens, and another to indicate whether it is an ARRIVAL event or a DEPARTURE event.
Every time you add a car to the garage, you add two events: an arrival event and a departure event. So you would end up with a container of all the events that take place in a garage over the course of the day.
When you want to determine the largest number of cars the garage had in it, you sort all your GarageEvents by what time they occur.
Iterate over this sorted collection, keeping track of both the current number of cars and the maximum number of cars seen at any one time.
When an ARRIVAL event occurs, add 1 to the current number of cars. If this new number is larger than the max number of cars you've ever seen, update max to reflect this.
When a DEPARTURE event occurs, subtract 1 from the current number of cars.
BACKGROUND: In any programming language,while writing a program we have to follow the "indent" i.e., the program should be written with an indent. and this is nothing but the Principles of a Programming Language(PPL). By following the indent,it will be easier for us to trace the errors after compiling and before executing the program successfully. In the above given program,a samll correction is needed i.e., it was written as "packing type". but please change it as "parking type". In the gelow written program,we have used some user-defined functions like getMaxSizeNeeded(),stack and an array with a size is defined. ILLUSTRATION OF A C++PROGRAM FOR A PARKING GARAGE OF VEHICLES.: #include <cstdlib> #include <iostream> #include <string> #include <vector> #include <ctime> using namespace std; class Car { // creates a car object, basically just stores arrival and departure times public: Car (int); // create a car with an arrival time int getArrivalTime(); // view arrival time int getDepartureTime(); //view departure time private: int arrivalTime; // arrival time of car int departureTime; // departure time of car int parkedTime; // how long the car stays in the garage int getDepartureTime(); // generates the time car stays in garage }; class Garage{ public: vector <Car> arrivals; //starts with all cars before they arrive int number(){ // returns the total number of car arrivals return arrivals.size(); } void gar_sort(); // sift the list out private: vector<Car> in_garage; // basically just the garage, after arrival before departure }; int main() { int garageSize = 0; Garage garage; int seed = time(NULL); // seed value for pseudo-random number generator srand(seed); int currentTime = 0; //current time since midnight in seconds int stopTime = 86400; while (currentTime < stopTime){ /*could be shortened since rates are the same at two different times, but it might be better to leave it to be able to set rates differently later */ int nextTime; if (currentTime < 21600){ // time 0000-0600 int nextRange = 500; nextTime = rand() % nextRange; } else if (currentTime < 25200){ // time 0600-0700 int nextRange = 180; nextTime = rand() % nextRange; } else if (currentTime < 28800){ // time 0700-0800 int next_range = 60; nextTime = rand() % nextRange; } else if (currentTime < 39600){ // time 0800-1100 int nextRange = 12; nextTime = rand() % nextRange; } else if (currentTime < 54000){ // time 1100-1400 int nextRange = 60; nextTime = rand() % nextRange; } else if (currentTime < 68400){ // time 1400-1800 int nextRange = 180; nextTime = rand() % nextRange; } else if (currentTime < 86400){ // time 1800-2400 int nextRange = 500; nextTime = rand() % nextRange; } cout << "Test" << endl; // ----------------------------------------------------debugging only currentTime += nextTime; Car car (currentTime); garage.arrivals.push_back(car); garage.gar_sort(); }; // Car definition functions Car::Car(int t){ // constructor for cars arrivalTime = t; parkedTime = getDepartureTime(); departureTime = arrivalTime + parkedTime; } int Car::getArrivalTime(){ // return arrival time of car return arrivalTime; } int Car::getDepartureTime(){ // return departure time of car return departureTime; } int Car::getDepartureTime(){ // generate staying time of car int val; while (300 > val){ val = rand() % 14400; } return val; } // Garage definition functions void gar_sort(){ // sift the list out int total; // this will be the greatest total number of cars in the garage from any given time cout << "The garage size needs to be " << total << endl; }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.