問題描述
?假設銀行有K個視窗提供服務,從早晨銀行開門(開門9:00am,關門5:00pm)起不斷有客戶進入銀行。由于每個視窗在某個時刻只能接待一個客戶,對于剛進入銀行的客戶(建議:客戶進入時間使用隨機函式產生),首先前往取號機取服務序號,取號機會自動列印出一張服務單。單上顯示服務號及該服務號前面正在等待的人數。當銀行職員服務完一個客戶后,只需按呼叫相應鍵,即可自動呼叫下一位客戶。
基本要求
(1)編制一個程式模擬銀行這種業務活動。
(2)計算一天中N個客戶在銀行的平均等待時間,最長等待時間,最后完成時間,并且統計每個視窗服務了多少顧客。
(3)建議有如下設定:
a)客戶到達的時間隨機,一天的人數設定為N人。
b)銀行業務員處理時間隨機產生。
代碼實作
#include<iostream>
#include <stdlib.h>
#include <time.h>
#include<windows.h>
using namespace std; int K,N; //定義銀行服務視窗個數和一天的客戶人數 //歡迎界面函式 void welcom(){ cout<<"*****************************歡迎光臨山大銀行****************************"<<endl; cout<<"銀行的營業時間為 9:00 -17:00,請您注意安排自己的時間,歡迎您的再次到來!"<<endl; } //定義鏈表節點 template<class T> struct chainNode {
//資料成員 T element;
chainNode<T> *next;
//方法 chainNode(){}
chainNode(const T& element){ this->element=element; }
chainNode(const T& element,chainNode<T> *next){ this->element=element; this->next=next; } }; //定義佇列 template<class T> class queue{ public: queue(); //初始化佇列 ~queue(){}; //解構式 bool empty(){ //判斷佇列是否為空 return queueSize==0; } int size(){ //回傳佇列中元素的個數 return queueSize; } T& front(){ //回傳佇列首元素 return queueFront->element; } T& back(){ //回傳佇列尾元素 return queueBack->element; } chainNode<T> * begin(){ return queueFront; } void pop(); //洗掉首元素 void push(const T& theElement); private: int queueSize; //佇列長度 chainNode<T> *queueFront; //指向佇列第一個元素的指標 chainNode<T> *queueBack; //指向佇列最后一個元素的指標 }; template<class T> queue<T>::queue(){ queueFront=queueBack=NULL; queueSize=0; } template<class T> void queue<T>::pop(){ if(queueFront==NULL) { cout<<"you queue is empty!"; return; } chainNode<T> *nextNode=queueFront->next; delete queueFront; queueFront=nextNode; queueSize--; } template<class T> void queue<T>::push(const T& theElement){ //申請新元素節點 chainNode<T> *newNode=new chainNode<T>(theElement,NULL); //把新節點插入隊尾 if(queueSize==0) queueFront=newNode; //隊空 else queueBack->next=newNode; //隊非空 queueBack=newNode; queueSize++; } //定義時間 struct thetime{ int h; //時 int m; //分 }; //定義每個客戶的到達銀行時間,等待時間,開始辦理業務時間,業務處理時間,結束業務時間(離開銀行時間) struct customers{ struct thetime arrive_time; //到達銀行時間 struct thetime wait_time; //等待時間 struct thetime start_time; //開始辦理業務時間 int business_time; //業務處理時間(假設業務處理時間為整數,且處理時間范圍為10-40min) struct thetime end_time; //結束辦理業務時間 int in_bank_number; //客戶進入銀行的序號 }; //展示視窗服務情況 void show_queue(queue<customers> cus_queue[]){ Sleep(1000); for(int i = 0;i < K;i++) { cout<<"視窗"<<i+1<<": "; chainNode<customers> *m = cus_queue[i].begin(); while(m != NULL) { cout<<"客戶"<<m->element.in_bank_number<<" "; m = m -> next; } cout<<endl; } cout<<endl; } //用戶到達銀行時間表函式 void customers_time(struct customers &c){ //隨機產生客戶到達銀行時間 c.arrive_time.h=9+rand()%8; c.arrive_time.m=rand()%60; //隨機產生客戶業務處理時間 //c.business_time=10+rand()%31; } //按用戶的到達時間將用戶的先后順序進行排序(由于用戶的到達時間是隨機產生的) void customer_sort(customers customer[]){ int max_time_index; //記錄客戶到達的最晚時間對應的下標 customers max_time_cus,swap_cus; //采用選擇排序進行排序 for(int i=N-1;i>0;i--) { max_time_cus=customer[i]; max_time_index=i; //找出到達時間最晚的 for(int j=0;j<i;j++) { if((customer[j].arrive_time.h)*60+customer[j].arrive_time.m > (max_time_cus.arrive_time.h)*60+max_time_cus.arrive_time.m) { max_time_cus=customer[j]; max_time_index=j; } } if(i!=max_time_index){ //swap部分 swap_cus=customer[i]; customer[i]=max_time_cus; customer[max_time_index]=swap_cus; } } } //判斷客戶需要去哪個視窗排隊,即算出等待時間最少的佇列 int judge_queue_in(queue<customers> cus_queue[],customers &customer,int each_queue_cus_number[]) { //將用戶在每個佇列需要等待的時間放在一個陣列里 int each_queue_wait_time[K]; for(int i=0;i<K;i++) { //客戶的等待時間取決于他隊伍最后一個人的結束辦理時間 int wait_h=cus_queue[i].back().end_time.h-customer.arrive_time.h; each_queue_wait_time[i]=wait_h*60+cus_queue[i].back().end_time.m-customer.arrive_time.m; } //找出需要時間最少的佇列 int min_time_queue_index=0; for(int j=1;j<K;j++) { if(each_queue_wait_time[j] < each_queue_wait_time[min_time_queue_index]) min_time_queue_index=j; } //定義客戶的各項資料 customer.business_time=10+rand()%31; customer.wait_time.h=each_queue_wait_time[min_time_queue_index]/60; customer.wait_time.m=each_queue_wait_time[min_time_queue_index]%60; customer.start_time.h=cus_queue[min_time_queue_index].back().end_time.h; customer.start_time.m=cus_queue[min_time_queue_index].back().end_time.m; customer.end_time.h=customer.start_time.h+(customer.start_time.m+customer.business_time)/60; customer.end_time.m=(customer.start_time.m+customer.business_time)%60; //將客戶加入佇列 //對銀行關門時間到了,還在辦理的客戶允許繼續辦理完業務再離開,還在排隊的進馬上離開 if((customer.start_time.h)*60+customer.start_time.m < 17*60) { cus_queue[min_time_queue_index].push(customer); each_queue_cus_number[min_time_queue_index]++; } return min_time_queue_index; } //判斷下一個客戶到來時哪個佇列的隊首客戶是否已經辦理完業務,并進行出隊 void leave_queue(queue<customers> cus_queue[],customers customer){ for(int i=0;i<K;i++) { while(!cus_queue[i].empty() && (cus_queue[i].front().start_time.h)*60+cus_queue[i].front().start_time.m+ cus_queue[i].front().business_time <= (customer.arrive_time.h)*60+customer.arrive_time.m) { cout<<"----------客戶"<<cus_queue[i].front().in_bank_number<<"離開了視窗"<<i+1<<"----------"<<endl; cus_queue[i].pop(); } } //show_queue(cus_queue); } //用戶進入佇列函式 void customers_in_queue(queue<customers> cus_queue[],customers customer[],int each_queue_cus_number[]) { //判斷哪個視窗是否有空閑的 int queue_number; for(int i=0;i<N;i++) { bool queue_free=false; //每次進入佇列判斷各隊首是否已經辦理完業務 leave_queue(cus_queue,customer[i]); for(int j=0;j<K;j++) { //視窗中有空閑的情況 if(cus_queue[j].empty()) { //客戶每進入一個佇列都需要判斷每個隊首客戶是否已經辦理完業務 customer[i].business_time=10+rand()%31; customer[i].wait_time.h=0; customer[i].wait_time.m=0; customer[i].start_time.h=customer[i].arrive_time.h; customer[i].start_time.m=customer[i].arrive_time.m; customer[i].end_time.h=customer[i].start_time.h+(customer[i].start_time.m+customer[i].business_time)/60; customer[i].end_time.m=(customer[i].start_time.m+customer[i].business_time)%60; cus_queue[j].push(customer[i]); each_queue_cus_number[j]++; queue_free=true; cout<<"----------客戶"<<customer[i].in_bank_number<<"到達了視窗"<<j+1<<"----------"<<endl; cout<<"客戶"<<customer[i].in_bank_number<<"需等人數:0"<<endl; break; } } //視窗中沒有空閑的情況 if(queue_free==false){ queue_number = judge_queue_in(cus_queue,customer[i],each_queue_cus_number); //判斷哪個佇列的等待時間最少 cout<<"----------客戶"<<customer[i].in_bank_number<<"到達了視窗"<<queue_number<<"----------"<<endl; cout<<"客戶"<<customer[i].in_bank_number<<"需等人數:"<<cus_queue[queue_number].size()-1<<endl; } show_queue(cus_queue); } //展示最后客戶的離開狀態 leave_queue(cus_queue,customer[N]); show_queue(cus_queue); } int main(){ //srand(time(0)); srand((unsigned int)time(NULL)); //使每次編譯完后產生的亂數不同 welcom(); //歡迎界面 cout<<"請輸入銀行服務視窗的個數:"; cin>>K; cout<<"請輸入銀行一天的客戶人數:"; cin>>N; customers customer[N]; queue<customers> cus_queue[K]; int each_queue_cus_number[N]; for(int i=0;i<N;i++){ customers_time(customer[i]); //初始用戶時間 each_queue_cus_number[i]=0; //初始視窗服務客戶個數 cout<<i+1<<" arrive_time: "<<customer[i].arrive_time.h<<": "; cout<<customer[i].arrive_time.m<<endl; } customer_sort(customer); //按客戶進入銀行的先后順序進行排序 cout<<"---------------------------after sort---------------------------"<<endl; for(int i=0;i<N;i++){ //Sleep(2000); customer[i].in_bank_number = i + 1; cout<<i+1<<" arrive_time: "<<customer[i].arrive_time.h<<": "; cout<<customer[i].arrive_time.m<<endl; } cout<<"---------------------------begin serve---------------------------"<<endl; customers_in_queue(cus_queue,customer,each_queue_cus_number); //客戶進佇列 cout<<"---------------------------end serve---------------------------"<<endl; cout<<"---------------------------after customer in queue---------------------------"<<endl; for(int i=0;i<N;i++){ cout<<i+1<<" start_time: "<<customer[i].start_time.h<<":"<<customer[i].start_time.m<<"\t"; cout<<i+1<<" end_time: "<<customer[i].end_time.h<<":"<<customer[i].end_time.m<<"\t"; cout<<i+1<<" bussiness_time: "<<customer[i].business_time<<"min\t"; cout<<i+1<<" wait_time: "<<(customer[i].wait_time.h)*60+customer[i].wait_time.m<<" "<<endl; } int max_cus_wait_time=(customer[0].wait_time.h)*60+customer[0].wait_time.m; int sum_cus_wait_time=max_cus_wait_time; for(int i=1;i<N;i++){ if((customer[i].wait_time.h)*60+customer[i].wait_time.m > max_cus_wait_time) max_cus_wait_time=(customer[i].wait_time.h)*60+customer[i].wait_time.m; sum_cus_wait_time+=(customer[i].wait_time.h)*60+customer[i].wait_time.m; } int actual_cus_numbers=0; for(int i=0;i<K;i++){ cout<<"視窗"<<i+1<<"服務顧客個數: "<<each_queue_cus_number[i]<<endl; actual_cus_numbers+=each_queue_cus_number[i]; } cout<<"max_cus_wait_time: "<<max_cus_wait_time<<"min"<<endl; cout<<"avg_cus_wait_time: "<<(double)sum_cus_wait_time/actual_cus_numbers<<"min"<<endl; return 0; }
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/91929.html
標籤:基礎類
上一篇:有沒有大佬會寫棋牌游戲的?
下一篇:Python問題
