溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

C++怎么實現(xiàn)銀行排隊系統(tǒng)

發(fā)布時間:2021-04-14 11:08:50 來源:億速云 閱讀:241 作者:小新 欄目:編程語言

這篇文章將為大家詳細講解有關(guān)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;
}

關(guān)于“C++怎么實現(xiàn)銀行排隊系統(tǒng)”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

c++
AI