In: Computer Science
Write a program in C++ (parking.cc) that reads a group of input lines. Each line contains an A for arrival or a D for departure, which is terminated by a :, and a license plate number, which is terminated by a :. The program should print a message each time a car arrives or departs. When a car arrives, the message should specify when the garage is full. If there is no room for a car, the car simply leaves. When a car departs, the message should include the number of times the car was moved within the garage, including the departure itself but not the arrival.
For a departure, you need to check if the car is in the garage. If it’s not, print an error message and ignore the departure. When a car arrives and if the garage is not full, create a C/C++ structure object for the car that contains the id number (starts with 1), the license plate number, and the number of times the car was moved within the garage (with initial value 0), and insert this structure object in a STL deque container. When a car departs, delete the corresponding structure object from the deque.
To save the original ordering of the cars that are moved out from the deque to open the way for a departing car, define a STL stack container to temporarily put the moved cars in the stack, and after the departing car, retrieve all cars from the stack back to the deque into their original positions.
Programming Notes:
typedef struct { int id; // id starts at 1 string lp; // license plate of car int no_mv; // number times the car has been moved }CAR;
In addition to the main() routine, implement the following subroutines in your program:
The main() routine gets the input data from the stdin, line by line. For each input line, it calls the subroutine get_input_vals() to extract the character action, which indicates an arrival or departure, and the license plate number. If action is not the character ‘A’ or ‘D’, prints an error message indicating the invalid action. For an arrival, it fills the individual components in a Car object and calls the function arrival() to process the arrival. For a departure, it calls the function departure() to process the departure.
Create a Makefile that builds an executable parking.exe, to test your program execute your program as ./parking.exe < parking.in. This will redirect the content of parking.in as standard input for your code.
Output results to a file named parking.out and add it to your repository.
You can find the correct output of this program in the output file parking.refout in your repository. You can compare two output files by executing diff parking.refout parking.out.
Assignment Notes:
Include any necessary headers and add necessary global constants.
You are not allowed to use any I/O functions from the C library, such as scanf or printf. Instead, use the I/O functions from the C++ library, such as cin or cout.
Working code implemented in and appropriate comments provided for better understanding.
Source Code for parking.cc:
#include <string>
#include <stdio.h>
#include <string.h>
#include<iostream>
#include <deque>
#include <stack>
#include <bits/stdc++.h>
#include <vector>
using namespace std;
typedef struct
{
int id; // id starts at 1
string lp; // license plate of car
int no_mv; // number times the car has been moved
}CAR;
int cnt =1;
/*
method: arrival
arguments: const CAR &car, deque<CAR> &D
returns: nothing
purpose: Signifies arrival of car in garage, checks to see if
theres
space in the deque and adds it/lets it drive away based on
space
*/
void arrival(const CAR &car, deque<CAR> &D){
cout << "Car " << car.id << " with license plate
" << "\"" << car.lp << "\"" << " is
arrived." << endl ;
if (D.size() < 10){ //check and see if theres space in
garage
D.push_front(car);
cout << endl;
}
else
{
cout << " But the garage is full!" << endl <<
endl;
}
}
/*
method: departure
arguments: const string &lp, deque<CAR> &D,
stack<CAR> &S
returns: nothing
purpose: checks to see if car is present in garage (prints error if
its not) then
moves all cars ahead of departing car into stack S so it can exit.
Cars
in stack S are then re-entered into the deque D
*/
void departure(const string &lp, deque<CAR> &D,
stack<CAR> &S){
bool carFlag = false;
int pos =0;
for (auto i: D){
if (i.lp==lp){ //if license plate is in garage
carFlag = true;
break;
}
pos++; //incremement parking spot position
}
if (carFlag ==false){
cout << "No car with license plate " << "\"" <<
lp << "\"" << " in garage." << endl <<
endl;
return;
}
for (unsigned int i =pos; i < D.size(); i++){ //increment number
ot times moved for cars ahead of departing vehicle
D[i].no_mv++;
}
for (auto i: D){
S.push(i); //push cars onto stack so vehicle can depart
}
D.clear(); // empty deque
while(!S.empty()){ //push cars back into deque
D.push_front(S.top());
S.pop();
}
string s = "";
if(D[pos].no_mv>1){ //if no_mv is more than 1, make cout
statement below plural by adding "s"
s="s";
}
cout << "Car " << D[pos].id << " with license
plate " << "\"" << D[pos].lp << "\"" << "
is departed," << endl ;
cout << " car was moved " << D[pos].no_mv << "
time" << s << " in the garage." << endl <<
endl;
D.erase(D.begin()+pos); // remove car from garage
}
/*
method: get_input_vals
arguments: const string &line, char &act, string
&lp
returns: nothing
purpose: takes a string (line) from stdinput and tokenizes it
into a character (act) and a license plate number (lp)
*/
void get_input_vals(const string &line, char &act, string
&lp){
string delim = ":"; // establish delimeter
string action = line.substr(0, line.find(delim)); //find position
where delimeter is located and substring up to that position
string tempLP = line.substr(line.find(delim)+1); // find location
of next delimeter and substring the rest of the string
tempLP.pop_back(); //remove trailing colon
lp = tempLP; //copy over values
act = action[0]; //copy over values
}
int main () {
// freopen("parking.in", "r", stdin); // ignore for putty - for
windows testing only
deque<CAR> D;
stack<CAR> S;
string line;
char action;
string license;
while (getline(cin,line)){ //while recieving from stdinput
get_input_vals(line, action, license);
if (action!='A' && action !='D'){ //checking to see if
action is valid
cerr << '\'' << action << '\'' << ": "
<< "invalid action!" << endl <<endl;
continue;}
CAR arr;
arr.id = cnt;
bool carFlag = false;
for (auto i: D){ // find out if license plate is already in
garage
if (i.lp==license){
carFlag = true;
break;
}
}
if (carFlag ==false){ //if license plate NOT in garage, its a new
car and needs incrememnted ID
cnt++;
}
arr.lp = license;
arr.no_mv = 0;
if (action == 'A'){ //if A, arriving
arrival(arr, D);
}
else{ //else has to be a departure since already verified the
action is valid
departure(arr.lp, D, S);
}
}//end of while
}
Sample Output Screenshots: