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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
|
#include <iostream>
#include <fstream>
#include <string>
#include <cassert>
using namespace std;
struct car
{
char code;
string license;
int hour;
int min;
};
class stack
{
public:
static const int CAPACITY = 6;
typedef int STACK_TYPE;
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];
};
stack::stack()
{
top=-1;//nothing on stack initially
}
void stack::push(STACK_TYPE value)
{
assert(top !=CAPACITY-1);
top++;
items[top]=value;
}
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, int &b, int &c) //prints arriving car's license & time
{
cout<<"car has been parked in the garage."<<endl;
cout<<"license plate number: "<<a <<endl;
cout<<"current time:" << b<<":";
if (c>=10) cout<<c<<endl;
else cout<<"0"<<c<<endl;
cout <<"*********************"<<endl;
}
void departing(string &a, int&b, int &c)//prints departing car's license & time
{
cout<<"License: "<<a<<endl;
cout<<"Time of departure: " <<b<<":";
if (c>=10) cout<<c<<endl;
else cout<<"0"<<c<<endl;
}
void time_parked(int &a, int &b, int &c, int &d)//prints time parked & charges
{
int hour_diff;
int min_diff;
double charges;
if (d<b)//if departing minutes is less than arriving minutes
{
c--;
}
hour_diff = c-a;
min_diff = d-b;
charges = 7.5*2*hour_diff;
if (min_diff>30)
{
charges = charges + 7.5*2;
}
else if (min_diff>0)
{
charges = charges + 7.5;
}
cout<<"Time Parked: "<<hour_diff<<":";
if (min_diff>=10) cout<<min_diff<<endl;
else cout<<"0"<<min_diff<<endl;
cout<<"Total Charges: $"<<charges<<endl;
}
void print_garage();
int main ()
{
stack garage;
stack street;
car cars[100];
car temp;
int new_hour;
int new_min;
int x=0;
fstream textfile;
textfile.open("data4.txt");
textfile>>cars[x].code;
while(!textfile.eof())
{
textfile>> cars[x].license;
if (cars[x].code =="A")
{
if (garage.is_full())
{
cout<<"Garage is full."<<endl;
cout<<"License: "<<license<<endl;
}
else
{
textfile>>cars[x].hour;
textfile>>cars[x].min;
arriving(cars[x].license, cars[x].hour, cars[x].min);
garage.push(cars[x]);
}
}
if (cars[x].code=="D")
{
textfile>>new_hour;
textfile>>new_min;
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, new_hour, new_min);
time_parked(cars[x].hour, cars[x].min, new_hour, new_min);
street.pop();
}
}
while(!street.is_empty())
{
temp = street.pop();
garage.push(temp);
}
print_garage();
}
}
system("pause");
}
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);
}
}
|