■问题描述: 设计一个停车场管理系统,模拟停车场的运作,此程序具备以下功能: (1)若车辆到达,则显示汽车在停车场内或者便道上的停车位置; (2)若车辆离去,则显示汽车在停车场内停留的时间和应缴纳的费用(在便 道上停留的时间不收费)
■基本要求: (1)要求以栈模拟停车场,以队列模拟车场外的便道,按照从终端读入和输 入数据序列进行模拟管理。
(2)要求处理的数据元素包括三个数据项:汽车“到达”或“离去”信息, 汽车牌照号码及到达或离去的时刻。
(3)要求栈以顺序结构实现,队列以链表实现。
■测试数据: 自行设计一组在停车场内停车的数据和在便道上停车的数据。
■实现提示: 该停车场容纳的车辆数量,便道上的停车数量。车辆到达对应栈的入栈操作, 车辆离开对应的出栈操作。
■选作内容: 栈的特性是先进后出,但实际停车场的车辆并不完全符合栈的规律。可从真实 模拟停车场管理的角度,巧妙应用栈和队列完善该系统。
代码如下:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include<stdlib.h>
#define MAXSIZE 100
//汽车的三项信息
typedef struct {
char ch;//车辆的识别符,到达or离去
int time;//车辆停车时间
char car[20];//车辆车牌号
int position;
}CarData;
//顺序栈实现停车场
typedef struct {
CarData data[MAXSIZE];//停车场最大可以停放100量汽车
int top;
}StopCar;
//链队实现排队进停车场
typedef struct WaitCar {
CarData data[MAXSIZE];//等候区最大可以停放100量汽车
struct WaitCar* next;
}WaitCar;
typedef struct {
WaitCar* front;
WaitCar* rear;
}WaitCarLink;
//顺序栈实现给临时出来的汽车让路
typedef struct {
CarData data[MAXSIZE];//临时停车最大可以停放100量汽车
int top;
}TemporaryCar;
void InitStopCar(StopCar*);//停车场初始化
int InitWaitCarLink(WaitCarLink*);//初始化等候区
void InitTemporaryCar(TemporaryCar*);//临时停车初始化
void ArrivalCar(StopCar*, WaitCarLink*, char, char*, int);//入库函数
void DepartureCar(StopCar*, char, char*, int);//离开函数
int IfTheCar(StopCar*, char*);//判断是否有输入的这辆车
void WaitToStop(StopCar*, WaitCarLink*,int time);//将便道上的车进入车库
int panduanrukrshunxu(StopCar* sc);//判断入库
StopCar sc;//停车场
WaitCarLink wcl;//等候区
WaitCar* wc;//创建节点指针
int n,h, cost, j = 0, k = 0,key,ye[20];//全局变量 n表示用户输入停车场停放的车 cost用户输入每小时停车的费用 j是便道的那个,k是定位,ye储存“0”
int a;
int main()
{
char car[20];
int time = 1, time1=0;// time存放时间 car 车牌号
char ch = 'A';//车的状态
InitStopCar(&sc);
InitWaitCarLink(&wcl);
system("color 9e");
printf(" 欢迎使用停车场管理系统\n");
printf("请输入停车场可以停放的最大车辆:");
scanf("%d", &n);
printf("便道可以停放的最大车辆:");
scanf("%d", &h);
printf("停车每小时收取的费用(元)");
scanf("%d", &cost);
getchar();
while (ch != 'E')
{
printf(" 停车场情况:\n");
for (int a = 0; a < n; a++)
{
printf(" 车牌为%s的车位置是%d\n", sc.data[a].car,sc.data[a].position);
}
printf("请输入车的状态(A进站,D出站,E退出程序):");
scanf("%c", &ch);//%c和%C不一样,ch值会变!!!
printf("车的车牌号:");
scanf("%s", &car);
printf("车的时间(进站时间,出站时间):");
if ((time1 >= 0) && (time1 <= 24))
{
time = time1;
scanf("%d", &time);
}
else
{
printf("无该时间,请重新输入:");
printf("再次输入车的时间(进站时间,出站时间):");
scanf("%d", &time);
}
getchar();
if (ch == 'A')
{
ArrivalCar(&sc, &wcl, ch, car, time);
}
else if (ch == 'D')
{
DepartureCar(&sc, ch, car, time);
WaitToStop(&sc, &wcl,time);//不需要判断车库有没有车,当执行出库的时候,车库已经有位置了
}
else
{
printf("请输入正确的编号,A是进库,D是出库,E是退出程序!\n");
}
}
return 0;
}
void WaitToStop(StopCar* sc, WaitCarLink* wcl,int time)//将便道上的车进入车库
{
if (key == 1)
{
if (wcl->front == wcl->rear)//便道没有车
printf("便道没有车!\n");
else {
//出队
WaitCar* S;
S = wcl->front->next;//S指向队头
wcl->front->next = S->next;//S指向的队头出队
j--;//便道车位少一个
if (wcl->rear == S)//如果S是最后一个元素,则让队尾指针指向队头,防止变成野指针
wcl->rear = wcl->front;
printf("%s出便道成功!\n", S->data[k].car);
//进栈
strcpy(sc->data[ye[k]].car, S->data[k].car);
sc->data[ye[k]].ch = S->data[k].ch;
sc->data[ye[k]].time = time;
S->data[k].position = sc->data[ye[k]].position;
printf("%s进库成功!\n", S->data[k].car);
k++;//便道下一个车
}
}
}
void DepartureCar(StopCar* sc, char ch, char* car, int time)//离开函数
{
TemporaryCar tc;//临时停车
InitTemporaryCar(&tc);
int timesum;//计算时间
if (sc->top == -1)
{
printf("车库没有车!无法离开\n"); key = 0;
}
else if (IfTheCar(sc, car)) {
printf("车库里没有此车!\n"); key = 0;
}
else
{
while (strcmp(sc->data[sc->top].car , car)!=0)//在此车前面的车,依次到临时停车区
{
tc.top++;
sc->top--;//注意是下移,先出
strcpy(tc.data[tc.top ].car, sc->data[sc->top+1].car);
tc.data[tc.top ].time = sc->data[sc->top+1].time;
}
timesum = time - sc->data[sc->top].time;
printf("车牌是%s在停车场停留的时间是%d,停车费用是%d\n", sc->data[sc->top].car, timesum, timesum * cost);
ye[k] = sc->top;
strcpy(sc->data[sc->top].car, "0");
//sc->top = sc->top - 1;
while (tc.top != -1)//临时停车区依次进入停车场
{
strcpy(sc->data[sc->top + 1].car, tc.data[tc.top].car);
sc->data[sc->top + 1].time = tc.data[tc.top].time;
tc.top--;
sc->top++;
}
key = 1;
}
}
void ArrivalCar(StopCar* sc, WaitCarLink* wcl, char ch, char *car, int time)//入库函数
{
wc = new WaitCar;
wc->next = NULL;
if (IfTheCar(sc, car) == 1)
{
if ((sc->top + 1) < n)//车库没有满
{
sc->data[sc->top + 1].ch = ch;
strcpy(sc->data[sc->top + 1].car, car);
sc->data[sc->top + 1].position = sc->top + 2;
sc->data[sc->top + 1].time = time;
printf("\n车牌号是%s的车停的位置是:%d\n", car, sc->data[sc->top + 1].position);
sc->top++;
}
else //满了,
{
a = panduanrukrshunxu(sc);
if (a!=-1)
{
sc->data[a].ch = ch;
strcpy(sc->data[a].car, car);
sc->data[a].time = time;
printf("\n车牌号是%s的车停的位置是:%d\n", sc->data[a].car, sc->data[a].position);
}
else if (j < h)//进便道
{
strcpy(wc->data[j].car, car);
wc->data[j].ch = ch;
wc->data[j].time = time;
wc->next = NULL;
wcl->rear->next = wc;
wcl->rear = wc;
wc->data[j].position = j + 1;
printf("\n车牌号是%s的车在便道上的位置是%d\n", car, wc->data[j].position);
j++;
}
else
{
printf("便道上位置满了\n");
}
}
}
else
{
printf("该车辆已进过车库!\n");
}
}
int IfTheCar(StopCar* sc, char *car)//判断是否有输入的这辆车
{
int i = 0;
while (i < sc->top + 1)
{
if (strcmp(sc->data[i].car , car)==0)
return 0;
i++;
}
return 1;
}
void InitStopCar(StopCar* sc)//停车场初始化
{
sc->top = -1;
}
int InitWaitCarLink(WaitCarLink* wcl) {//等候区初始化
wcl->front = new WaitCar;
if (wcl->front != NULL)
{
wcl->rear = wcl->front;
wcl->front->next = NULL;
return 0;
}
else
return 1;
}
void InitTemporaryCar(TemporaryCar* tc)//临时停车初始化
{
tc->top = -1;
}
int panduanrukrshunxu(StopCar* sc)
{
int i = 0;
while (i <= n)
{
if (strcmp(sc->data[i].car, "0") == 0)
return i;
i++;
}
return (-1);
}
以上代码有错误的话,欢迎评论或者私我解决