C++实现银行排队系统
本文实例为大家分享了C++实现银行排队系统的具体代码,供大家参考,具体内容如下
#include#include #include intcnt=0;//当日客流量 intsum=0;//当日客户排队总时间 typedefstructpnode{ intnumber; intcometime; intleavetime; structpnode*next; }*person; typedefstructnode{ personfront; personrear; intlength; }linkqueue; linkqueueq[5]; intnumber,time; intflag=1; voidinitqueue(linkqueue&q){ q.front=q.rear=(person)malloc(sizeof(pnode)); if(!q.front||!q.rear) exit(0); q.front->next=NULL; q.length=0; } voidenterqueue(linkqueue&q,intnumber,inttime){ personp; p=(person)malloc(sizeof(person)); if(!p)exit(0); q.length++; p->number=number; p->cometime=time; //sum+=p->finishtime;//统计每个人的排队时长 p->next=NULL; q.rear->next=p; q.rear=p; } voidpopqueue(linkqueue&q){ personp; if(q.front==q.rear){ return; } p=q.front->next; q.front->next=p->next; q.length--; if(q.rear==p){ q.front=q.rear; } } intgetmin(linkqueueq[]){ inttemp=q[1].length; intj=1; for(inti=2;i<=4;i++){ if(q[i].length temp){ temp=q[i].length; j=i; } } returnj; } voidCustomer_Come(){ printf("客户选队并排队\n\n"); printf("输入客户编号:"); scanf("%d",&number); printf("输入当前时间:"); scanf("%d",&time); printf("\n"); /* if(one.length<=two.length&&one.length<=three.length&&one.length<=four.length){ enterqueue(one,number,time); } elseif(two.length<=one.length&&two.length<=three.length&&two.length<=four.length){ enterqueue(two,number,time); } elseif(three.length<=one.length&&three.length<=two.length&&three.length<=four.length){ enterqueue(three,number,time); } else{ enterqueue(four,number,time); }*/ printf("%d号顾客来到%d号窗口\n",number,getmin(q)); enterqueue(q[getmin(q)],number,time); } voidCustomer_Leave(){ printf("客户离队:\n\n"); printf("输入要离队的客户编号:"); scanf("%d",&number); printf("输入当前时间:"); scanf("%d",&time); //从四个队的队首分别去找,因为离队的只能是队首。 /*if(one.front->next->number==number){ printf("%d号客户办理完成业务从1号窗口离开\n",number); popqueue(one); break; } if(two.front->next->number==number){ printf("%d号客户办理完成业务从2号窗口离开\n",number); popqueue(two); break; } if(three.front->next->number==number){ printf("%d号客户办理完成业务从3号窗口离开\n",number); popqueue(three); break; } if(four.front->next->number==number){ printf("%d号客户办理完成业务从4号窗口离开\n",number); popqueue(four); break; }*/ for(inti=1;i<=4;i++){ if(q[i].front->next->number==number){ sum+=time-q[i].front->next->cometime; printf("%d号客户办理完成业务从%d号窗口离开\n",number,i); popqueue(q[i]); flag=0; } } if(flag) printf("编号错误请重新输入\n"); } voidAdjust_Queue(linkqueueq[]){ intm=getmin(q); intn=getmax(q); personx,y; x=(person)malloc(sizeof(pnode)); y=(person)malloc(sizeof(pnode)); x=q[n].rear; enterqueue(q[m],x->number,x->cometime); y=q[n].front; while(y->next!=q[n].rear) y->next=y->next->next; q[n].rear=y; free(y->next); q[n].length--; q[m].length++; } voidClose_Door(){ printf("下班时间到,工作结束。\n"); printf("今天共接待了%d位客户\n",cnt); printf("每位客户平均逗留时间为%.2f\n",(float)sum/cnt); } voidwelcome(){ printf("***************************************************\n"); printf("*模拟银行排队系统**请输入号码选择功能*\n"); printf("***************************************************\n"); printf("当前一号队列人数:%3d\n",q[1].length); printf("1客户选队并排队\n"); printf("当前二号队列人数:%3d\n",q[2].length); printf("2客户离队\n"); printf("当前三号队列人数:%3d\n",q[3].length); printf("3退出系统\n"); printf("当前四号队列人数:%3d\n",q[4].length); printf("***************************************************\n"); } intmain(){ //initqueue(one); //initqueue(two); //initqueue(three); //initqueue(four); for(inti=1;i<=4;i++){ initqueue(q[i]); } while(1){ welcome(); intx; scanf("%d",&x); switch(x){ case1: cnt++; Customer_Come(); break; case2: Customer_Leave(); while(q[getmax(q)].length-q[getmin(q)].length>=2) Adjust_Queue(q); break; case3: Close_Door(); exit(0); break; } } return0; }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。