Question

In: Computer Science

Write a program in C++ (parking.cc) that reads a group of input lines. Each line contains...

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:

  • To keep all necessary information for a car, define the following structure:
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:

    • void get_input_vals(const string &line, char &act, string &lp) Extracts individual components from the input line line, where act indicates if the line corresponds to an arrival or a departure event, and lp is the license plate number of the arriving/departing car.
    • void arrival(const CAR &car, deque<CAR> &D) Prints a message stating that a car is arrived. It gets the id number and the license plate number of the car from the car object, and in the message, species those values. If the garage is not full, inserts the car object in the deque D; otherwise, prints a message stating that “the garage is full!”.
    • void departure(const string &lp, deque<CAR> &D, stack<CAR> &S) Prints a message stating that the car with the license plate number lp is departed, and in that message specifies the number of times the car was moved within the garage. For every car moved from the deque D to the stack S and back to D, it updates the value no_mv in the CAR object of that car.
  • 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.

Solutions

Expert Solution

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:


Related Solutions

Use C language Write a program that reads in a series of lines of input character...
Use C language Write a program that reads in a series of lines of input character by character (using getchar()). The first line of the input contains an integer which specifies the number of remaining lines of input, each of which contains a floating point number. The integer value on the first line can be read with scanf(), but all of the following lines can only be read with getchar(). Each line after the first contains a single floating point...
Write a program that reads a file line by line, and reads each line’s tokens to...
Write a program that reads a file line by line, and reads each line’s tokens to create a Student object that gets inserted into an ArrayList that holds Student objects.  Each line from the file to read will contain two strings for first and last name, and three floats for three test grades.  After reading the file and filling the ArrayList with Student objects, sort the ArrayList and output the contents of the ArrayList so that the students with the highest average...
Write a program in c that reads the content from the file and stores each line...
Write a program in c that reads the content from the file and stores each line in an int array in heap(using dynamic memory allocation). For example, let the file has elements following (we do not know the size of files, it could be above 100,000 and contents of the file and make sure to convert file elements to int): 10067 26789 6789 3467
Part 1 Write a program that reads a line of input and display the characters between...
Part 1 Write a program that reads a line of input and display the characters between the first two '*' characters. If no two '*' occur, the program should display a message about not finding two * characters. For example, if the user enters: 1abc*D2Efg_#!*345Higkl*mn+op*qr the program should display the following: D2Efg_#! 1) Name your program stars.c. 2) Assume input is no more than 1000 characters. 3) String library functions are NOT allowed in this program. 4) To read a...
2. Write a Java program that reads a series of input lines from given file “name.txt”,...
2. Write a Java program that reads a series of input lines from given file “name.txt”, and sorts them into alphabetical order, ignoring the case of words. The program should use the merge sort algorithm so that it efficiently sorts a large file. Contents of names.text Slater, KendallLavery, RyanChandler, Arabella "Babe"Chandler, StuartKane, EricaChandler, Adam JrSlater, ZachMontgomery, JacksonChandler, KrystalMartin, JamesMontgomery, BiancaCortlandt, PalmerDevane, AidanMadden, JoshHayward, DavidLavery,k JonathanSmythe, GreenleeCortlandt, OpalMcDermott, AnnieHenry, DiGrey, MariaEnglish, BrookeKeefer, JuliaMartin, JosephMontgomery, LilyDillon, AmandaColby, LizaStone, Mary FrancesChandler, ColbyFrye, DerekMontgomery,...
Write a function in C++ that reads the line separates it with comma. Input: hello how...
Write a function in C++ that reads the line separates it with comma. Input: hello how are you. hello world hello_world I am there for you! Output: hello, how, are and you. hello and world hello_world I, am, there, for and you! just add a comma and (and) before the last word. CODE IN C++ ONLY.
Write a short C++ program that takes all the lines input to standard input and writes...
Write a short C++ program that takes all the lines input to standard input and writes them to standard output in reverse order. That is, each line is output in the correct order, but the ordering of the lines is reversed. Please use vector datatype standaard library #include <vector> Thanks
Write a program that reads two strings from an input file (The first line is X,...
Write a program that reads two strings from an input file (The first line is X, the second line is Y), compute the longest common subsequence length AND the resulting string. You will need to write 2 methods 1) return LCS length in iterative function // return the length of LCS. L is the 2D matrix, X, Y are the input strings, m=|X|, n=|Y| int lcs_it(int **C, string X, string Y, int m, int n ) 2) return LCS resulting...
Write a C++ or Java program that reads an input graph data from a user. Then,...
Write a C++ or Java program that reads an input graph data from a user. Then, it should present a path for the travelling salesman problem (TSP). In the assignment, you can assume that the maximum number ofvertices in the input graph is less than or equal to 20. Input format: This is a sample input from a user. 4 12 0 1 2 0 3 7 0 2 5 1 0 2 1 2 8 1 3 3 2...
Q20. Using C++ style string to write a program that reads a sentence as input and...
Q20. Using C++ style string to write a program that reads a sentence as input and converts each word of the sentence following the rule below: For every word in the sentence, the first letter is relocated the end of the word. Then append the string “KPU” to the word. More requirements: All letters in the output should be uppercase. More assumptions: The input sentence contains no non-alphabetic letters Sample Run: Please enter the original sentence: i LOVE to program...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT