停车场管理系统源代码
- 格式:doc
- 大小:40.00 KB
- 文档页数:19
#include<stdio.h>#include<malloc.h>#define N 30 /*停车场类最多的停车数*/ #define M 20 /*便道内最多的停车数*/ #define price 2 /*每单位时间的停车费用*/ typedef struct{int carNo[N]; /*车牌号*/int carTime[N]; /*进场时间*/int top; /*栈指针*/}seqstack; /*定义顺栈占类型*/ typedef struct Node{int carNo[M]; /*车牌号*/struct Node *next;}linkQueue Node;typedef struct{LinkQueue Node *front; /*队首指针*/LinkQueue Node *rear; /*队尾指针*/}LinkQueue; /*定义链队类型*//*以下是顺序栈的基本运算算法*/void Initstack(seqstack *s){s=(seqstack*)malloc(sizeof(seqstack));s->top=-1;}int IsEmpt(seqstack *s){return(s->top==-1);}int IsFull(seqstack *s){return(s->top==N-1);}int Push(seqstack *s,int e1,int e2){if(s->top==N-1) return 0;s->top++;s->carNo[s->top]=e1;s->carTime[s->top]=e2;return 1;}int Pop(seqstack *s,int &e1,int &e2)if(s->top==-1)return 0;e1=s->carNo[s->top];e2=s->carTime[s->top];s->top--;return 1;}void Disqstack(seqstack *s){int i;for(i=s->top;i>=0;i--)printf("%d",s->carNo[i]);printf("\n");}/*以下是链队的基本运算算法*/int InitQueue(LinkQueue *q){q->front=(LinkQueue Node *)malloc(sizeof(LinkQueue Node)); if(q->front!=NULL){q->rear=q->front;q->front->next=NULL;return(true);}else return(false);}int Empt(LinkQueue *q) /*判队满*/{return(q->front==q->rear);}int Lull(LinkQueue *q){return((q-a.rear+1)%M==q->front);}int EnterQueue(LinkQueue *&q,int e) /*进队*/{if((q->rear+1)%M==q->front) /*队满*/return 0;q->rear=(q->rear+1)%M;q->carNo[q->rear]=e;return 1;}int DeleteQueue(LinkQueue *&q,int &e) /*出队*/if(q->front==q->rear) /*对空情况*/return 0;q->front=(q->front+1)%M;e=q->carNo[q->front];return 1;}void DispQueue(LinkQueue *q) /*输出队中元素*/{int i;i=(q->front+1)%M;printf("%d",q->carNo[i];while((q->rear-i+M)%M>0){i=(i+1)%M;printf("%d",q->carNo[i]);}printf("\n");}void main(){int comm;int No,e1,Time,e2;int i,j;seqstack *st1,*st2;LinkQueue *qu;Initstack(st)Initstack(st1);InitQueue(Qu);do{printf("input a number(1:到达2:离开3:停车场4:便道0退出):"); scanf("%d%d%d",&comm,&no,&time);switch(comm){case 1; /*汽车到达*/if(!stackFull(st)) /*便道不满*/{Push(st,no,time);printf(">>停车场位置:%d\n",st->top+1);}else /*停车场满*/{if(!QueueFull(Qu)) /*便道不满*/{EnterQueue(Qu,no);printf(">>候车场位置:%d\n",qu->rear);}elseprintf(">>候车场已满,不能停车\n");}break;case 2: /*汽车离开*/for(i=0;i<=st->top&&st->carNo[i]!=no;i++);if(i>st->top)printf(">>未找到该编号汽车\n";else{ for(j=i;i<=st->top;j++){Pop(st,e1,e2);Push(st1,e1,e2); /*倒车到临时栈st1中*/}Pop(st,e1,e2); /*该汽车离开*/printf(">>%d汽车停车费用:%d\n",no,(time-e2)*price); while(!stackEmpty(st1)) /*将临时栈St1重新回到St中*/ { Pop(st1,e1,e2);Push(st,e1,e2);}if(!QueueEmpty(Qu)) /*队不空时,将队头进栈St*/{DeleteQueue(Qu,e1);Push(st,e1,time); /*以当前时间开始记费*/}}break;case 3: /*显示停车场情况*/if(!stackEmpty(Qu)){ printf(">>停车场中车辆:"); /*输出停车场中的车辆*/ Dispstack(st);}elseprintf(">>停车场中无车辆:");break;case 4: /*显示便道情况*/if(!QueueEmpty(Qu)){printf(">>便道中的车辆:"); /*输出便道中的车辆*/ DispQueue(Qu);}elseprintf(">>便道中无车辆\n");break;case 0: /*结束*/if(!stackEmpty(st)){printf(">>停车场中的车辆:"); /*输出停车场中的车辆*/ Dispstack(st);}if(!QueueEmpty(Qu)){printf(">>便道中车辆:"); /*输出便道中的车辆*/DispQueue(Qu);}break;defaut: /*其他情况*/printf("error\n");break;}}while(comm!=0);}。
C语⾔版停车位管理系统本⽂实例为⼤家分享了C语⾔实现停车位管理系统的具体代码,供⼤家参考,具体内容如下简单功能介绍1、录⼊车辆信息2、查找车辆信息3、删除车辆信息4、修改车辆信息5、查找区域车辆信息6、排序(按照车主姓名排序)7、展⽰所有车辆信息8、将录⼊的数据保存在⽂件中9、将⽂件中的数据读出算法构造链表的增删改查结构体的定义typedef struct Parking{char name[20]; //车主姓名char carname[10]; //车牌号long information; //车主联系⽅式char region; //车位区域编号int num; //车位编号struct Parking *next; //指针}Parking;录⼊车辆信息利⽤尾插法插⼊新添加的数据Parking *Addcar(Parking *head) //录⼊车辆信息{int x;system("cls"); //清屏操作system("color B"); //改变字体颜⾊Parking *p = head;while(p->next!=NULL) //利⽤尾插法插⼊新⽤户信息{p = p->next;}printf("输⼊需要添加的车辆个数:");scanf("%d", &x);while(x--) //输⼊⽤户信息{system("cls");Parking *Node = (Parking *)malloc(sizeof(Parking));printf("\n输⼊⽤户姓名:");scanf("%s",Node->name);printf("\n输⼊车牌号:");scanf("%s",Node->carname);printf("\n输⼊车主联系⽅式:");scanf("%ld",&Node->information);printf("\n输⼊车位区域编号:");getchar();scanf("%c",&Node->region);printf("\n输⼊车位编号:");scanf("%d",&Node->num);Node->next=NULL;p->next = Node;p = p->next;}printf("\n输⼊完成!");F(head); //打印添加后的⽤户信息printf("\n按任意键返回主页\n");getch(); //⽤来显⽰结果return head;}删除⽤户信息根据⽤户名匹配查找⽤户信息进⾏删除Parking *Delete(Parking *head) //删除⽤户信息{char name[20];system("cls");system("color B");printf("\n\t\t输⼊你要删除的⽤户姓名:");scanf("%s", name);Parking *p = head;Parking *q = p->next;while(q) //找到需要删除数据的前结点{if(strcmp(q->name,name)==0) //判断字符串是否相等函数{p->next = q->next;free(q); //释放内存空间break;}p = q;q = p->next;}if(p->next==NULL){printf("\n\t\t未找到该⽤户信息!");}else{printf("\n\t\t删除成功!!");F(head);}printf("\n\t\t按任意键返回主菜单");getch();return head;}查找⽤户信息根据字符串匹配查找⽤户信息void Find(Parking *head) //查找⽤户信息{system("cls");system("color B");char name[20];printf("\n\t\t输⼊你要查找的⽤户姓名:");scanf("%s", name);Parking *p = head;while(p->next){if(strcmp(p->next->name,name)==0){printf("找到了!\n");printf("该⽤户的信息如下:");printf("\n\t\t---⽤户姓名 %s",p->next->name);printf("\n\t\t---车牌号 %s",p->next->carname);printf("\n\t\t---车主联系⽅式 %ld",p->next->information); printf("\n\t\t---车位区域编号 %c",p->next->region);printf("\n\t\t---车位编号 %d",p->next->num);break;}p = p->next;}if(p->next==NULL)printf("\n\t\t没有找到该⽤户信息");printf("\n\t\t按任意键返回主菜单");getch();}修改⽤户信息查找⽤户找到后显⽰该⽤户的信息,根据⽤户选择修改信息Parking *Change(Parking *head) //修改⽤户信息{char name[20];system("cls");system("color B");printf("输⼊需要修改的⽤户姓名:");scanf("%s", name);Parking *p = head->next;while(p){if(strcmp(p->name,name)==0){system("cls");system("color B");int x;printf("\n\t\t --该⽤户信息-- \n\n");printf("\t---------------------------------------------------------------------\n");printf("\t车主姓名--------车牌号-------车主联系⽅式---车位区域编号-----车位编号----\n");printf("\t%-17s%-16s%-15ld%-16c%-17d\n", p->name, p->carname, p->information, p->region, p->num); printf("\n输⼊你要修改的信息编号:\n");printf(" 1-车主姓名 \n");printf(" 2-车牌号 \n");printf(" 3-车主联系⽅式 \n");printf(" 4-车位区域编号 \n");printf(" 5-车位编号 \n");scanf("%d", &x);switch(x){case 1:printf("\n\t输⼊修改后的信息");scanf("%s", p->name);break;case 2:printf("\n\t输⼊修改后的信息");scanf("%s", p->carname);break;case 3:printf("\n\t输⼊修改后的信息");scanf("%ld", &p->information);break;case 4:printf("\n\t输⼊修改后的信息");scanf("%c", &p->region);break;case 5:printf("\n\t输⼊修改后的信息");scanf("%d", &p->num);break;}printf("\n\t\t修改后的⽤户信息为");printf("\t车主姓名--------车牌号-------车主联系⽅式---车位区域编号-----车位编号----\n");printf("\t%-17s%-16s%-15ld%-16c%-17d\n", p->name, p->carname, p->information, p->region, p->num); break;}p = p->next;}if(p==NULL){printf("\n未找到相应⽤户");printf("\n按任意键返回主页\n");getch();return head;}printf("\n修改完成!");F(head); //显⽰修改后的全部信息printf("\n按任意键返回主页\n");getch();return head;}其他链表操作排序(根据⽤户姓名排序)void *Sort(Parking *head) //排序{Parking *p=head;Parking *q,*p1=NULL,*p2=NULL;while(p->next != NULL){q = p->next;while(q->next != NULL){if(strcmp(p->next->name,q->next->name)>0) //字符串⽐较{p1 = p->next; //记录p与q的下⼀结点p2 = q->next;q->next = q->next->next;p->next = p2;p2->next = p1;}elseq = q->next;}p = p->next;}F(head);printf("\n\t\t按任意键返回主菜单");getch();}查找区域车辆信息void RegionalVehicles(Parking *head) //寻找指定区域内车辆信息{system("cls");system("color B");char c;printf("\n\t\t输⼊你想要查看的区域");getchar();scanf("%c", &c);Parking *p = head->next;printf("\n\t\t --%c区域内车辆⽤户信息-- \n\n",c);printf("\t---------------------------------------------------------------------\n");printf("\t车主姓名--------车牌号-------车主联系⽅式---车位区域编号-----车位编号----\n");while(p){if(p->region==c) //判断区域字符与输⼊字符是否相等,相等则输出{printf("\t%-17s%-16s%-15ld%-16c%-17d\n", p->name, p->carname, p->information, p->region, p->num);}p = p->next;}printf("\n\t\t按任意键返回主菜单");getch();}⽂件的读取与输⼊将数据保存在⽂件中之所以⽤w形式打开⽂件是因为我在程序运⾏前已将⽂件内数据读出,最后进⾏保存数据时覆盖原数据不会导致原数据丢失void Preservation(Parking *head) //保存数据在⽂件中{const char* filename1 = "C:/Users/judicious/Desktop/car.text"; //⽂件路径FILE *fp = fopen(filename1, "w"); //以写的⽅式打开⽂件,若⽂件不存在,则建⽴新的⽂件。
#include<stdio.h>#include<stdlib.h>#define stacksize 2 //车站//容量///////////////////////////////////////////////////////typedef struct Snode{int number;float int_time[2];float bian_time[2];}record;typedef struct {record *base;record *top;int size;}Stack;/////////////////////////////////////////////////////typedef struct Qnode{int number;float int_time[2];struct Qnode *next;}Qnode,*Queue;typedef struct {Queue front;Queue rear;}Linkqueue;void xunhuan(Stack L,Linkqueue Q);void jixu(Stack L,Linkqueue Q);//////////////////////////////////////////////////////////////////////////////////////////////////////////void InitStack(Stack &L) //堆栈操作{L.base=(record*)malloc(sizeof(Snode)*stacksize);if(!L.base)exit(0);L.top=L.base;L.size=stacksize;}/////////////////////////////////////////////////////void input(Stack &L,record h){*L.top++=h;}///////////////////////////////////////////////////Snode output(Stack &L,record &e){e=*--L.top;return e;}int Stackman(Stack L){if(L.top-L.base==L.size)return 0;elsereturn 1;}int StackEmpty(Stack L){if(L.base==L.top)return 0;elsereturn 1;}////////////////////////////////////////////////////////////////////////////////////////////////////void Initque(Linkqueue &Q) //队列操作{Q.front=Q.rear=(Queue)malloc(sizeof(Qnode));if(!Q.front)exit(0);Q.front->next=NULL;}//////////////////////////////////////////////////void enqueue(Linkqueue &Q,int number,float time[]){Queue q;printf("停车场已满,请将车辆停入便道!\n");q=(Queue)malloc(sizeof(Qnode));q->int_time[0]=time[0];q->int_time[1]=time[1];q->number=number;q->next=NULL;Q.rear->next=q;Q.rear=q;}///////////////////////////////////////////////////void outqueue(Linkqueue &Q,Queue &e) //此处有点问题??????????????{// Qnode *q;// q=(Queue)malloc(sizeof(Qnode));e=Q.front->next;// Q.front->next=q->next;//delete q;Q.front->next=Q.front->next->next;if(Q.rear==e){Q.front=Q.rear;// Q.front=NULL;}}int QEmpty(Linkqueue Q){if(Q.rear==Q.front)return 0;elsereturn 1;}//////////////////////////////////////////////////// //停车场管理操作////////////////////////////////////////////////////void jixu(Stack L,Linkqueue Q){int n;fflush(stdin);scanf("%d",&n);switch(n){case 1:printf("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");xunhuan(L,Q);break;case 2:printf("****************************退出管理系统*****************************\n");default:printf("\n输入错误,请重新输入: ");jixu(L,Q);break;}}void jiaofei(float time[],float time1[],float time2[]) //缴费操作{float data1,data2;double money;if(time2[0]!=0&&time2[1]!=0)data2=(time1[0]-time2[0]-1)*60+60-time2[1]+time1[1];elsedata2=0;data1=(time[0]-time1[0]-1)*60+60-time1[1]+time[1];// printf("进入车场时间%f,退出车场时间%f\n",time1[1],time[1]);printf("你的停车时间为%lf小时,在便道停留时间%f小时\n",data1/60,data2/60);money=data1/60*5.0+data2/60*2.0;printf("请交纳%lf元\n",money);/* if(data1<=60.0)printf("请交纳5元。
设停车场是一个可停放n 辆车的狭长通道,且只有一个大门可供汽车进出。
在停 车场内,汽车按到达的先后次序,由北向南依次排列(假设大门在最南端)。
若 车场内已停满n 辆车,则后来的汽车要在门外的便道上等候, 当有车开走时,便 道上的第一辆车即可开入。
当停车场内某辆车要离开时,在它之后进去的车辆必 须先推出车场为它让路,待该辆车开出大门以后,其他车辆再按原次序返回车场。
每辆车离开停车场时,应按其停留时间的长短交费(在便道上停留的时间不收 费)。
试编写程序,模拟上述管理过程。
要求以顺序栈模拟停车场,以链队列模 拟便道。
从终端读入汽车到达或离去的数据,每组数据包括三项:(1)是“到达” 还是“离去” (2)汽车牌照号码;(3) “到达”或“离去”的时刻。
与每组输入信 息相应的输出信息为:如果是到达的车辆,则输出其在停车场中或便道上的位置; 如果是离去的车辆,则输出其在停车场中停留的时间和应缴的费用。
(需另设一个栈,临时停放为让路而从车场退出的车。
)#include<iostream> #define M 5using namespace std;typedef int Datatype;typedef struct {Datatype bianhao[M];int top;int h[M]; // int m[M]; //int s[M]; // }Seqstack; // typedef struct Node // { Datatype bianhao; struct Node *next; }node;typedef struct{node *front;node *rear;int count;}biandao; // 便道//停车场顺序栈初始化void lnitSeqstack(Seqstack *t)时分秒停车场栈的定义{t->top=-1;}//进栈,即进入停车场int Push(Seqstack *t,int x,int h,int m,int s){if(t->top==M-1)return 0; // 停车场栈已满t->top++;t->bianhao[t->top]=x;t->h[t->top]=h;t->m[t->top]=m;t->s[t->top]=s;return 1;}//出栈,即离开停车场int Pop(Seqstack *t,int *x,int *h,int *m,int *s) {if(t->top==-1)return 0;else{*x=t->bianhao[t->top]; *h=t->h[t->top]; *m=t->m[t->top];*s=t->s[t->top]; t->top--;return 1;}}//查找某牌照的车在停车场中的位置,若找到则返回其位置,否则返回-1int Find(Seqstack t,int x){int i;for(i=0;iv=t.top;i++){ if(t.bianhao[i]!=x) continue;elsebreak;}if(i>t.top)return(-1);elsereturn(i);}//判断停车场内是否已满int lsSeqstackFull(Seqstack t){if(t.top==M-1)return 1;elsereturn 0;}//判断停车场内是否已没有车辆int lsSeqstackEmpty(Seqstack t) {if(t.top==-1)return 1;elsereturn 0;}//依次显示停车场内停放的所有车辆void ShowSeqstack(Seqstack t) {int i;if(t.top==-1)coutvv"停车场内没有停放车辆"vvendl;elsefor(i=0;i<=t.top;i++)coutvv"牌照:"vvt.bianhao[i]vvendl;}//队列初始化int Initbiandao(biandao *Q){Q->front=new node;if(Q-> front!=NULL){Q->rear=Q->fr ont;Q->front->next=NULL;Q->count=0;return(true);}else return(false);}//入队操作,即当停车场满了的时候,再到达的车辆进去便道队列int Enterbiandao(biandao *Q,int x) {node *NewNode; NewNode=new node; if(NewNode!=NULL) {NewNode->bianhao=x; NewNode->next=NULL;Q->rear->next=NewNode; Q->rear=NewNode;Q->co un t++;return(true);}else return(false);}//出队操作,即便道上的车辆从便道开出来int Deletebiandao(biandao *Q,int *x){node *p;if(Q->fro nt==Q->rear)return(false);p=Q->front->next;Q->front->next=p->next; if(Q->rear==p) Q->rear=Q->fr ont;*x=p->bianhao;free(p);Q->count--;return(true);}//判断便道队列是否为空int lsbiandaoEmpty(biandao Q){if(Q.front==Q.rear) return(true);elsereturn(false);// 当count=0时,就空了}//依次显示便道上停放的所有车辆void Showbiandao(biandao Q)node *p;p=Q.front->next;if(p==NULL)coutvv"便道上没有停放车辆!"vvendl;elsewhile(p!=NULL){coutvv" 牌照:"vvp->bianhaovvendl; p=p->n ext;}}36//计算停留时间差double Time(int h1,int m1,int s1,int h2,int m2,int s2) {double p,q;p=h1*3600+m1*60+s1 -(h2*3600+m2*60+s2); q=(double)p/3600;return(q);}//计算停车费用double Cost(double t,int u){return(t*u);}//主函数void main(){double time,cost;int ch;int x,*y, z;int i,flag=1,h,m,s,unit_price,hh,mm,ss;y=new int; Seqstack *t;// 定义停车栈t=new Seqstack;InitSeqstack(t);biandao *Q;// 定义便道队列Q=new biandao;Initbiandao(Q); Seqstack *r; //定义让路 栈r=new Seqstack;InitSeqstack(r);coutvv"请设置停车费用单价:(_元/小时)"vvendl; cin>>unit_price;while(flag){ 束"vvendl;cin>>ch;switch(ch) {case 1:coutvv" 请输入到达的汽车牌照号码:"vvendl; cin>>x;if(lsSeqstackFull(*t)){coutvv" 停车场已满,请在便道等候 !"vvendl; "vvendlvv"2: 离开"vvendlvv"3:依次显示停车场内停放车H***************************" coutvv" 请选 择命令:"vvendlvv"1:辆"vvendlvv"4:依次显示便道上停放车辆 "<<endlvv"5: 结 coutvv vvendl;coutvv H***************************" vvendl;Enterbiandao(Q,x);coutvv" 将此车停放在便道的"vvQ->countvv" 号位置!"vvendl;}else{while(1){coutvv" 请输入到达的时间(例如,21 08 23) : "vvendl;cin>>h>>m>>s;if (hv0 || h>23) continue; //输入数据不合法,回去重新输入if (mv0 || m>59) continue;if (s>-1 && sv61)break;//输入时间全部合法,退出循环}Push(t,x,h,m,s);coutvv" 将此车停放在停车场的"vvt->top+1vv" 号停车位置"vvendl;}break;case 2:if(lsSeqstackEmpty(*t))coutvv" 停车场内已没有车辆"vvendl;else{coutvv" 请输入要离开的汽车的车牌照号码:"vvendl;cin>>x;if(Find(*t,x)==-1)coutvv" 停车场内没有该汽车!"vvendl;else//有该号码的汽车{while⑴{coutvv" 请输入离开的时间(例如,2358 03) : "vvendl;cin>>h>>m>>s;if (h<0 || h>23) continue;//输入数据不合法,回去重新输入if (m<0 || m>59) continue;if (s>-1 && s<61)break; // 输入时间全部合法,退出循环}if(Find(*t,x)==t->top){Pop(t,y,&hh,&mm,& ss);//要离开的车辆正好是最后一辆进入停车场的车time=Time(h,m,s,hh,mm,ss);coutvv" 停留时间为:"vvtimevv"小时"vvendl; // 结账cost=Cost(time,unit_price);coutvv"应缴费用为:"vvcostvv" 元"vvendl;}else{for(i=(Find(*t,x)+1);iv=t->top;)〃该车不是最后一辆进入停车场的车,其它车要让路{Pop(t,y,&hh,&mm,& ss);Push(r,*y,hh,mm,ss); //进入让路栈rPop(t,y,&hh,&mm, &ss);//要离开的车辆离开了time=Time(h,m,s,hh,mm,ss);coutvv" 停留时间为:时"vvendl; // 结账cost=Cost(time,unit_price);coutvv"应缴费用为: "vvendl;for(i=0;iv=r->top;) // 让路的车辆重新回到停车场{Pop(r,y,&hh,&mm, &ss); Push(t,*y,hh,mm,ss); }}if(!lsbiandaoEmpty(*Q)){Deletebiandao(Q, &z);//便道上的第一辆车出队Push(t, z, h,m,s);//进入停车场,默认进入的时刻就是刚才的车离开的时刻}elsecoutvv"便道上没有车辆等候!"vvendl;}}break;"vvtimevv"小 "vvcostvv"元case 3:ShowSeqstack(*t); break;case 4:Showbiandao(*Q); break;case 5:flag=O;break;default:cout<<"错误命令!"vvendl;}}}。
C停车场管理系统简单代码实现#include<iostream.h>#include<stdio.h>#include<stdlib.h>#include<string.h>#define MAX 30 /*车库容量*/#define price 30 /*每车每小时费用*/typedef struct time{int hour;int min;}Time; /*时间结点*/typedef struct node{char num[10];Time reach;Time leave;}CarNode; /*车辆信息结点*/typedef struct NODE{CarNode *stack[MAX+1];int top;}SeqStackCar; /*模拟车站*/typedef struct car{CarNode *data;struct car *next;}QueueNode;typedef struct Node{QueueNode *head;QueueNode *rear;}LinkQueueCar; /*模拟通道*/void InitStack(SeqStackCar *); /*初始化栈*/int InitQueue(LinkQueueCar *); /*初始化便道*/int Arrival(SeqStackCar *,LinkQueueCar *); /*车辆到达*/void Leave(SeqStackCar *,SeqStackCar *,LinkQueueCar *); /*车辆离开*/ void List(SeqStackCar,LinkQueueCar); /*显示存车信息*/void main(){system("color 00f");SeqStackCar Enter,Temp;LinkQueueCar Wait;int ch;InitStack(&Enter); /*初始化车站*/InitStack(&Temp); /*初始化让路的临时栈*/InitQueue(&Wait); /*初始化通道*/while(1){cout<<" 欢迎光临"<<endl;cout<<"**************************"<<endl;cout<<" 1. 车辆到达"<<endl;cout<<" 2. 车辆离开"<<endl;cout<<" 3. 列表显示"<<endl;cout<<" 4. 退出系统"<<endl;cout<<"**************************"<<endl;cout<<"请选择所需要的服务!"<<endl;cout<<"请注意正确输入时间,不要为非数字!"<<endl;while(1){cin>>ch;if(ch>=1&&ch<=4)break;else cout<<"请选择:1|2|3|4."<<endl;}switch(ch){case 1:Arrival(&Enter,&Wait);break; /*车辆到达*/case 2:Leave(&Enter,&Temp,&Wait);break; /*车辆离开*/case 3:List(Enter,Wait);break; /*列表打印信息*/case 4:exit(0); /*退出主程序*/default: break;}}}void InitStack(SeqStackCar *s) /*初始化栈*/{int i;s->top=0;for(i=0;i<=MAX;i++)s->stack[s->top]=NULL;}int InitQueue(LinkQueueCar *Q) /*初始化便道*/{Q->head=(QueueNode *)malloc(sizeof(QueueNode));if(Q->head!=NULL){Q->head->next=NULL;Q->rear=Q->head;return(1);}else return(-1);}void PRINT(CarNode *p,int room) /*打印出站车的信息*/ {int A1,A2,B1,B2;cout<<"请输入离开的时间:/**:**/"<<endl;cin>>p->leave.hour;while(p->leave.hour<p->reach.hour||p->leave.hour>23) {cout<<"error!"<<endl;cout<<"请输入离开的时间的时("<<p->reach.hour<<"-23)"<<endl;cin>>p->leave.hour;B1=p->leave.hour;}cout<<":"<<endl;cin>>p->leave.min;cout<<endl<<"离开车辆的车牌号为:"<<endl;puts(p->num);cout<<"其到达时间为:"<<p->reach.hour<<":"<<p->reach.min<<endl;cout<<"离开时间为: "<<p->leave.hour<<":"<<p->leave.min<<endl;A1=p->reach.hour;A2=p->reach.min;B1=p->leave.hour;B2=p->leave.min;cout<<"应交费用为: "<<((B1-A1)*60+(B2-A2))*price<<"元!"<<endl;free(p);}int Arrival(SeqStackCar *Enter,LinkQueueCar *W) /*车辆到达*/{CarNode *p;QueueNode *t;p=(CarNode *)malloc(sizeof(CarNode));flushall();cout<<"请输入车牌号(例:A1234):"<<endl;gets(p->num);if(Enter->top<MAX) /*车场未满,车进车场*/{Enter->top++;cout<<"车辆在车场第"<<Enter->top <<"位置!"<<endl;cout<<"请输入到达时间:/**:**/"<<endl;cin>>p->reach.hour;while(p->reach.hour<0||p->reach.hour>23){cout<<"error!"<<endl;cout<<"请输入到达时间的时(0-23)!"<<endl;cin>>p->reach.hour;}cout<<":"<<endl;cin>>p->reach.min;while(p->reach.min<0||p->reach.min>59){cout<<"error!"<<endl;cout<<"请输入到达时间的分(0-59)!"<<endl;cin>>p->reach.min;}Enter->stack[Enter->top]=p;return(1);}else /*车场已满,车进便道*/{cout<<"该车须在便道等待!"<<endl;t=(QueueNode *)malloc(sizeof(QueueNode));t->data=p;t->next=NULL;W->rear->next=t;W->rear=t;return(1);}}void Leave(SeqStackCar *Enter,SeqStackCar *Temp,LinkQueueCar *W) { /*车辆离开*/int room;CarNode *p,*t;QueueNode *q;/*判断车场内是否有车*/if(Enter->top>0) /*有车*/{while(1) /*输入离开车辆的信息*/{cout<<"请输入车在车场的位置1--"<<Enter->top<<":";cin>>room;if(room>=1&&room<=Enter->top) break;}while(Enter->top>room) /*车辆离开*/{Temp->top++;Temp->stack[Temp->top]=Enter->stack[Enter->top];Enter->stack[Enter->top]=NULL;Enter->top--;}p=Enter->stack[Enter->top];Enter->stack[Enter->top]=NULL;Enter->top--;while(Temp->top>=1){Enter->top++;Enter->stack[Enter->top]=Temp->stack[Temp->top];Temp->stack[Temp->top]=NULL;Temp->top--;}PRINT(p,room);/*判断通道上是否有车及车站是否已满*/if((W->head!=W->rear)&&Enter->top<MAX) /*便道的车辆进入车场*/{q=W->head->next;t=q->data;Enter->top++;cout<<"便道的"<<t->num<<"号车进入车场第"<<Enter->top<<"位置!"<<endl;cout<<"请输入现在的时间如/*:*/:"<<endl;if(p->reach.hour<0||p->reach.hour>23){cout<<"error!"<<endl;cout<<"请输入到达时间的时(0-23)!"<<endl;cin>>p->reach.hour;}cout<<":"<<endl;cin>>p->reach.min;if(p->reach.min<0||p->reach.min>59){cout<<"error!"<<endl;cout<<"请输入到达时间的分(0-59)!"<<endl;cin>>p->reach.min;}W->head->next=q->next;if(q==W->rear) W->rear=W->head;Enter->stack[Enter->top]=t;free(q);}else cout<<"便道里没有车!"<<endl;}else cout<<"车场里没有车!"<<endl; /*没车*/}void List1(SeqStackCar *S) /*列表显示车场信息*/{int i;if(S->top>0) /*判断车站内是否有车*/{cout<<"车场:"<<endl;cout<<" 位置到达时间车牌号"<<endl;for(i=1;i<=S->top;i++){cout<<" "<<i<<""<<S->stack[i]->reach.hour<<":"<<S->stack[i]->reach.min<<" "<<S->stack[i]->num<<endl;}}else cout<<"车场里没有车!"<<endl;}void List2(LinkQueueCar *W) /*列表显示便道信息*/ {QueueNode *p;p=W->head->next;if(W->head!=W->rear) /*判断通道上是否有车*/ {cout<<"等待车辆的号码为:"<<endl;while(p!=NULL){puts(p->data->num);p=p->next;}}else cout<<"便道里没有车!";}void List(SeqStackCar S,LinkQueueCar W){int flag,tag;flag=1;while(flag){cout<<"请选择1|2|3:"<<endl;cout<<"1.车场"<<endl<<"2.便道"<<endl<<"3.返回"<<endl;while(1){cin>>tag;if(tag>=1||tag<=3) break;else cout<<"请选择1|2|3:"<<endl;}switch(tag){case 1:List1(&S);break; /*列表显示车场信息*/case 2:List2(&W);break; /*列表显示便道信息*/case 3:flag=0;break;default: break;}}}。
// 1、停车场管理.cpp : Defines the entry point for the console application. //^include stdafx.h*'^include vcon 1o.h>^include <malloc・h>^include <stdio.h>^include <stdlib.h>^include <windows. h>//清空当前屏幕#define ClearScreen() system( 'cls M )〃设置背景前景颜色//#define setcolor() system(u color 2f )//显不字符串szPrompt并等待用户按下任意键#define Pause( szPrompt) printf( %s: szPrompt ),getch()typedef struct carinformation // 车辆信息{char szRegistrationMark[64]; // 车牌号char szArrivalTime[16]; // 到达时间char szEntranceT1me[16]; //进入停车场(开始计费)时间char szDepartureTime[16]; // 离开时间} TCARINFORMATION, *LPTCARINFORAAATION;typedef struct carstack{LPTCARINFORMATION IpCarlnformation; // 车辆信息int nTop; //栈顶元素下标int nStackSize; // 栈容量} TCARSTACK, *LPTCARSTACK;//初始化栈IpCarStack,将其容量设置为nSizevoid ln1tStack( LPTCARSTACK ftlpCarStack, int nSize ){IpCarStack = ( LPTCARSTACK ) malloc( sizeof ( TCARSTACK ));lpCarStack->lpCarlnformation = ( LPTCARINFORMATION ) malloc( nSize * sizeof ( TCARINFORAAATION ));lpCarStack->nTop = -1;lpCarStack->nStackSize = n Size;}// 车辆信息carinfo 入栈IpCarStackvoid Push( LPTCARSTACK ftlpCarStack, TCARINFORAAATION carinfo ){IpCarStack-Top++;lpCarStack->lpCarl nformation [lpCarStack->nTop] = carinfo;}//车辆信息从栈IpCarStack中弹出并存入carinfovoid Pop( LPTCARSTACK ftlpCarStack, TCARINFORMATION &carinfo ) {carinfo = lpCarStack->lpCarlnformation[lpCarStack-> nTop];IpCarStack->nTop・・;}//若栈IpCarstack空,返回TRUE;否贝!J,返回FALSEBOOL IsStackEmptyf LPTCARSTACK IpCarStack ){return IpCarStackjnTop == -1;}//若栈IpStackFull满,返回TRUE;否则,返回FALSEBOOL IsStackFulK LPTCARSTACK IpCarStack ) return lpCarStack->nTop == ( lpCarStack->nStackSize • 1 );}// 销毁栈IpCarStack,将指针IpCarStack 置为NULLvoid DestroyStack( LPTCARSTACK &IpCarStack ){free( lpCarStack->lpCarlnformation );free( IpCarStack );IpCarStack = NULL;}typedef struct carnode // 链队结点信息{TCARINFORMATION carinfo; // 车辆信息struct carnode *lpNext; //指向下一个元素的指针} TCARNODE, *LPTCARNODE;typedef struct carqueue // ¥连队{LPTCARNODE IpHead; // 头结点LPTCARNODE IpRear; //指向当前队尾的指针int nEffectivesize; //当前队中元素个数} TCARQUEUE, *LPTCARQUEUE;//初始化链队IpCarQueuevoid lnitQueue( LPTCARQUEUE ftlpCarQueue ){IpCarQueue = ( LPTCARQUEUE ) malloc( sizeof( TCARQUEUE )); lpCarQueue->lpHead = ( LPTCARNODE) malloc( sizeof( TCARNODE ));lpCarQueue->lpHead->lpNext = NULL;lpCarQueue->lpRear = lpCarQueue->lpHead;IpCarQueue-EffectiveSize = 0;}// 车辆信息carinfo 入队IpCarQueuevoid EnQueue( LPTCARQUEUE ftlpCarQueue, TCARINFORMATION carinfo ) {LPTCARNODE IpCarNode = ( LPTCARNODE ) malloc( sizeof( carnode )); lpCarNode->car1nfo = cari nfo;lpCarNode->lpNext = NULL;lpCarQueue->lpRear->lpNext = IpCarNode;lpCarQueue->lpRear = lpCarQueue->lpRear->lpNext;IpCarQueue->n EffectiveSize++;}//队头元素从链队IpCarQueue中出队并存入carinfovoid DeQueue( LPTCARQUEUE ftlpCarQueue, TCARINFORMATION ftcarinfo ) { LPTCARNODE IpTemp = lpCarQueue->lpHead->lpNext;carinfo = IpTemp ・>cari nfo;lpCarQueue->lpHead->lpNext = IpTemp ・> IpNext;free( IpTemp );IpCarQueue->n EffectiveSize-;}//若链队IpCarQueue为空,返回TRUE;否则,返回FALSEBOOL lsQueueEmpty( LPTCARQUEUE IpCarQueue ){return LpCarQueue->nEffect1veSize == 0;}//销毁链队IpCarQueuevoid DestroyQueue( LPTCARQUEUE EtlpCarQueue )LPTCARNODE IpNextCarNode = NULL;for ( LPTCARNODE IpCarNode = lpCarQueue->lpHead; IpCarNode != NULL; IpCarNode = IpNextCarNode ){IpNextCarNode = lpCarNode->lpNext;free( IpCarNode );}free( IpCarQueue );IpCarQueue = NULL;}//将字符串时间格式转换为数字(分钟)格式,例如12:36将被转换为756 (12 * 60 + 36)int ConvertTimeFormat( char *lpTime ){Int nHour = 0;int nMinute = 0;sscanf( IpTime, “%d:%d; 8tnHour, SnMinute );return nHour * 60 + nMinute;}//根据在停车场内的停留时间nContinuanceMinutes (分钟)计算费用double CalculateExpense( int nContinuanceMinutes ){return nContinuanceMinutes * ( 5.0 / 60 );}int main( void ){// setcolor();Int nParkCapability = 0; // 停车场容量putchar( \n*);PHns)jscanf(・•衣d jppnparkcapabujry「LPTCARSTACK -pear-stack"NULL"二®世曲生需更蹦-njrsrack 二pcarsrack 》nparkcmpmbsrty)八LPTCARQUEUE 【Pear-Queue =NULL"、二冊®曲爲兰赫更-njrQueue(-pcarQueue)jcharcCommandTypeHNULLj二劭3淋起 charSzuser-npuru28jH宀NULLY二a l}^> do宀Qearscreenoj二 seccoor(rpurchar(An ・rI .... );I -n -)j1 ->・-W 養区);purs(d ・W S B J F)jI H ・)」1-0・)」pucchar( -\n ・)一purs(=0辽)j P U £・A S§A 3926N 3W 3・・rPUS ・・9SPA3926二 444・・);puts(ni・);puts( © );putchar( \rf );printf(“请输入命令:“);seanf( “%s", szllserlnput);puts(H ................. “);char szCarlnformation[128] = { NULL};sscanff szllserlnput, //将命令类型与车辆信息分开存放”%c,%s“,&cCommandType, //用户输入的前半部分,即命令类型szCarlnformation //用户输入的后半部分,即车辆信息);char *LpCommaLocation = NULL; //车辆信息字符串中的逗号位置for ( IpCommaLocation = szCarlnformation; *lpCommaLocation != *\0'; lpCommaLocation++ ){if (*lpCommaLocation ==){break;}}*lpCommaLocati on = \0';TCARINFORMATION carinfo = { NULL }; //存储本次用户输入的车辆信息strcpy( carinfo.szRegistrationMark, szCarlnformation );if ( cCommandType == A )strcpy( carinfo.szArrivalTime, IpCommaLocation + 1 );if ( FALSE == lsStackFull( IpCarStack )){strcpy( carinfo.szEntranceTime, carinfo.szArrivalTime ); Push( IpCarStack, carinfo );printf(“已进入停车场第%d个车位\n“,lpCarStack->nTop + 1);printf("车牌号:\t\t%s\n", carinfo.szRegistrationMark ); printf("进入时间:\t%s\n", carinfo.szEntranceTime ); puts(“是否收费:\t是“);}else{EnQueue( IpCarQueue, carinfo );printf(“停车场已满,已停放在便道的第%d个车位\n“,lpCarQueue->nEffectiveS1ze);printf("车牌号:\t\t%s\n", carinfo.szRegistrationMark ); printf("停放时间:\t%s\n", carinfo.szArrivalTime );puts( ••是否收费:\t否”);}}else if ( cCommandType == 'D')strcpy( carinfo.szDepartureTime, IpCommaLocation + 1 );LPTCARSTACK IpTempCarStack = NULL;lnitStack( IpTempCarStack, nParkCapability );TCARINFORMATION carinfoOut = { NULL };BOOL blsCarFound = FALSE;while ( FALSE == lsStackEmpty( IpCarStack )){Pop( IpCarStack, carinfoOut);if ( 0 != strcmp( carinfoOut.szRegistrationMark, carinfo.szRegistrationMark )) {Push( IpTempCarStack, carinfoOut);}else{blsCarFound = TRUE;break;}}while ( FALSE == IsStackEmptyf IpTempCarStack )){TCARINFORAAATION tempcarinfo = { NULL };Pop( IpTempCarStack, tempcarinfo );Push( IpCarStack, tempcarinfo );} if ( FALSE == blsCarFound ) printf(1车牌号为%s 的车未进入停车场・\n;carinfo.szRegistrationMark );Pause(M.................... \n按任意键输入下 V信息…\n“);continue;strcpy( carinfoOut.szDepartureTime, carinfo.szDepartureTime );int nEntranceTime = ConvertTimeFormat( carinfoOut.szEntranceTime ); Int nDepartureTIme = ConvertTimeFormat( carinfoOut.szDepartureTime ); int nContinuanceMinutes = nDepartureTime • nEntranceTime;printf("计费时段:\t%s - %s (共%d 分钟)\n",carinfoOut.szE ntran ceTime,carinfoOut.szDepartureTime, nContinu anceMinutes);double rExpense = CalculateExpense( nContinuanceMinutes );printf("应交纳的费用:\t%.1 If 7E\n", rExpense );if ( FALSE == lsQueueEmpty( IpCarQueue )){TCARINFORAAATION tempcarinfo = { NULL };DeQueue( IpCarQueue, tempcarinfo );strcpy( tempcarinfo.szEntranceTim® carinfoOut.szDepartureTime ); Push( IpCarStack, tempcarinfo );puts(" .................... ”);printf(“停放在便道的第1个车位,车牌号为%s的车已进入停车场\n“,tempcarinfo. szRegistrati on Markelse if ( cCommandType == E )puts( "********************"puts(H :吴远彦\n M);puts「学号:\n u);Uts( M********************" )•break;else if ( cCommandType == 0*){ClearScree n();//setcolor();putchar( *\n );puts( “[停车场使用情况]\n“);puts( •[车位]\t[车牌号]\t倒达时间]\t[进入(开始计费)时间]\n“); for (in t i = 0; i <= IpCarStack-Top; i++ ){printf( M%d\t%s\t\t%s\t\t%s\n\i + 1,IpCarStack- > IpCar I nformati on [i]・ szRegistratio nAAark, lpCarStack->lpCarlnformat1 on [i].szArrivalTime,lpCarStack->lpCarlnformation[i].szE ntranceTime);}putchar( \n );putchar( \n );putchar( \n );puts( •■[便道使用情况]\n“);puts( •[车位]\t[车牌号]\t[到达时间]\t[进入(开始计费)时间]\n“); int nNum = 0;for ( LPTCARNODE IpCarNode = lpCarQueue->lpHead->lpNext; IpCarNode != NULL; IpCarNode = lpCarNode->lpNext ){n Num++;printf( ,,%d\t%s\t\t%s\t\t%s\n\n Num,lpCarNode->carinfo.szRegistratio nMark,lpCarNode->carinfo.szArrivalTime,lpCarNode->carinfo.szEntra nceTime);}putchar( \n );}else{puts(“输入信息有误.第f 字符只能为'A'或D或E或0'(区分大宵) }Pause(”.................... \n按任意键输入下一息.\n“);} while ( TRUE );DestroyStack( IpCarStack );DestroyQueue( IpCarQueue );Pause( “\n按任意键退出程序...\n“);return 0;。
源程序代码:#include <stdio.h>#include <stdlib.h>#include <iostream.h>#include <time.h>#define MaxSize 5#define fee 2#define L 10000#define M 20000typedef int ElemType;ElemType tmpnum=0;ElemType tmptime=0;typedef struct {ElemType car_num[MaxSize];ElemType car_time[MaxSize];int top;}STACK;typedef struct qnode {ElemType car_num;ElemType car_time;struct qnode *next;}QTYPT;typedef struct qptr {QTYPT *front;QTYPT *rear;}SQUEUE;SQUEUE LQ;void InitStack(STACK *S){S->top = -1;}int Full(STACK *S){if(S->top==MaxSize-1){printf("\n Stack is full! Push");return 0;}return 1;}int Push(STACK *S,ElemType num,ElemType time){ if(S->top==MaxSize-1){printf("\n Stack is full! Push");return 0;}S->top++;S->car_num[S->top]=num;S->car_time[S->top]=time;return 1;}int Empty(STACK *S){return (S->top==-1 ? 1:0);}int Pop(STACK *S,ElemType *num,ElemType *time){if(Empty(S)){puts("Stack is free Pop");return 0;}*num=S->car_num[S->top];*time=S->car_time[S->top];S->top--;return 1;}int GetTop(STACK *S,ElemType *num,ElemType *time){ if(Empty(S)){puts("Stack is free Gettop");}*num=S->car_num[S->top];*time=S->car_time[S->top];return 1;}void InitQueue(SQUEUE *LQ){QTYPT *p=NULL;p=(QTYPT *)malloc(sizeof(QTYPT));p->next=NULL;LQ->front=LQ->rear=p;}int EnQueue(SQUEUE *LQ,ElemType num,ElemType time){ QTYPT *s;s=(QTYPT *)malloc(sizeof(QTYPT));s->car_num=num;s->car_time=time;s->next=LQ->rear->next;LQ->rear->next=s;LQ->rear=s;return 1;}int CountQueue(SQUEUE *LQ){int i=1;QTYPT *mfront=NULL;QTYPT *mrear=NULL;mfront=LQ->front;mrear=LQ->rear;while(!(LQ->front==LQ->rear)){i++;LQ->front=LQ->front->next;}LQ->front=mfront;return i;}int Empty_Q(SQUEUE *LQ){return (LQ->front==LQ->rear?1:0);}int OutQueue(SQUEUE *LQ,ElemType *num,ElemType *time){ QTYPT *p;if(Empty_Q(LQ)){puts("Quenue is free OutQuenue");return 0;}p=LQ->front->next;*num=p->car_num;*time=p->car_time;LQ->front->next=p->next;if(LQ->front->next==NULL)LQ->rear=LQ->front;free(p);return 1;}int GetHead(SQUEUE *LQ,ElemType *num,ElemType *time){ if(Empty_Q(LQ)){puts("Quenue is free GetHead");return 0;}*num=LQ->front->next->car_num;*time=LQ->front->next->car_time;return 1;}void sleep(int time){clock_t goal;goal=time*(CLOCKS_PER_SEC)+clock();while(goal > clock()){;}}int chackinput(STACK *S,int pnum){int i=0;int num;num = pnum;i=S->top;for(;!(i==-1);i--)if(S->car_num[i]==num)return 1;return 0;}int chacktime(STACK *S,int ptime){return S->car_time[S->top] <= ptime ? 1 : 0;}int displaystats(STACK *S,int pinput){void displayhead(void);int i = 0;i = S->top;switch(pinput){case 10000:{if(!Empty(S))for(;!(i==-1);i--)printf("<===%d时%d号车停与%d车位===>\n",S->car_time[i],S->car_time[i],i+1);elsecout<<"停车场为空";printf("还有车%d个位\n",MaxSize-S->top-1);break;}case 20000:{displayhead();break;}default:{return 1;}}return 0;}void displayhead(void){cout<<'\n'<<"<===============CT停车场管理系统===================>"<<endl;cout<<"<==操作说明: ******* ==>"<<endl;cout<<"<==A:停车命令 ******* ==>"<<endl;cout<<"<==D:出车命令 *** ==>"<<endl;cout<<"<==E:退出程序 *** ==>"<<endl;cout<<"<==L: 显示停车场内状况 "<<endl;cout<<"<==============================================>"<<endl;}void displayChange(STACK *S,ElemType pnum,int ptime){printf(" (单价 %d元/小时 )\n",fee);printf("<======================电子收据===================>\n");printf("<==停车时间:--------------------------%d小时==>\n",ptime-tmptime);printf("<==车牌号码:--------------------------%d==>\n",tmpnum);printf("<==应收费用:--------------------------%d 元==>\n",(ptime-tmptime)*fee);printf("<====================谢谢=欢迎下次再来=============>\n");printf("正在打印收据请稍等\n");for(int count=1; count < 1;count++){printf("=");fflush(stdout);sleep(1);}printf(">\n");sleep(1);}void wait(char *string){printf("%s\n",string);for(int count=1; count < 1;count++){printf("-");fflush(stdout);sleep(1);}printf(">\n");}int outparkstation(STACK *S1,STACK *TS,ElemType pnum){int t_num=0;int t_time=0;while(1){Pop(S1,&t_num,&t_time);if(t_num == pnum){tmpnum=t_num;tmptime=t_time;while(!Empty(TS)){Pop(TS,&t_num,&t_time);Push(S1,t_num,t_time);}return 1;}Push(TS,t_num,t_time);}return 0;}int inparkstation(STACK *S){int parknum;int parktime;printf("还有车%d个位\n",MaxSize-S->top-1);printf("请输入车牌号码:");cin>>parknum;while(chackinput(S,parknum)){ printf("车牌号码重复,请输入%d1或者其他",parknum);cin>>parknum;}printf("请输入停车时间:");cin>>parktime;printf("%d号车于%d时停靠在%d位\n",parknum,parktime,S->top+2);Push(S,parknum,parktime);return 1;}int inbiandao(SQUEUE *SQ,STACK *S){int parknum ;printf("对不起,停车场已满,请您到便道等待.您将第");printf("%d进入停车场\n",CountQueue(SQ));printf("请输入车牌号码:");cin>>parknum;while(chackinput(S,parknum)) { printf("车牌号码重复,请输入%d1或者其他",parknum);cin>>parknum;} EnQueue(SQ,parknum,0);return 1;}int OutParkingStation(SQUEUE *biandao,STACK *car,STACK *tmp){int parknum = 0;int parktime = 0;int buf=0;if(!Empty(car)){displaystats(car,10000);printf("请输入您要调出的车牌号码:");cin>>parknum;while(!chackinput(car,parknum)) { printf("没有您要的%d的车牌号码,请输入正确的车牌号码:",parknum);cin>>parknum;} outparkstation(car,tmp,parknum); printf("%d时%d号车进入停车场\n",tmptime,tmpnum);printf("请输入现在的时间:");cin>>parktime;while(!chacktime(car,parktime)) { cout<<"输入时间小于停车时间,请重新输入:";cin>>parktime; }displayChange(car,parknum,parktime); if(biandao->front==biandao->rear) { }else{ printf("%d号车位空开\n",car->top+2);printf("%d时便道上的%d号汽车驶入%d号车位",parktime,biandao->front->next->car_num,car->top+2);OutQueue(biandao,&parknum,&buf); Push(car,parknum,parktime); }return 2;}printf("停车场为空\n"); return 1; };int main(int argc,char* agv[]){char chance='A';STACK car;InitStack(&car);STACK tmp;InitStack(&tmp);SQUEUE biandao;InitQueue(&biandao);loop:while(1){displayhead();cout<<"=>:";cin>>chance;switch(chance) {case 'A':{wait("正在查询车位,请稍等:");if(Full(&car))inparkstation(&car);elseinbiandao(&biandao,&car);break;}case 'D':{OutParkingStation(&biandao,&car,&tmp);break;}case 'L':{displaystats(&car,10000);break;}case 'M':{displaystats(&car,20000);break;}case 'E':{wait("正在保存数据程序即将退出\n命令执行中:"); exit(0);}default:{cout<<"输入错误"<<endl;goto loop;}}}system("PAUSE");return 0;}。
C语⾔源码实现停车场管理系统本⽂实例为⼤家分享了C语⾔停车场管理系统的具体代码,供⼤家参考,具体内容如下题⽬要求:刚开始在Codeblocks下⽤C语⾔写的,但是⽤指针传递参数的时候总是出问题。
后来就⽤C++,但是调⽤了C的输⼊输出和⽂件操作的头⽂件,所以代码都是C的main.cpp#include <iostream>#include <cstdio>#include <cstdlib>#include <windows.h>#include <ctime>#include <cstring>#include <conio.h>#define N 100using namespace std;typedef struct{char num[8];//车牌号long int time_in;int pos;//车辆的状态,0表⽰停在便道中,1表⽰停在停车场} vehicle; //定义车辆类型typedef struct{vehicle veh[N];int top;} SqStack; //⽤栈表⽰停车场typedef struct LNode{vehicle veh;struct LNode *next;} LinkList; //⽤单链表表⽰便道void Load(FILE *,SqStack *,LinkList *);void ShowMenu(int );int MakeChoice(int ,int );void Parking(SqStack *,LinkList *);void Back(SqStack *);void EnterPkl(SqStack *,LinkList *);void LeavePath(LinkList *);void View(SqStack *,LinkList *);void Write_and_Quit(FILE *,SqStack *,LinkList *);int main(){SqStack *pkl;LinkList *path;FILE *fp;pkl=(SqStack *)malloc(sizeof(SqStack));path=(LinkList *)malloc(sizeof(LinkList));fp=fopen("Parking_lot.txt","r+");if(fp==NULL){printf("数据加载失败!按任意键退出程序");getch();return 0;}Load(fp,pkl,path);while(1){system("cls");ShowMenu(pkl->top);switch(MakeChoice(1,6)){case 1:system("cls");Parking(pkl,path);break;case 2:system("cls");Back(pkl);break;case 3:system("cls");EnterPkl(pkl,path);break;case 4:system("cls");LeavePath(path);break;case 5:system("cls");View(pkl,path);break;default:system("cls");Write_and_Quit(fp,pkl,path);return 0;}}return 0;}function.cpp#include <iostream>#include <cstdio>#include <cstdlib>#include <windows.h>#include <ctime>#include <cstring>#include <conio.h>#define N 100using namespace std;typedef struct{char num[8];//车牌号long int time_in;int pos;//车辆的状态,0表⽰停在便道中,1表⽰停在停车场} vehicle; //定义车辆类型typedef struct{vehicle veh[N];int top;} SqStack; //⽤栈表⽰停车场typedef struct LNode{vehicle veh;struct LNode *next;} LinkList; //⽤单链表表⽰便道void Load(FILE * fp,SqStack * pkl,LinkList * path){pkl->top=-1;path->next=NULL;LinkList *p;char num[8];long int time_in;int pos;while(fscanf(fp,"%s %ld %d\n",num,&time_in,&pos)!=EOF){if(pos==0)//该车辆在便道中{//尾插法建⽴单链表p=(LinkList *)malloc(sizeof(LinkList));strcpy(p->veh.num,num);p->veh.time_in=time_in;p->veh.pos=pos;path->next=p;path=p;}else//该车辆在停车场中{++pkl->top;strcpy(pkl->veh[pkl->top].num,num);pkl->veh[pkl->top].time_in=time_in;pkl->veh[pkl->top].pos=pos;}}path->next=NULL;}void ShowMenu(int n){printf("********⼀个简单的停车场管理系统********\n");if(n+1==N)printf("***************停车场已满***************\n");elseprintf("**********当前停车场共有%03d辆车**********\n",n+1);printf("********说明:停车场每⼩时收费5元********\n");printf("****************1.停车******************\n");printf("****************2.取车******************\n");printf("*********3.便道车辆进⼊停车场***********\n");printf("**************4.离开便道****************\n");printf("**************5.查看车辆****************\n");printf("****************6.退出******************\n");}int MakeChoice(int m,int n){int judge;printf("请输⼊%d~%d\n",m,n);scanf("%d",&judge);while(judge<m||judge>n)//确保输⼊的是1~n{printf("输⼊不合法,请输⼊%d~%d\n",m,n);fflush(stdin);//如果不加这句,输⼊⼀些字母会导致函数⽆限循环 scanf("%d",&judge);}return judge;}void Parking(SqStack *pkl,LinkList *path){LinkList *r;printf("请输⼊车牌号:");if(pkl->top<N-1){fflush(stdin);scanf("%8s",pkl->veh[++pkl->top].num);time(&(pkl->veh[pkl->top].time_in));pkl->veh[pkl->top].pos=1;printf("您的车辆已停⾄%2d号车位\n",pkl->top);}else{fflush(stdin);r=(LinkList *)malloc(sizeof(LinkList));scanf("%8s",r->veh.num);printf("停车场已满,您要暂时停放在便道中吗?\n");printf("1.确定 2.取消\n");if(MakeChoice(1,2)==1){while(path->next!=NULL)path=path->next;r->veh.time_in=0;r->veh.pos=0;path->next=r;r->next=NULL;printf("您的车辆已停放到便道中\n");}elsefree(r);}printf("按任意键返回主菜单");getch();return;}void Back(SqStack *pkl){int n,i=0;long int time_out;double hours;vehicle t_pkl[N];printf("请输⼊您的车辆所在的车位(⽬前还有个⼩问题,前⾯的车⾛了之后当前车位会-1):"); n=MakeChoice(0,pkl->top);printf("%2d上的车辆车牌号为%s,您确定要取⾛该车辆吗?\n",n,pkl->veh[n].num);printf("1.确定 2.取消\n");if(MakeChoice(1,2)==1){time(&time_out);hours=(time_out-pkl->veh[n].time_in)/3600.0;printf("本次停车共计%lf⼩时,收费%lf元,请按任意键确认⽀付\n",hours,hours*5);getch();for(i=0; pkl->top>=n; --pkl->top,++i) //把第n辆到第pkl->top辆车移到t_pklt_pkl[i]=pkl->veh[pkl->top];//此时pkl->top指向第n-1辆车for(i-=2; i>=0; --i) //把第n+1辆到第pkl->top辆车移回pklpkl->veh[++pkl->top]=t_pkl[i];printf("⽀付成功!\n");printf("取车成功,按任意键返回主菜单");getch();return;}else{printf("按任意键返回主菜单");getch();return;}}void EnterPkl(SqStack *pkl,LinkList *path){if(pkl->top==N-1)printf("停车场已满!");else{printf("您确定将便道中第⼀辆车(车牌号:%8s)停⼊停车场吗?\n",path->next->veh.num); printf("1.确定 2.取消\n");if(MakeChoice(1,2)==1){pkl->veh[++pkl->top]=path->next->veh;time(&pkl->veh[pkl->top].time_in);path->next=path->next->next;printf("已停⼊停车场\n");}}printf("按任意键返回主菜单");getch();return;}void LeavePath(LinkList *path){int i=0,n;LinkList *q;printf("请输⼊要离开便道的车辆的位序:");scanf("%d",&n);while(i<n&&path!=NULL){++i;q=path;//保存当前节点的前⼀个节点,如果找到的位置在链表最后,需要将前⼀个节点的指针域置为NULL path=path->next;}if(path!=NULL){printf("您确定便道中第%03d辆车(车牌号:%8s)离开便道吗?\n",n,path->veh.num);printf("1.确定 2.取消\n");if(MakeChoice(1,2)==1){if(path->next!=NULL)//确定离开并且不是便道中最后⼀辆车{q=path->next;path->next=q->next;free(q);printf("第%03d辆车已离开便道\n",n);}else//确定离开并且是便道中最后⼀辆车{printf("第%03d辆车已离开便道\n",n);q->next=NULL;free(path);}}}elseprintf("没有找到第%03d辆车\n",n);printf("按任意键返回主菜单");getch();return;}void View(SqStack *pkl,LinkList *path){int i;long int time_out;double hours;time(&time_out);printf("停车场共有%03d辆车:\n",pkl->top+1);for(i=0; i<=pkl->top; ++i){hours=(time_out-pkl->veh[i].time_in)/3600.0;printf("车位:%2d 车牌号:%8s 停车时长:%lf 应缴费⽤:%lf\n",i,pkl->veh[i].num,hours,hours*5);}printf("便道车辆:\n");if(path->next==NULL)printf("⽆\n");while(path->next!=NULL){path=path->next;printf("车牌号:%s\n",path->veh.num);}printf("按任意键返回主菜单");getch();return;}void Write_and_Quit(FILE *fp,SqStack *pkl,LinkList *path){rewind(fp);LinkList *pre=path,*p=path->next;for(; pkl->top>-1; --pkl->top)fprintf(fp,"%s %ld %d\n",pkl->veh[pkl->top].num,pkl->veh[pkl->top].time_in,pkl->veh[pkl->top].pos);while(p!=NULL){free(pre);fprintf(fp,"%s %ld %d\n",p->veh.num,p->veh.time_in,p->veh.pos);pre=p;p=pre->next;}free(pre);free(pkl);fclose(fp);}以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。