題目不貼了,直接上代碼
/**
* 數據結構與算法討論課二 停車管理系統
* 作者:周萌
* 博客:https://blog.csdn.net/zhoumeng1998
* GitHub:https://github.com/ZhouMeng666
*/
#include<iostream>
#define MAXNUM 5000
using namespace std;
/**
* 異常說明
* 0:不能對空隊列/棧進行出隊/出棧
* 1:停車場已滿,不能停車
* 2:停車場中無該車
*/
//全局變量
int n, m, t; //停車場大小n,每小時車費m,當前時間t
//車輛類
class Car {
public:
int number;
int time;
Car() {
number = 0;
time = 0;
}
Car(int number) {
this->number = number;
}
Car(const Car &a) {
number = a.number;
time=a.time;
}
void setNumber(int number) {
this->number = number;
}
void setTime(int time) {
this->time = time;
}
int getMoney(int endTime) {
return (endTime - time)*m;
}
};
//節點類
class Node {
public:
Car data;
Node *next;
Node(){
next = NULL;
}
};
//隊列類
class Queue {
protected:
Node * head, *tail;
public:
Queue() {
head = new Node;
tail = head;
}
bool isInThis(int num) {
Node *p = head->next;
while (p != NULL) {
if (num == p->data.number) {
return true;
}
p = p->next;
}
return false;
}
bool isEmpty() {
return head == tail;
}
void push(Car a) {
Node *p;
p = new Node;
p->data = a;
tail->next = p;
tail = p;
}
Car pop() {
if (isEmpty()) {
throw 0; //隊列爲空不可出隊
}
else
{
Node *p;
p = head;
head = head->next;
delete p;
return head->data;
}
}
Car getHead() {
return head->next->data;
}
};
//棧類
class Stack {
protected:
Node *top;
public:
Stack() {
top = new Node;
}
bool isInThis(int num) {
Node *p;
p = top->next;
while (p)
{
if (num == p->data.number) {
return true;
}
p = p->next;
}
return false;
}
bool isEmpty() {
return top->next == NULL;
}
void push(Car a) {
Node *p;
p = new Node();
p->data = a;
p->next = top->next;
top->next = p;
}
Car pop() {
if (isEmpty()) {
throw 0;
}
else{
Node *p;
p = top->next;
top->next = p->next;
Car a = p->data;
delete p;
return a;
}
}
Car getTop() {
return top->next->data;
}
void show() {
Node *p = top->next;
while (p) {
cout << p->data.number << endl;
p = p->next;
}
}
};
//停車場類
class Park:public Stack {
protected:
int cars;
public:
Park() :Stack() {
cars=0;
cout <<cars<< "停車場創建\n";
}
bool isFull() {
return cars >= n;
}
void enterPark(int num) {
Car a(num);
a.setTime(t);
push(a);
cars+=1;
cout << "車輛" << a.number << "於時間" << t<<" test "<<cars<< "進入停車場\n";
}
Car exitPark() {
Car a = pop();
cars-=1;
int money = a.getMoney(t);
cout << "車輛" << a.number << "於時間" << t << "離開停車場,付費"<<money<<"元\n";
return a;
}
void remove(int num) { //臨時停車場在停車場外
Stack s;
while (!isEmpty() && num != getTop().number) {
Car a = pop();
s.push(a);
}
exitPark();
while (!s.isEmpty())
{
push(s.pop());
}
}
};
//便道類
class Road :public Queue {
public:
Road() :Queue() {
cout << "便道創建\n";
}
void enterRoad(int num) {
Car a(num);
cout << "車輛" << a.number << "於時間" << t << "進入便道\n";
push(a);
}
int exitRoad() {
Car a = pop();
cout << "車輛" << a.number << "於時間" << t << "離開便道\n";
return a.number;
}
void remove(int num) {
while (num != getHead().number) {
Car a = pop();
push(a);
}
exitRoad();
}
};
Park P;
Road R;
void enter() {
int number, time;
cin >> number >> time;
if (P.isInThis(number) || R.isInThis(number)) {
cout << "該車已在停車場或便道內,請確認輸入";
return;
}
t = time;
if (!P.isFull()) {
P.enterPark(number);
}
else {
R.enterRoad(number);
}
}
void exit() {
int number, time;
cin >> number >> time;
if ((!P.isInThis(number)) && (!R.isInThis(number))) {
cout << "該車不在停車場或便道內,請確認輸入";
return;
}
t = time;
if (P.isInThis(number)) {
if (P.isFull()) {
P.remove(number);
P.enterPark(R.exitRoad());
}
else {
P.remove(number);
}
}
else
{
R.remove(number);
}
}
int main() {
cout << "請輸入停車場大小\n";
cin >> n;
cout << "請輸入每小時停車費\n";
cin >> m;
while (1) {
int x;
cout << "請輸入所需操作(輸入0打開幫助):\n";
cin >> x;
switch (x)
{
default:
break;
case 0:
cout << "車輛到達:請輸入 1 車牌號 當前時間\n";
cout << "車輛離開:請輸入 2 車牌號 當前時間\n";
cout << "查看停車場:請輸入 3\n";
cout << "結束測試:請輸入 4\n";
break;
case 1:
enter();
break;
case 2:
exit();
break;
case 3:
P.show();
return 0;
break;
case 4:
cout << "測試結束\n";
system("pause");
return 0;
break;
}
6};