这不是典型的栈的吗?照着那个思路做不就行了
Way.h
const int SIZE = 20;
const int INCREMENT = 10;
struct Car
{ int NO;
int time;
};
//队列
template <class T>
class Queue
{
private:
T *base;
int max_size;
int head,tail;
public:
Queue();
~Queue();
void InQue(T elem);
T* OutQue();
};
//栈
template <class U>
class Stack
{
private:
U *base;
int max_size;
int top;
public:
Stack(){;}
~Stack(){;}
void Set(int n);
void Pop();
void Push(U elem);
bool IsEmpty();
bool IsFull();
U* GetTop();
};
//Park
class Park
{
private:
Car car;
Stack<Car> ParkedCar;
Stack<Car> OutCar;
Queue<Car> Road;
int n,Mon;
public:
Park(){;}
bool Search();
void manage();
void PrintBill(Car &CurCar);
void Init();
void ParkIn();
};
Park.cpp
#include <iostream>
#include "Way.h"
using namespace std;
//Park
void Park::Init()
{
cout << "请输入车站最大停车数和每单位时间应收费:" <<endl;
cin >> n >> Mon;
ParkedCar.Set(n);
OutCar.Set(n);
}
void Park::manage()
{
cout << "请输入数据:" <<endl;
char Inf;
Car *temp;
do
{
cin >> Inf >> car.NO >> car.time;
switch (Inf)
{
case 'A' : ParkIn();
break;
case 'D' : if(!Search()) continue;
PrintBill(*ParkedCar.GetTop());
ParkedCar.Pop();
while(!ParkedCar.IsFull())
{
if(!OutCar.IsEmpty())
{
temp = OutCar.GetTop();
OutCar.Pop();
ParkedCar.Push(*temp);
}
else
{
temp = Road.OutQue();
ParkedCar.Push(*temp);
}
}
break;
case 'E' : cout << endl;
}
}while(Inf != 'E');
}
void Park::PrintBill(Car &CurCar)
{
int TimeDif,Money;
TimeDif = car.time-CurCar.time;
Money = Mon*TimeDif;
cout << "该车停留的时间和应付的费用为:" <<TimeDif << '\t' << Money << '\n';
}
void Park::ParkIn()
{
int Number;
if(!ParkedCar.IsFull())
{
ParkedCar.Push(car);
Number = car.NO;
cout << "该车停在车库的第" << Number << "位" <<endl;
}
else
{
Road.InQue(car);
Number = car.NO;
cout << "该车停在候车道的第" << Number <<"位" <<endl;
}
}
bool Park::Search()
{
Car *temp;
if(ParkedCar.IsEmpty())
{
cout << "车库已空" <<endl;
return false;
}
while(car.NO != ParkedCar.GetTop()->NO && !ParkedCar.IsEmpty())
{
temp = ParkedCar.GetTop();
ParkedCar.Pop();
OutCar.Push(*temp);
}
if(ParkedCar.IsEmpty())
{
cout << "此车不在车库。" <<endl;
while(!OutCar.IsEmpty())
{
temp = OutCar.GetTop();
OutCar.Pop();
ParkedCar.Push(*temp);
}
return false;
}
return true;
}
//Queue
template <class T>
Queue<T>::Queue()
{
base = new T [SIZE];
max_size = SIZE;
head=tail=0;
}
template <class T>
Queue<T>::~Queue()
{
delete []base;
}
template <class T>
void Queue<T>::InQue(T elem)
{
if(tail==max_size)
{
T *temp = new T [max_size];
int i;
for(i=0;i<max_size;i++)
temp[i] = base[i];
delete []base;
base = new T [max_size + INCREMENT];
for(i=0;i<max_size;i++)
base[i] = temp[i];
delete []temp;
max_size += INCREMENT;
}
base[tail++]=elem;
}
template <class T>
T *Queue<T>::OutQue()
{
T *temp = &base[head++];
return temp;
}
//Stack
template <class U>
void Stack<U>::Set(int n)
{
max_size = n;
base = new U[max_size+1];
top = 0;
}
template <class U>
void Stack<U>::Pop()
{
if(top>0)top--;
else cout<<"The Stack is Empty!"<<endl;
}
template <class U>
void Stack<U>::Push(U elem)
{
if(top==max_size)
cout<<"The Stack is Full!"<<endl;
else base[++top] = elem;
}
template <class U>
U *Stack<U>::GetTop()
{
U *temp = &base[top];
return temp;
}
template <class U>
bool Stack<U>::IsEmpty()
{
return top==0;
}
template <class U>
bool Stack<U>::IsFull()
{
return top==max_size;
}
//main
int main()
{
Park park;
park.Init();
park.manage();
return 0;
}