C++實(shí)現(xiàn)銀行排隊(duì)系統(tǒng)
本文實(shí)例為大家分享了C++實(shí)現(xiàn)銀行排隊(duì)系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int cnt=0; //當(dāng)日客流量
int sum=0; //當(dāng)日客戶排隊(duì)總時(shí)間
typedef struct pnode{
int number;
int cometime;
int leavetime;
struct pnode *next;
}*person;
typedef struct node{
person front;
person rear;
int length;
}linkqueue;
linkqueue q[5];
int number,time;
int flag=1;
void initqueue(linkqueue &q){
q.front=q.rear=(person)malloc(sizeof(pnode));
if(!q.front||!q.rear)
exit(0);
q.front->next=NULL;
q.length=0;
}
void enterqueue(linkqueue &q,int number,int time){
person p;
p=(person)malloc(sizeof(person));
if(!p) exit(0);
q.length++;
p->number=number;
p->cometime=time;
// sum+=p->finishtime; //統(tǒng)計(jì)每個(gè)人的排隊(duì)時(shí)長
p->next=NULL;
q.rear->next=p;
q.rear=p;
}
void popqueue(linkqueue &q){
person p;
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;
}
}
int getmin(linkqueue q[]){
int temp=q[1].length;
int j=1;
for(int i=2;i<=4;i++){
if(q[i].length<temp){
temp=q[i].length;
j=i;
}
}
return j;
}
int getmax(linkqueue q[]){
int temp=q[1].length;
int j=1;
for(int i=2;i<=4;i++){
if(q[i].length>temp){
temp=q[i].length;
j=i;
}
}
return j;
}
void Customer_Come(){
printf("客戶選隊(duì)并排隊(duì)\n\n");
printf("輸入客戶編號(hào):");
scanf("%d",&number);
printf("輸入當(dāng)前時(shí)間:");
scanf("%d",&time);
printf("\n");
/*
if(one.length<=two.length&&one.length<=three.length&&one.length<=four.length){
enterqueue(one,number,time);
}
else if(two.length<=one.length&&two.length<=three.length&&two.length<=four.length){
enterqueue(two,number,time);
}
else if(three.length<=one.length&&three.length<=two.length&&three.length<=four.length){
enterqueue(three,number,time);
}
else{
enterqueue(four,number,time);
}*/
printf("%d號(hào)顧客來到%d號(hào)窗口\n",number,getmin(q));
enterqueue(q[getmin(q)],number,time);
}
void Customer_Leave(){
printf("客戶離隊(duì):\n\n");
printf("輸入要離隊(duì)的客戶編號(hào): ");
scanf("%d",&number);
printf("輸入當(dāng)前時(shí)間: ");
scanf("%d",&time);
//從四個(gè)隊(duì)的隊(duì)首分別去找,因?yàn)殡x隊(duì)的只能是隊(duì)首。
/* if(one.front->next->number==number){
printf("%d號(hào)客戶辦理完成業(yè)務(wù)從1號(hào)窗口離開\n",number);
popqueue(one);
break;
}
if(two.front->next->number==number){
printf("%d號(hào)客戶辦理完成業(yè)務(wù)從2號(hào)窗口離開\n",number);
popqueue(two);
break;
}
if(three.front->next->number==number){
printf("%d號(hào)客戶辦理完成業(yè)務(wù)從3號(hào)窗口離開\n",number);
popqueue(three);
break;
}
if (four.front->next->number==number){
printf("%d號(hào)客戶辦理完成業(yè)務(wù)從4號(hào)窗口離開\n",number);
popqueue(four);
break;
} */
for(int i=1;i<=4;i++){
if(q[i].front->next->number==number){
sum+=time-q[i].front->next->cometime;
printf("%d號(hào)客戶辦理完成業(yè)務(wù)從%d號(hào)窗口離開\n",number,i);
popqueue(q[i]);
flag=0;
}
}
if(flag)
printf("編號(hào)錯(cuò)誤請(qǐng)重新輸入\n");
}
void Adjust_Queue(linkqueue q[]){
int m=getmin(q);
int n=getmax(q);
person x,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++;
}
void Close_Door(){
printf("下班時(shí)間到,工作結(jié)束。\n");
printf("今天共接待了%d位客戶\n", cnt);
printf("每位客戶平均逗留時(shí)間為%.2f\n", (float)sum/cnt);
}
void welcome(){
printf(" ************************** *************************\n");
printf(" * 模擬銀行排隊(duì)系統(tǒng) * * 請(qǐng)輸入號(hào)碼選擇功能 *\n");
printf(" ************************** *************************\n");
printf(" 當(dāng)前一號(hào)隊(duì)列人數(shù):%3d \n",q[1].length);
printf(" 1 客戶選隊(duì)并排隊(duì) \n");
printf(" 當(dāng)前二號(hào)隊(duì)列人數(shù):%3d \n",q[2].length);
printf(" 2 客戶離隊(duì) \n");
printf(" 當(dāng)前三號(hào)隊(duì)列人數(shù):%3d \n",q[3].length);
printf(" 3 退出系統(tǒng) \n");
printf(" 當(dāng)前四號(hào)隊(duì)列人數(shù):%3d \n",q[4].length);
printf(" ************************** *************************\n");
}
int main(){
//initqueue(one);
//initqueue(two);
//initqueue(three);
//initqueue(four);
for(int i=1;i<=4;i++){
initqueue(q[i]);
}
while(1){
welcome();
int x;
scanf("%d",&x);
switch(x){
case 1:
cnt++;
Customer_Come();
break;
case 2:
Customer_Leave();
while(q[getmax(q)].length-q[getmin(q)].length>=2)
Adjust_Queue(q);
break;
case 3:
Close_Door();
exit(0);
break;
}
}
return 0;
}
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C語言編程計(jì)算信噪比SNR理解學(xué)習(xí)
這篇文章主要介紹了C語言編程信噪比SNR計(jì)算的理解學(xué)習(xí),信噪比,英文名稱叫做SNR或S/N(SIGNAL-NOISE RATIO)。是指一個(gè)電子設(shè)備或者電子系統(tǒng)中信號(hào)與噪聲的比例2021-10-10
C語言全面細(xì)致講解單雙精度float與double的使用方法
C語言中小數(shù)的數(shù)據(jù)類型為 float 或 double:float 稱為單精度浮點(diǎn)數(shù),double 稱為雙精度浮點(diǎn)數(shù)。不像整數(shù),小數(shù)的長度始終是固定的,float 占用4個(gè)字節(jié),double 占用8個(gè)字節(jié)2022-05-05
C語言編程中的聯(lián)合體union入門學(xué)習(xí)教程
這篇文章主要介紹了C語言編程中的聯(lián)合體union入門學(xué)習(xí)教程,也是C語言入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-12-12
C++有限狀態(tài)機(jī)實(shí)現(xiàn)計(jì)算器小程序
這篇文章主要為大家詳細(xì)介紹了C++有限狀態(tài)機(jī)實(shí)現(xiàn)計(jì)算器小程序的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
C語言實(shí)現(xiàn)循環(huán)隊(duì)列
這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)循環(huán)隊(duì)列,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-07-07
使用VC6.0對(duì)C語言程序進(jìn)行調(diào)試的基本手段分享
這篇文章主要介紹了用VC6.0開發(fā)c語言程序的時(shí)候調(diào)試代碼的一些小技巧,需要的朋友可以參考下2013-07-07
C++實(shí)現(xiàn)團(tuán)購訂單管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了如何利用C++實(shí)現(xiàn)團(tuán)購訂單管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-12-12
C++ vector在多線程操作中出現(xiàn)內(nèi)存錯(cuò)誤問題及解決
這篇文章主要介紹了C++ vector在多線程操作中出現(xiàn)內(nèi)存錯(cuò)誤問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08

