本文为本人在学校做的停车上管理系统作业,通过验收,具体实现功能,以及如何操作,可以看末尾的演示截图。验收时老师会重点关注,一些异常情况,我在片尾有列举,大家可以自行查看。
1.题目:停车场管理系统
2.背景:
随着时代的发展,汽车越来越多,停车场也随之增多,因此本实验设计一个停车场系统,来管理进入和离开的车辆并计算费用。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define max 3
#define cost 1
typedef struct{
char zt; //zt表示状态进入或者离开
float time; //停车时间
char cph[10]; //cph代表车牌号
}car;
typedef struct{ // 停车场的栈
car Car[max];
int top; // 标记栈顶位置
}tcc;
typedef struct bd{ //便道上的队列
car Car;
struct bd *next;
}bd;
typedef struct{
bd *front; //对头指针和队尾指针
bd *rear;
}bdLink;
typedef struct{ //实现让路的栈
car Car[max];
int top;
}rl;
void Inittcc(tcc *C) //停车场初始化
{
C->top=-1;
}
void InitbdLink(bdLink *bdl){ //便道初始化
bdl->front=new bd;
bdl->rear=bdl->front;
bdl->front->next=NULL;
}
void Initrl(rl *tc) //让路栈初始化
{
tc->top=-1;
}
void incar(tcc *C,bdLink *wc1,char zt,char* cph,float time){ //停车函数
bd *wc;
wc=new bd;
wc->next=NULL;
int i=C->top;
while(i!=-1){
if(0 == strcmp(C->Car[i].cph,cph)){ //判断停车场里是否已经存在此车
printf("输入有误,此汽车已存在!\n");
return;
}
i--;
}
if(C->top<max-1) //停车场没有满
{
C->Car[C->top+1].zt=zt;
strcpy(C->Car[C->top+1].cph,cph);
C->Car[C->top+1].time=time;
C->top++;
printf("\n车牌号是%s的车停的位置是:%d\n",cph,C->top+1);
}
else//满了,进便道
{
strcpy(wc->Car.cph,cph);
wc->Car.zt=zt;
wc->Car.time=time;
wc->next=NULL;
wc1->rear->next=wc;
wc1->rear=wc;
printf("车牌号是%s的车成功停在便道\n",cph);
}
}
int IfCar(tcc *C,char* cph) //判断停车场是否有这辆车
{
int i=0;
while(i<C->top+1)
{
if(0==strcmp(C->Car[i].cph,cph))
return 0;
i++;
}
return 1;
}
int ifbd(bdLink *wcl,char *cph){
bd *s;
s=new bd;
s=wcl->front;
while(s!=wcl->rear){
if(0==strcmp(s->Car.cph,cph)) return 0;
s=s->next;
}
if(0==strcmp(s->Car.cph,cph)) return 0;
else return 1;
}
void outcar(tcc *sc,char zt,char* cph,float time) //离开函数
{
rl tc; //让路栈
Initrl(&tc);
float timesum; //计算时间
if(sc->top==-1)
printf("停车场没有车!\n");
else
{
while(strcmp(sc->Car[sc->top].cph,cph)) //在此车后面的车,依次到临时停车区
{
strcpy(tc.Car[tc.top+1].cph,sc->Car[sc->top].cph);
tc.Car[tc.top+1].zt=sc->Car[sc->top].zt;
tc.Car[tc.top+1].time=sc->Car[sc->top].time;
tc.top++;
sc->top--; //注意是下移,先出
}
timesum=time-sc->Car[sc->top].time;
printf("车牌号%s,停车费用是%.3f\n",sc->Car[sc->top].cph,timesum*cost);
sc->top--; //将此车出栈
while(tc.top!=-1) //让路栈车依次进入停车场
{
strcpy(sc->Car[sc->top+1].cph,tc.Car[tc.top].cph);
sc->Car[sc->top+1].zt=tc.Car[tc.top].zt;
sc->Car[sc->top+1].time=tc.Car[tc.top].time;
tc.top--;
sc->top++;
}
}
}
void gototcc(tcc *sc,bdLink *wcl,float time) //将便道上的车进入停车场
{
if(wcl->front==wcl->rear) //便道没有车
printf("便道没有车!\n");
else{
//出队(便道)
bd *S;
S=wcl->front->next; //S指向队头
wcl->front->next=S->next; //S指向的队头出队
if(wcl->rear==S) wcl->rear=wcl->front; //如果S是最后一个元素,则让队尾指针指向队头
strcpy(sc->Car[sc->top+1].cph,S->Car.cph);
sc->Car[sc->top+1].zt=S->Car.zt;
sc->Car[sc->top+1].time=time;
sc->top++;
}
}
void outcar2(bdLink *wcl,char *cph){
bd *s,*d;
s=wcl->front;
while(strcmp(s->Car.cph,cph)){
d=s;
s=s->next;
}
d->next=s->next;
printf("成功离开便道,无费用\n");
}
void playtcc(tcc *sc,float time){
int i = sc->top;
int num=1;
float t;
if(-1 == i) printf("停车场目前为空\n");
while(i != -1){
t=time-sc->Car[i].time;
printf("车牌号%s停放在停车场第%d个,费用为%.3f\n",sc->Car[i].cph,i+1,t);
i--;
}
}
void playbd(bdLink *wcl){
bd *s;
s=wcl->front->next;
int i=1;
if(s==NULL) printf("便道上无车\n");
else
while(s){
printf("车牌号%s停在便道第%d位置上\n",s->Car.cph,i);
s=s->next;
i++;
}
}
int main()
{
char cph[10]; //cph车牌号
float time=1.0; //time存放时间
char zt; //车的状态
tcc sc; //停车场
Inittcc(&sc);
bdLink wcl; //便道
InitbdLink(&wcl);
bd *wc; //创建节点指针
printf("本停车场最多存放3台车,每小时收取的费用1元");
while(zt!='E'&&time!=0)
{
static float timemax=1.0;
fflush(stdin); //清除缓存区回车
printf("\n请依次输入 1A进 D出 S展示 2车牌号 3入/出的时间:");
scanf("%c %s %f",&zt,&cph,&time);
if(time>=timemax) timemax=time; //时间必须依次增大否则报错
else{
printf("输入时间存在问题\n");
continue;
}
if(zt=='A') incar(&sc,&wcl,zt,cph,time);
else if(zt=='D')
{
if(IfCar(&sc,cph)==1&&ifbd(&wcl,cph)==1) printf("无此车\n");
else if(IfCar(&sc,cph)==0){
outcar(&sc,zt,cph,time);
gototcc(&sc,&wcl,time);
}
else if(ifbd(&wcl,cph)==0) outcar2(&wcl,cph);
}
else if(zt=='S'){
playtcc(&sc,time);
playbd(&wcl);
}
else{
printf("输入错误!\n");
}
}
return 0;
}
结果展示:

时间存在问题如上图

将003出库 004入库,时间更新如上图

输入车辆已有提示错误如上图

输出车辆没有提示无此车如上图
感谢大家阅读,欢迎大家点赞评论,后续我会更新更多,计算机四大学科大作业相关内容,大家可以自行查看关注我的专栏,我会不定时更新!!!
3万+

被折叠的 条评论
为什么被折叠?



