Hey guys i need with this program everytime i run it nothing is outputing i don't know how to fix it.
Thanks in advance. By the way i am new to this forum so i don't if this is the right place to post this thanks.
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 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.
Here are the program it has 3 files a cpp and 2 headers.
The text file is name ParkingGarage.txt
it suppose to read this inputs
A 123ABC
A 345XYZ
D 123DEF
A 674GTX
A 896YUX
D 234FDS
A 567TYD
A 891JKL
D 435GHD
A 786IOC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
|
Main
#include<iostream>
#include<string>
#include<fstream>
#include <cstdlib>
#include"Garage.h"
#include"Stack.h"
using namespace std;
int main()
{
GarageD car;
ifstream infile;
ofstream outfile;
infile.open("ParkingGarage.txt");
if (infile.fail())
{
cout << "file can not open!" << endl;
}
string newcar, newcar2;
while (!infile.eof())
{
infile >> newcar;
infile >> newcar2;
if (newcar == "a")
{
car.arrive(newcar2);
}
else
if (newcar == "d")
car.depart(newcar2);
}
infile.close();
return 0;
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
|
Garage.h
#include<iostream>
#include<string>
#include"stack.h"
#ifndef Garage_h
#define Garage_h
using namespace std;
struct car
{
int moves;
string plateNumber;
public:
bool operator==(const car&x)
{ return(plateNumber == x.plateNumber); }
};
class GarageD
{
private:
car a;
car c;
Stack<car> lane1;
Stack<car> lane2;
Stack<car> street;
public:
GarageD();
void arrive(string vehicle);
void depart(string vehicle);
};
GarageD::GarageD()
{
}
void GarageD::arrive(string vehicle)
{
if (!lane1.IsFull())
{
cout << vehicle << " has been parked in line 1.\n";
a.plateNumber = vehicle;
a.moves = 0;
lane1.push(a);
}
else if (!lane2.IsFull())
{
cout << "line1 is full, the car is moving to line2" << endl;
cout << vehicle << " has been parked in line 2.\n";
a.plateNumber = vehicle;
a.moves = 0;
lane2.push(a);
}
else
cout << "both lines are full, your car was not parkerd!" << endl;
}
void GarageD::depart(string vehicle)
{
a.plateNumber = vehicle;
c.plateNumber = vehicle;
cout << endl;
if (lane1.Search(a))
{
while (a.plateNumber != lane1.Top().plateNumber)
{
c = lane1.Top();
c.moves++;
if (!lane2.IsFull()) {
lane2.push(c);
lane1.pop();
}
else {
street.push(c);
lane1.pop();
}
}
c = lane1.Top();
c.moves++;
cout << "Car is in line1, and it has been depart from line 1."<<c.plateNumber<<"Total number of moves"<<c.moves << endl;
lane1.pop();
}
else if (lane2.Search(a))
{
while (a.plateNumber != lane2.Top().plateNumber)
{
c = lane2.Top();
c.moves++;
if (!lane1.IsFull())
{
lane1.push(c);
lane2.pop();
}
else {
street.push(c);
lane2.pop();
}
}
c = lane2.Top();
c.moves++;
cout << "The car depart from line 2."<<c.plateNumber <<"Total number of moves"<<c.moves<< endl;
lane2.pop();
}
while (!street.IsEmpty())
{
if (!lane1.IsFull())
{
c = street.Top();
c.moves++;
lane1.push(c);
street.pop();
}
else
{
c = street.Top();
c.moves++;
lane2.push(c);
street.pop();
}
}
}
#endif
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
|
Stack.h
// file Stack.h
// array stack implementation
#ifndef Stackh
#define Stackh
#include <cstdlib>
template<class StackType>
class Stack {
// LIFO objects
public:
Stack(int MaxStackSize = 5);
~Stack() { delete[] stack; }
bool IsEmpty() const { return top == -1; }
bool IsFull() const { return top == MaxTop; }
StackType Top() const;
void push(StackType x);
void pop();
bool Search(StackType x);
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 MaxStackSize 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(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");
exit(1);
}; // Pop fails
top--;
}
template<class StackType>
bool Stack<StackType>::Search(StackType x)
{
for (int i = 0; i <= top; i++)
{
if (*(stack + i) == x)
return true;
}
return false;
}
#endif
|