数据结构 实验五 停车场管理

实验五 停车场管理

1、实验目的:
(1)掌握栈的使用方法。
(2)掌握队列的使用方法。

2、实验环境与设备:
已安装Visual Studio 2010(或其以上版本)集成开发环境的计算机。
3、实验原理:
(1)使用队列遵循“先来后到”规则的排队停车。
(2)利用栈实现后进先出的进出停车场移车操作。

4、实验内容:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
5、实验考核:
(1)完成纸质版实验报告
(2)提交电子版作业

6、执行结果示例如下:
在这里插入图片描述
成果展示

#include <iostream>
#include <stdio.h>
#include <math.h>
#include <algorithm>
#include <string.h>
#include <sstream>
#include <stdlib.h>
#include <malloc.h>

using namespace std;

struct BUS
{
	int num;
	int intime;
	int outtime;
}bus[10000], buss[1000];

struct seq//停车场
{
	int bus[10000];
	int top;
};

struct seqq
{
	int buss[1000];
	int topp;
};

struct Biandao
{
	int data;
	int time;
	int num;
	struct Biandao *next;
};

struct biandao
{
	Biandao *first;
	Biandao *rear;
}biandao;

int main()
{
	printf("温馨提示:本停车场每十分钟收费0.50元,不足10分钟的部分按10分钟收费。\n");
	int x, t;
	int n;
	printf("请输入停车场车位数量n:");
	cin >> n;

	char a[5];

	seq s;
	s.top = 0;
	seqq ss;
	ss.topp = 0;
	biandao.first = NULL;
	biandao.rear = NULL;
	int num = 0;
	printf("请输入停车信息(格式(动作,车牌号,时间)):\n");
	while (cin >> a)
	{
		if (!strcmp(a, "A"))
		{
			cin >> x >> t;
			if (s.top >= n)//如果停车场已满
			{
				Biandao *p;
				p = (Biandao *)malloc(sizeof(Biandao));
				p->data = x;
				p->time = t;
				num++;
				p->next = NULL;

				if (biandao.rear != NULL)
					biandao.rear->next = p;
				biandao.rear = p;

				if (biandao.first == NULL)
					biandao.first = p;
				printf("\n时间为第 %d 分钟时,车牌号为%d的车进入便道等待车位\n", t, x);
				printf("车库已满,该车在便道的第 %d 位\n", num);

			}
			else//停车场未满
			{
				bus[s.top].num = x;
				bus[s.top].intime = t;
				printf("\n时间为第%d分钟时,车牌号为%d的车进入停车场 %d 号车位\n\n",t,x,s.top+1);
				s.top++;
			}
		}
		else if (!strcmp(a, "D"))
		{
			cin >> x >> t;
			if (s.top == 0)
			{
				printf("输入错误 重新输入\n");
			}
			else
			{
				int nu;
				for (int i = 0; i <= s.top; i++)
				{
					if (bus[i].num == x)
					{
						nu = i;
					}
				}
				bus[nu].outtime = t;
				printf("\n时间为第%d分钟时,车牌号为 %d 的车离开停车场\n", bus[nu].outtime,bus->num);
				printf("该车在停车场停留时间为 %d , 应该收费 %.2f 元 \n\n", bus[nu].outtime - bus[nu].intime, (int((bus[nu].outtime - bus[nu].intime - 1) / 10.0) + 1.0)*0.5);

				for (int i = nu; i <= s.top; i++)//出停车站
				{
					bus[i] = bus[i + 1];
				}

				s.top--;

				if (biandao.first != NULL)//如果便道上有车
				{
					bus[s.top].num = biandao.first->data;
					bus[s.top].intime = t;
					printf("\n时间为第 %d 分钟时,车牌号为%d的车进入停车场 %d 号车位\n", t, bus[s.top].num, s.top + 1);
					s.top++;
					num--;
					Biandao *q;
					q = biandao.first;
					biandao.first = biandao.first->next;
					free(q);
				}
			}
		}
		else if (!strcmp(a, "E"))
		{
			break;
		}
		else
		{
			printf("输入有误 重新输入\n");
		}
	}
	return 0;
}


在这里插入图片描述

发布了15 篇原创文章 · 获赞 0 · 访问量 240

猜你喜欢

转载自blog.csdn.net/qq_44796882/article/details/105464688