Can anyone help me,please?
this is about:
There is only one entrance/exit to the garage. If someone comes to retrieve a car that is not first out of exit, all cars blocking its path must be moved out (hence the stack). After the car is taken out, all other cars are restored in order they were taking out. Each line of the input file contains an "A" for arrival or "D" for departure, a license plate number (6 characters) and the time of arrival or departure.
The program should print a message whenever a car arrives or departs. When a car arrives, the message should specify the time of arrival and whether or not there is room for the car in the garage (10 car limits).
sample output:
Car with license no. DT3172 arriveg at 8:01 AM car parked
Car with license no. BFD111 arriving at 12:52 PM no room available
Car with license no. BFD111 deparing at 1:33 PM no such car parked here
Car with license no. DT3172 departing at 2:59 PM
times moved: 2
time parked: 6 hours and 58 minutes
Note: when calculating the time parked, first convert the time of arrival and the time of departure to military time.
Note: use stack
Code:
#ifndef GARAGE_H
#define GARAGE_H
const int MAX_CARS= 6;
class GarageStack
{
public:
bool IsEmpty() const;
bool IsFull() const;
void Push(char AorD, char tag[], int milhours, int milmin, char AorP[]);
void Pop(char AorD, char tag[], int milhours, int milmin, char AorP[]);
GarageStack();
struct car
{
char arrival;
char tag[MAX_CARS];
int hours;
int minutes;
char AorP[2];
};
private:
char spaces;
int top;
};
#endif
Code:
#include <iostream>
#include "garage.h"
using namespace std;
GarageStack::GarageStack()
{
top= -1;
}
bool GarageStack::IsEmpty()const
{
if(top ==-1)
{
return true;
}
return false;
}
bool GarageStack::IsFull()const
{
if (top== MAX_CARS-1)
{
return true;
}
return false;
}
void GarageStack::Push(char AorD, char tag[], int milhours, int milmin, char AorP[])
{
if(!IsFull())
{
top++;
spaces[top].arrival= AorD;
for (int i=0; i<6; i++)
spaces[top].tag[i] = tag[i];
spaces[top].hours = milhours;
spaces[top].minutes = milmin;
for (int k=0; k<2; k++)
spaces[top].ampm[k]= AorP[k];
cout<<"Car with license number: ";
int i=0;
while(i<6 && spaces[top].tag[i]!='\0')
{
cout<<spaces[top].tag[i];
i++;
}
spaces[top].moved=0;
cout<< "arriving at: "<<spaces[top].hours<<":";
(spaces[top].minutes<10)?cout<<'0'<<spaces[top].minutes:cout<<spaces[top].minutes;
cout<<" Car is Parked "<<endl<<endl;
}
else
{
cout<<" Car with license number: ";
for (int i=0; i<6; i++)
cout<<tag[i];
cout<<"\nNo Room Available"<<endl<<endl;
}
}
void GarageStack::Pop(char AorD, char tag[], int milhours, int milmin, char AorP[])
{
if(!IsEmpty())
{
bool match=false;
int i= top;
while(!match && i>0)
{
match = true;
int j=0;
while (match && j<6)
{
if (spaces[i].tag[j] !=tag[j])
{
spaces[i].moved++;
match=flase;
}
j++;
}
if (!match)
i--;
}
if (match)
{
int diffhours;
int diffminutes;
if(AorP[0]=='P' && Hr !=12)
Hr+=12;
if (spaces[i].ampm[0]=='P' && soaces[i].hours !=12)
spaces[i].hours+=12;
diffhours = Hr - spaces[i].hours;
diffminutes = Min - spaces[i].minutes;
if(diffminutes < 0 )
{
diffhours = diffhours - 1;
diffminutes = diffminutes = 60;
}
cout<< "Car with license number: ";
int i=0;
while ( i<10 && spaces[i].tag[i]!='\0')
{
cout<<spaces[i].tag[i];
i++;
}
cout<<" departing at: "<<spaces[top].hours<<":";
(spaces[top].minutes)<10?cout<<'0'<<spaces[top].minutes:cout<<spaces[top].minutes;
cout<<endl;
cout<<"Time parked: "<<diffhours<<" Hours and "<<diffminutes<<" minutes "<<endl;
cout<<"Time the car was moved: "<<spaces[i].numx<<endl;
int z;
cout<<endl;
for (z = top; z>=i; z--)
{
tempspaces[top-z] = spaces[z];
}
for (z = i; z<=(top-1); z++)
spaces[z] = tempspaces[top-1-z];
top = top -1;
}
else
cout<<"There is no such car."<<endl;
}
else
cout<<"The garage is Empty"<<endl;
}
Code:
#include <iostream>
#include <fstream>
#include "garage.h"
#include <stack>
using namespace std;
const int MAX = 6;
void main()
{
ifstream infile;
GarageStack stack;
char AorD;
char AorP[2];
char tag[MAX_CARS];
int hour;
int min;
infile.open ("garagepark.txt");
if (infile.fail ())
{
cout << "Inut file open failed." <<endl;
exit(1);
}
infile >> AorD>>tag>>hour>>min>>AorP;
while (infile)
{
if (AorD == 'A')
stack.Push(AorD,tag,hour,min,AorP);
else if (AorD == 'D')
stack.Pop(AorD,tag,hour,min,AorP);
infile >>AorD>>tag>>hour>>min>>AorP;
}
infile.close();
}
need help for space[top], how should I call it from my h file.
thanks