The Parking Garage The CSC326 Parking Garage contains 2 lane

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.

INCLUDEPICTURE \"http://domanski.cs.csi.cuny.edu/imberman/datastructures/Image18.gif\" \\* MERGEFORMATINET

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.

#include

#include

#include

#include

using namespace std;

struct car

{

   char code;

   string license;

      

};

class stack

{

public:

   static const int CAPACITY = 10;

   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];

  

   int capacity;

};

stack::stack()

{

   top = -1;//nothing on stack initially

}

void stack::push(STACK_TYPE value)

{

   assert(top != CAPACITY - 1);

   top++;

   items[top] = value;

   num_moves++;

}

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) //prints arriving CAR\'s license & time

{

   cout << \"car has been parked in the garage.\" << endl;

   cout << \"license plate number: \" << a << endl;

       cout << \"*********************\" << endl;

}

void departing(string &a)//prints departing car\'s license & TIME

{

   //cout << \"Another car must be moved out of the way!\";

   cout << \"License: \" << a << endl;

}

void print_garage();

stack garage;

stack garage2;

stack street;///GLOBAL

int num_moves;

int main()

{

   // stack garage;

   // stack street;

   car cars[100];

   car temp;

  

   int x = 0;

   fstream textfile;

   textfile.open(\"lab4.txt\");

  

   while (!textfile.eof())

   {

       textfile >> cars[x].code;

       textfile >> cars[x].license;

       if (cars[x].code == \'A\')

       {

           if (garage.is_full())

           {

               cout << \"Lane 1 is full.\" << endl;

               cout << \"License: \" << cars[x].license << endl;

               cout << \"Moving future cars to Lane 2!\";

               if (garage2.is_full())

               {

                   cout << \"Lane 1 is full.\" << endl;

                   cout << cars[x].license << endl;

               }

               else{

                   garage2.push(cars[x]);

               }

           }

           else

           {

               garage.push(cars[x]);

               cout << \"Car is Arriving in lane 1 with license plate \" << cars[x].license << endl;

           }

       }

       if (cars[x].code == \'D\')

       {

          

           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);

                  

                   street.pop();

               }

           }

           while (!street.is_empty())

           {

               temp = street.pop();

               garage.push(temp);

           }

           print_garage();

       }

   }

}

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);

   }

}

The program stops after one depart and im not sure if the cars are being moved from one to the other properly . Do not need any statements for time etc, just needs to take the information from the text document add a car or delete a car from the stack or move it between lane 1, 2 and the street if cars needed to be moved around. Also need a statement to count how many times each car is moved.

Solution

#include

#include

#include

#include

using namespace std;

struct car

{

   char code;

   string license;

      

};

class stack

{

public:

   static const int CAPACITY = 10;

   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];

  

   int capacity;

};

stack::stack()

{

   top = -1;//nothing on stack initially

}

void stack::push(STACK_TYPE value)

{

   assert(top != CAPACITY - 1);

   top++;

   items[top] = value;

   num_moves++;

}

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) //prints arriving CAR\'s license & time

{

   cout << \"car has been parked in the garage.\" << endl;

   cout << \"license plate number: \" << a << endl;

       cout << \"*********************\" << endl;

}

void departing(string &a)//prints departing car\'s license & TIME

{

   //cout << \"Another car must be moved out of the way!\";

   cout << \"License: \" << a << endl;

}

void print_garage();

stack garage;

stack garage2;

stack street;///GLOBAL

int num_moves;

int main()

{

   // stack garage;

   // stack street;

   car cars[100];

   car temp;

  

   int x = 0;

   fstream textfile;

   textfile.open(\"lab4.txt\");

  

   while (!textfile.eof())

   {

       textfile >> cars[x].code;

       textfile >> cars[x].license;

       if (cars[x].code == \'A\')

       {

           if (garage.is_full())

           {

               cout << \"Lane 1 is full.\" << endl;

               cout << \"License: \" << cars[x].license << endl;

               cout << \"Moving future cars to Lane 2!\";

               if (garage2.is_full())

               {

                   cout << \"Lane 1 is full.\" << endl;

                   cout << cars[x].license << endl;

               }

               else{

                   garage2.push(cars[x]);

               }

           }

           else

           {

               garage.push(cars[x]);

               cout << \"Car is Arriving in lane 1 with license plate \" << cars[x].license << endl;

           }

       }

       if (cars[x].code == \'D\')

       {

          

           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);

                  

                   street.pop();

               }

           }

           while (!street.is_empty())

           {

               temp = street.pop();

               garage.push(temp);

           }

           print_garage();

       }

   }

}

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);

   }

}

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

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site