📄 car management.cpp
字号:
/* 停车场管理系统 */
#include <string.h>
#include <stdio.h>
#include <malloc.h>
#define MAXI 3 /* 停车场最大容量*/
#define MAXO 3 /* 便道最大容量*/
#define mperh 5 /* 每小时小车停车价格,客车为其两倍,卡车为其三倍*/
/* 5吨以下为小车,5-10吨为客车,10吨以上为卡车*/
typedef struct car
{
char num[7];
int hour;
int min;
int ton;
}carnode,*nodeptr;
carnode stor[MAXI+1]; /* 单向停车场栈数组*/
carnode outway[MAXO+1];
int top1=0,top2=MAXI,base1=-1,base2=MAXI+1;
int front=0,rear=0;
void push(nodeptr p)
{
if(top1>MAXI-1)
{
if((rear+1)%(MAXO+1)==front)
{
printf("\n停车场和便道上均已无空位!");
getchar();
return;
}
strcpy(outway[rear].num,p->num);
outway[rear].hour=p->hour;
outway[rear].min=p->min;
outway[rear].ton=p->ton;
rear=(rear+1)%(MAXO+1);
printf("\n由于停车场已满,汽车暂停门口便道!!");
if((rear+1)%(MAXO+1)==front)
printf("\n注意! 现在门口便道已无空位!");
getchar();
}
else
{
strcpy(stor[top1].num,p->num);
stor[top1].hour=p->hour;
stor[top1].min=p->min;
stor[top1].ton=p->ton;
top1++;
printf("\n车辆 %s 已经成功进入停车场.",p->num);
if(top1==MAXI)
printf("\n 注意! 现在停车场已经停满车辆.");
getchar();
}
}
void popush(int x,int y)
{
if(y==1)
{
for(--top1;top1>x;top1--,top2--)
{
strcpy(stor[top2].num,stor[top1].num);
stor[top2].hour=stor[top1].hour;
stor[top2].min=stor[top1].min;
stor[top2].ton=stor[top1].ton;
strcpy(stor[top1].num," ");
stor[top1].hour=NULL;
stor[top1].min=NULL;
stor[top1].ton=NULL;
}
top1=top1+1;
}
if(y==2)
{
for(++top2;top2<x&&top2!=base2;top1++,top2++)
{
strcpy(stor[top1].num,stor[top2].num);
stor[top1].hour=stor[top2].hour;
stor[top1].min=stor[top2].min;
stor[top1].ton=stor[top2].ton;
strcpy(stor[top2].num," ");
stor[top2].hour=NULL;
stor[top2].min=NULL;
stor[top2].ton=NULL;
}
top2=top2-1;
}
}
void printcost(int n,int h,int m)
{
float price=0,time;
if(h>=stor[n].hour)
time=double ((h-stor[n].hour)*60.0-stor[n].min+m)/60.0;
else
time=double ((24-stor[n].hour+h)*60-stor[n].min+m)/60.0;
if(stor[n].ton<=5)
price=mperh*time;
if(stor[n].ton>5&&stor[n].ton<=10)
price=mperh*2*time;
if(stor[n].ton>10)
price=mperh*3*time;
printf("\n 离 去 的 车 为: %s",stor[n].num);
printf("\n 到达及离开时间: From %d:%d To %d:%d",stor[n].hour,stor[n].min,h,m);
printf("\n 车 的 吨 位 为: %d",stor[n].ton);
printf("\n 停车应付费用为: %5.2f",price);
strcpy(stor[n].num," ");
stor[n].hour=NULL;
stor[n].min=NULL;
stor[n].ton=NULL;
top1--;
printf("\n 恭喜!成功离开停车场!!");
getchar();
}
int find(nodeptr x,int *y)
{
int i,j;
for(i=0;i<MAXI+1;i++)
{
j=strcmp(x->num,stor[i].num);
if(j==0)
{*y=i;return j;}
}
for(i=0;i<MAXO+1;i++)
{
j=strcmp(x->num,outway[i].num);
if(j==0)
{*y=i;return j+1;}
}
printf("\n停车场内以及门口便道上没有这辆车!!");
getchar();
return 2;
}
void deletecar(nodeptr s,int i,int j)
{
int x,y;
x=find(s,&y);
if(x==2)
return;
if(x==0)
{
popush(y,1);
printcost(y,i,j);
popush(base2,2);
if(front!=rear)
{
strcpy(stor[top1].num,outway[front].num);
stor[top1].hour=i;
stor[top1].min=j;
stor[top1].ton=outway[front].ton;
top1++;
strcpy(outway[front].num," ");
outway[front].hour=NULL;
outway[front].min=NULL;
outway[front].ton=NULL;
front=(front+1)%(MAXO+1);
}
}
if(x==1)
{
while(front!=y)
{
strcpy(outway[rear].num,outway[front].num);
outway[rear].hour=outway[front].hour;
outway[rear].min=outway[front].min;
outway[rear].ton=outway[front].ton;
strcpy(outway[front].num," ");
outway[front].hour=NULL;
outway[front].min=NULL;
outway[front].ton=NULL;
front=(front+1)%(MAXO+1);
rear=(rear+1)%(MAXO+1);
}
printf("\n 离 去 的 车 为: %s",outway[front].num);
printf("\n 到达及离开时间: From %d:%d To %d:%d",outway[front].hour,outway[front].min,i,j);
printf("\n 车 的 吨 位 为: %d",outway[front].ton);
printf("\n 该车停在便道上,暂不收费.");
strcpy(outway[front].num," ");
outway[front].hour=NULL;
outway[front].min=NULL;
outway[front].ton=NULL;
front=(front+1)%(MAXO+1);
getchar();
}
}
void manage()
{
char x;
carnode temp;
printf("\n======================= 车辆调度模拟程序==========================");
printf("\n================ 事件说明:A->到达 D->离开 ,E->结束==============");
printf("\n===================================================================");
while(1)
{
printf("\n\n===============> 输入命令:");
x=getchar();
if(x=='a'||x=='A'||x=='d'||x=='D'||x=='e'||x=='E')
{
printf("%c",x);
if(x=='e'||x=='E')
{
break;
}
if(x=='a'||x=='A')
{
printf("\n\n============= 车辆到达 ===============\n");
while(1)
{
printf("\n请 输 入 车 牌 :");
scanf("%s",temp.num);
if(strlen(temp.num)>7)
printf("\n输入字符串过长!!");
else
break;
}
while(1)
{
printf("\n请输入到达时间 (HH:MM):");
scanf("%d:%d",&temp.hour,&temp.min);
if(temp.hour>23||temp.hour<0||temp.min>60||temp.min<0)
printf("\n时间不符合实际!!");
else
break;
}
printf("\n请输入进场车辆的吨位:");
scanf("%d",&temp.ton);
push(&temp);
}
if(x=='d'||x=='D')
{
printf("\n\n============= 车辆离开 ===============\n");
while(1)
{
printf("\n请 输 入 车 牌 :");
scanf("%s",temp.num);
if(strlen(temp.num)>7)
printf("\n输入字符串过长!!");
else
break;
}
while(1)
{
printf("\n请输入离开时间(HH:MM):");
scanf("%d:%d",&temp.hour,&temp.min);
if(temp.hour>23||temp.hour<0||temp.min>60||temp.min<0)
printf("\n时间不符合实际!!");
else
break;
}
deletecar(&temp,temp.hour,temp.min);
}
}
else
{
printf("\n===============> 命令错误!!");
}
}
}
void check()
{
int i,j;
i=0;j=1;
printf("\n\n============ 停车场内车辆停放情况 ==============\n");
printf("\n车位 *** 北端 *** ----->到达时间 吨位");
for(i=0,j=1;i<top1;i++,j++)
{
printf("\n %d * %7s * -----> %2d:%2d %2d",j,stor[i].num,stor[i].hour,stor[i].min,stor[i].ton);
printf("\n *************");
}
for(;j<=MAXI;j++)
{
printf("\n %d * X *",j);
printf("\n *************");
}
printf("\n * 入口 *");
printf("\n * *");
printf("\n 按任意键继续查看门口情况");
getchar();
printf("\n\n============ 停车场外便道上车辆停放情况 ============\n");
printf("\n * *");
printf("\n * 出口 *");
printf("\n车位 ************* ----->到达时间 吨位");
for(i=front,j=1;i!=rear;i=(i+1)%(MAXO+1),j++)
{
printf("\n %d * %7s * -----> %2d:%2d %2d",j,outway[i].num,outway[i].hour,outway[i].min,outway[i].ton);
printf("\n *************");
}
for(;j<MAXO+1;j++)
{
printf("\n %d * X *",j);
printf("\n *************");
}
printf("\n * 入口 *");
printf("\n * *");
printf("\n 查看完毕!按任意键返回!");
getchar();
}
int main()
{
printf("\n\t Welcome to Geyuancheng's Car-parked Manage System!\n");
printf("\n 感谢您使用本停车场管理系统!\n");
char select;
while(1)
{
/*clrscr();*/
printf("\n\t *************************************************\n");
printf("\n\t *** 1 : 停车场车辆调度 ***\n");
printf("\n\t *** 2 : 查看车场子停车现况 ***\n");
printf("\n\t *** 3 : 退出程序 ***\n");
printf("\n\t *************************************************\n");
printf("\t 选择:");
while(1)
{
select=getchar();
if(select>51||select<49)
printf("\n输入错误!!!重新输入:");
else
break;
}
switch(select)
{
case '1' :manage();break;
case '2' :check();break;
case '3' :break;
}
if(select=='3')
break;
}
return 1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -