diluan6349
diluan6349
2017-06-04 06:18

C++的一个问题,求助大神

  • c++

设停车场内只有一个可停放n辆汽车的狭长通道,且只有一个大门可供汽车进出。汽车在停车场内按车辆到达时间的先后顺序,依次由北向南排列(大门在最南端,最先到达的第一辆车停放在车场的最北端),若车场内已停满n辆汽车,则后来的汽车只能在门外的便道上等候,一旦有车开走,则排在便道上的第一辆车即可开入;当停车场内某辆车要离开时,在它之后开入的车辆必须先退出车场为它让路,待该辆车开出大门外,其它车辆再按原次序进入车场,每辆停放在车场的车在它离开停车场时必须按它停留的时间长短交纳费用。试为停车场编制按上述要求进行管理的模拟程序。
template
class Queue//循环队列
{
public:
Queue()
{//构造空队列
maxSize = MAX_PASS_SIZE + 1;
queue = new T[maxSize];
front = rear = 0;
}
~Queue() { delete[]queue; }
bool isEmpty()
{//判断队列是否为空
return front == rear;
}
bool isFull()
{//判断队列是否满
if ((rear + 1) % maxSize == front) return true;
else return false;
}
T first()
{//返回对首元素
if (isEmpty())cout << "队列为空" << endl;
else return queue[(front + 1) % maxSize];

}
T last()
{//返回队尾元素
if (isEmpty())cout << "队列为空" << endl;
else return queue[rear];
}
Queue&push(T &x)
{//入队
if (isFull()) cout << "队列已满" << endl;
else
{
queue[rear] = x;
rear = (rear + 1) % maxSize;

}return *this;
}
Queue&pop(T &x)
{//出队
if (isEmpty())cout << "队列为空" << endl;
else
{
x = queue[front];
front = (front + 1) % maxSize;

}return *this;
}
//private:
int front;//队首
int rear;//队尾
int maxSize;//队列大小
T*queue;
};

template
class Stack
{
public:
Stack()
{//构造空栈
maxTop = MAX_PARKING_SIZE - 1;
stack = new T[MAX_PARKING_SIZE];
top = -1;
}
~Stack() { delete[]stack; }
bool isEmpty()
{//判断栈是否为空
return top == -1;
}
bool isFull()
{//判断栈是否满
return top == maxTop;
}
T getTop()
{//返回栈顶元素
if (isEmpty()) cout << "栈为空" << endl;
else return stack[top];
}
Stack&push(T &x)
{//入栈
if (isFull()) cout << "栈已满" << endl;
else
{
top++;
stack[top] = x;
}
return *this;
}
Stack&pop(T &x)
{//出栈
if (isEmpty())cout << "栈为空" << endl;
else
{
x = stack[top];
top--;

}
return *this;
}
//private:
int top;//栈顶
int maxTop;//最大栈顶值
T*stack;//动态数组存储元素
};

#include
#include
#include"stack.h"
#include"queue.h"
using namespace std;
const int MAX_PARKING_SIZE = 5;
const int MAX_PASS_SIZE = 5;
class Car
{
public:

void createArriveCar()
{
cout << "请输入车牌号:";
cin >> carNumber;
status = true;
cout << "请输入到达的时间:";
cin >> hour >> minute;
}
void createLeaveCar()
{
cout << "请输入车牌号:";
cin >> carNumber;
status = false;
cout << "请输入离开的时间:";
cin >> hour >> minute;
}
void print()
{
if (status)
cout << "车牌号:" << carNumber << " 到达时间" << hour << ":" << minute << endl;
else cout << "空" << endl;

}
string carNumber;//车牌号
int hour;
int minute;//到达的时间
bool status = false;//true代表进入停车场,false代表出停车场

};
class Parking
{
public:
void carArrive(Car car)
{
if (!parkingStack.isFull())
{
parkingStack.push(car);
cout << "车辆进入停车场,在停车场的位置为:" << parkingStack.top + 1 << endl;
}
else if (parkingQueue.isFull())
cout << "停车场和通道中都没有位置" << endl;
else if (parkingStack.isFull())
{
parkingQueue.push(car);
cout << "停车场已满,车辆进入等待通道,在通道中的位置为:" << parkingQueue.rear << endl;
}
}
void carLeave(Car car)
{
Car leaveCar;
Car temporaryCar;
int time;//以分钟记的停留时间
double money;//收费
Stack temporaryStack; //临时栈
while (car.carNumber != parkingStack.getTop().carNumber)
{
parkingStack.pop(leaveCar);
temporaryStack.push(leaveCar);
if (parkingStack.isEmpty())
{
cout << "车站中没有这辆车" << endl;
break;
}

}
if (!parkingStack.isEmpty())
{
parkingStack.pop(leaveCar);
time = (car.hour - leaveCar.hour) * 60 + (car.minute - leaveCar.minute);
money = time * 1;
cout << "离开的车辆在停车场里停留了" << time << "分钟,需付费用:" << money << endl;
}

while (!temporaryStack.isEmpty())
{
temporaryStack.pop(temporaryCar);
parkingStack.push(temporaryCar);
}

while (!parkingStack.isFull() && !parkingQueue.isEmpty())//在通道中的车辆进入停车场,时间改为进入的时间
{
parkingQueue.pop(temporaryCar);
temporaryCar.hour = car.hour;
temporaryCar.minute = car.minute;
parkingStack.push(temporaryCar);
}

}
void listshow()
{
for (int i = 0; i<MAX_PARKING_SIZE; i++)
{

cout << "第" << i + 1 << "号停车位:";
parkingStack.stack[i].print();

}
}
private:
Stack parkingStack;
Queue parkingQueue;
};
int main()
{
Parking parking;
int option;
while(1)
{
try {
cout << "欢迎使用本停车管理系统" << endl;
cout << "1. 车辆到达,2. 车辆离开,3. 列表显示,4.退出系统" << endl;
cin >> option;
switch (option)
{
case 1:
{
Car aParkingCar;
aParkingCar.createArriveCar();
parking.carArrive(aParkingCar);
break;
}
case 2:
{
Car aLeaveCar;
aLeaveCar.createLeaveCar();
parking.carLeave(aLeaveCar);
break;
}
case 3:
{
parking.listshow();
break;
}
case 4:return 0;
default:
{
cout << "命令错误" << endl;
break;
}
}
}
catch (exception e) {
break;
}
}
}

我进入2辆车然后把第1辆,删了结果输出的时候 ,第2辆车占了2个位置

图片说明.csdn.net/upload/201706/04/1496557088_650510.png)

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

2条回答