C++實現(xiàn)銀行排隊系統(tǒng)
本文實例為大家分享了C++實現(xiàn)銀行排隊系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int cnt=0; //當日客流量
int sum=0; //當日客戶排隊總時間
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)計每個人的排隊時長
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("客戶選隊并排隊\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);
}
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號顧客來到%d號窗口\n",number,getmin(q));
enterqueue(q[getmin(q)],number,time);
}
void Customer_Leave(){
printf("客戶離隊:\n\n");
printf("輸入要離隊的客戶編號: ");
scanf("%d",&number);
printf("輸入當前時間: ");
scanf("%d",&time);
//從四個隊的隊首分別去找,因為離隊的只能是隊首。
/* if(one.front->next->number==number){
printf("%d號客戶辦理完成業(yè)務(wù)從1號窗口離開\n",number);
popqueue(one);
break;
}
if(two.front->next->number==number){
printf("%d號客戶辦理完成業(yè)務(wù)從2號窗口離開\n",number);
popqueue(two);
break;
}
if(three.front->next->number==number){
printf("%d號客戶辦理完成業(yè)務(wù)從3號窗口離開\n",number);
popqueue(three);
break;
}
if (four.front->next->number==number){
printf("%d號客戶辦理完成業(yè)務(wù)從4號窗口離開\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號客戶辦理完成業(yè)務(wù)從%d號窗口離開\n",number,i);
popqueue(q[i]);
flag=0;
}
}
if(flag)
printf("編號錯誤請重新輸入\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("下班時間到,工作結(jié)束。\n");
printf("今天共接待了%d位客戶\n", cnt);
printf("每位客戶平均逗留時間為%.2f\n", (float)sum/cnt);
}
void welcome(){
printf(" ************************** *************************\n");
printf(" * 模擬銀行排隊系統(tǒng) * * 請輸入號碼選擇功能 *\n");
printf(" ************************** *************************\n");
printf(" 當前一號隊列人數(shù):%3d \n",q[1].length);
printf(" 1 客戶選隊并排隊 \n");
printf(" 當前二號隊列人數(shù):%3d \n",q[2].length);
printf(" 2 客戶離隊 \n");
printf(" 當前三號隊列人數(shù):%3d \n",q[3].length);
printf(" 3 退出系統(tǒng) \n");
printf(" 當前四號隊列人數(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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
上一篇:數(shù)據(jù)結(jié)構(gòu) 中數(shù)制轉(zhuǎn)換(棧的應(yīng)用)
欄 目:C語言
本文地址:http://www.jygsgssxh.com/a1/Cyuyan/1402.html
您可能感興趣的文章
- 04-02c語言沒有round函數(shù) round c語言
- 01-10數(shù)據(jù)結(jié)構(gòu)課程設(shè)計-用棧實現(xiàn)表達式求值的方法詳解
- 01-10使用OpenGL實現(xiàn)3D立體顯示的程序代碼
- 01-10深入理解C++中常見的關(guān)鍵字含義
- 01-10求斐波那契(Fibonacci)數(shù)列通項的七種實現(xiàn)方法
- 01-10C語言 解決不用+、-、&#215;、&#247;數(shù)字運算符做加法
- 01-10使用C++實現(xiàn)全排列算法的方法詳解
- 01-10c++中inline的用法分析
- 01-10用C++實現(xiàn)DBSCAN聚類算法
- 01-10深入全排列算法及其實現(xiàn)方法


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機閱讀
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 01-10delphi制作wav文件的方法
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 04-02jquery與jsp,用jquery
- 01-11ajax實現(xiàn)頁面的局部加載
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-10C#中split用法實例總結(jié)


