Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Lab Assignment #2 The Parking Garage The CSC326 Parking Garage contains 2 lanes,

ID: 3680970 • Letter: L

Question

Lab Assignment #2

The Parking Garage

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 likearriveand 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.

// file Stack.h

// array stack implementation

#ifndef Stackh

#define Stackh

#include <cstdlib>

#include<iostream>

using namespace std;

template<class StackType>

class Stack {

// LIFO objects

public:

Stack(int MaxStackSize = 3);

~Stack() { delete[] stack; }

bool IsEmpty() const { return top == -1; }

bool IsFull() const { return top == MaxTop; }

StackType Top() const;

void push(const StackType & x);

void pop();

private:

int top; // current top of stack

int MaxTop; // max value for top

StackType *stack; // element array

};

template<class StackType>

Stack<StackType>::Stack(int MaxStackSize)

{

//Pre: none'

//Post: Array of size MaxStackSaize to implement stack

// Stack constructor.

MaxTop = MaxStackSize - 1;

stack = new StackType[MaxStackSize];

top = -1;

}

template<class StackType>

StackType Stack<StackType>::Top() const

{

//Pre: stack is not empty

// Post: Returns top element.

if (IsEmpty())

throw logic_error("Top fails: Stack is empty");// Top fails

return stack[top];

}

template<class StackType>

void Stack<StackType>::push(const StackType & 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

stack[++top] = x;

}

template<class StackType>

void Stack<StackType>::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

// file Stack.h

// array stack implementation

#ifndef Garageh

#define Garageh

#include<iostream>

#include<fstream>

#include<string>

#include"stack.h"

using namespace std;

//structure for car file

//V is vehicle that mean car

struct car

{

int counter = 0;

char code;

string plateName;

bool operator==(const car &obj2)

{

if (plateName == obj2.plateName)

return true;

else

return false;

}

};

//Garage function

template<class GarageType>

class Garage

{

public:

Garage();

bool isEmpty() const;

bool isFull() const;

void arrive(car v);

void depart(car v);

int searchCar(Stack<car> &line, car v);

bool operator==(const Stack<car> &obj2);

private:

Stack<car> line1;

Stack<car> line2;

Stack<car> street;

};

template<class GarageType>

Garage<GarageType>::Garage()

{

}

//departing function

template<class GarageType>

void Garage<GarageType>::depart(car v)

{

int carposition = searchcar(line1, v);

if (carposition == -1)

{

carposition = searchcar(line2, v);

if (carposition != -1)

{

cout << "The cardepart from line 2 ." << endl;

}

}

else

{

cout << "Car is in line1, and it has been depart from line 1." << endl;

}

if (carposition == -1)

{

cout << "The car is not in both line" << endl;

}

}

//arriving car function

template<class GarageType>

void Garage<GarageType>::arrive(car v)

{

if (!line1.IsFull())

{

line1.push(v);

cout << v.plateName << " has been parked in line 1. ";

}

else if (!line2.IsFull())

{

cout << "line1 is full, the car is moving to line2" << endl;

line2.push(v);

cout << v.plateName << " has been parked in line 2. ";

}

else

{

cout << "both lines are full, no more space to park your car!" << endl;

}

}

template<class GarageType>

int Garage<GarageType>::searchCar(Stack<car> &line, car v)

{

car temp;

int counter = 0;

while (!line.IsEmpty())

{

if (line.Top().plateName == v.plateName)

{

line.pop(); int i = 0;

while (i < counter)

{

temp = street.Top();

street.pop();

v.moveCount++;

line.push(temp);

i++;

}

cout << "The movecount : " << v.moveCount << endl;

return 1;

}

else

{

if (line2 == line)

{

if (!line2.IsFull())

{

temp = line.Top();

line.pop();

v.moveCount++;

line2.push(temp);

counter++;

}

}

else if (line1 == line)

{

if (!line1.IsFull())

{

temp = line.Top();

line.pop();

v.moveCount++;

line1.push(temp);

counter++;

}

}

else

{

temp = line.Top();

line.pop();

v.moveCount++;

street.push(temp);

counter++;

}

}

}

return -1;

}

template<class GarageType>

bool Garage<GarageType>::operator==(const Stack<car> &obj2)

{

Stack<car> obj1;

if (obj1 == obj2)

return true;

else

return false;

}

#endif

#include<iostream>

#include<string>

#include<fstream>

#include"Garage.h"

#include"Stack.h"

using namespace std;

// main function

int main()

{

Garage<car> Garage;

//read in the file

fstream input;

//open the text file

input.open("parking.txt");

if (!input)

{

cout << "file can not open !" << endl;

input.close();

system("pause");

return 0;

}

while (!input.eof())

{

car newcar;//make a newcar

input >> newcar.code >> newcar.plateName;//to get the car's status and the plate number

//find the car status is departing or arriving.

switch (newcar.code)

{

case 'A':

Garage.arrive(newcar);

break;

case 'D':

Garage.depart(newcar);

break;

default:

cout << "This car status is not exist. ";

break;

}

}

system("pause");

return 0;

}

i know i have error in this program. so i need someone help me fix it . and can you use all if loop change to other loop. please follow what i give.

Explanation / Answer

using stack with linked list and pointer can make things work easy as follows

/*
* C++ Program To Implement Stack using Linked List
*/
#include<iostream>
#include<cstdlib>
using namespace std;

/*
* Node Declaration
*/
struct node
{
int info;
struct node *link;
}*top;

/*
* Class Declaration
*/
class stack_list
{
public:
node *push(node *, int);
node *pop(node *);
void traverse(node *);
stack_list()
{
top = NULL;
}   
};

/*
* Main: Contains Menu
*/
int main()
{
int choice, carnum;
stack_list park;
while (1)
{
cout<<" -------------"<<endl;
cout<<"Operations on Stack"<<endl;
cout<<" -------------"<<endl;
cout<<"1.park the car in garage"<<endl;
cout<<"2.depart the car from garage"<<endl;
cout<<"3.check the garage"<<endl;
cout<<"4.Quit"<<endl;
cout<<"Enter your Choice: ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Enter car number to be parked ";
cin>>carnum;
top = sl.push(top, carnum);
break;
case 2:
top = sl.pop(top);
break;
case 3:
sl.traverse(top);
break;
case 4:
exit(1);
break;
default:
cout<<"Wrong Choice"<<endl;
}
}
return 0;
}

/*
* Push Element into the Stack
*/
node *stack_list::push(node *top, int carnum)
{
node *tmp;
tmp = new (struct node);
tmp->info = carnum;
tmp->link = top;
top = tmp;
return top;
}

/*
* Pop Element from the Stack
*/
node *stack_list::pop(node *top)
{
node *tmp;
if (top == NULL)
cout<<"Stack is Empty"<<endl;
else
{   
tmp = top;
cout<<"Element Popped: "<<tmp->info<<endl;
top = top->link;
free(tmp);
}
return top;
}

/*
* Traversing the Stack
*/
void stack_list::traverse(node *top)
{   
node *ptr;
ptr = top;
if (top == NULL)
cout<<"garage is empty"<<endl;
else
{
cout<<"cars in the garage :"<<endl;
while (ptr != NULL)
{
cout<<ptr->info<<endl;
ptr = ptr->link;
}
}
}