数据结构与算法讨论课二 停车管理系统

题目不贴了,直接上代码

/**
*	数据结构与算法讨论课二 停车管理系统
*	作者:周萌
*	博客: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};

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
>