数据结构队列C++应用:模拟银行排队系统(下)
接上文……
main.cpp的代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 | #include <iostream> #include<vector> #include <queue> #include <time.h> #include<iomanip> #include "customer.h" using namespace std; const int CASHIERS = 5; //设置5个窗口 void simulate(int cashiers, double prob_of_arrival, int service_time, int last_time); int main() { int cashiers=CASHIERS; // 顾客的到达概率 double prob_of_arrival; // 每名顾客接受服务所需的时间 int service_time; // 程序模拟持续的总时间 int last_time; cout << "***********************模拟银行排队系统***********************\n" << endl; cout << "请输入每分钟内顾客的到达概率(概率应介于0和1之间): "; cin >> prob_of_arrival ; cout << "请输入每个顾客接受服务所需要的时间(时间单位:分): "; cin >> service_time; cout << "请输入银行的工作总时间(时间单位:分): "; cin >> last_time; cout << endl; // 调用函数,仿真银行运行情况 simulate(cashiers, prob_of_arrival, service_time, last_time); system("pause"); return 0; } void simulate(int cashiers, double prob_of_arrival, int service_time, int last_time) { // 用于存储顾客信息 Customer customer; //5个Window的对象,分别对应5个窗口 Window Wnd[5]; // 顾客排队队列 queue<Customer> wait_queue; // 接受了服务的顾客总数 int served_customers; // 存储每个顾客的等待时间 vector<int> wait_time; // 总共的服务时间 int total_wait_time = 0; // 随机数种子 srand(time(NULL)); // 随机数 double rand_num; int count; //用于给顾客编号,每来一位顾客就顺序加1 int ticket=0; // 对每个窗口的时间和处理顾客数进行初始化 for (count = 0; count < cashiers; ++count){ //cashier_vacant[count] = 0; //cashier_customer[count]=0; Wnd[count].set_cashier_vacant(0); Wnd[count].set_cashier_customer(0); } //开始仿真,一个单位时间扫描一次 for ( int t = 0; t < last_time; ++t){ // 小于到达概率,表示说明顾客到达,顾客入队 if ((rand_num = (rand()%100)/100.0 )<= prob_of_arrival){ customer.set_arrival_time(ticket++,t); wait_queue.push(customer); } cout<<"****************第"<<t+1<<"个时间单元******************"<<endl; // 对窗口进行逐一扫描,判断其是否有空闲, 并且判断队列是否非空 for(count=0;count<cashiers;++count){ //if ((cashier_vacant[count]>t)){ if(Wnd[count].get_cashier_vacant()>t){ cout<<"第"<<count+1<<"号窗口正在为第"<< Wnd[count].get_cashier_ticket()+1<<"号顾客服务…"<<endl; } } for (count=0;count<cashiers;++count){ if( (Wnd[count].get_cashier_vacant()<=t) && !wait_queue.empty() && (Wnd[count].get_cashier_vacant()<last_time)){ // 顾客得到服务 customer = wait_queue.front(); wait_queue.pop(); Wnd[count].set_cashier_customer( Wnd[count].get_cashier_customer()+1 ); Wnd[count].set_cashier_ticket(customer.get_ticket_num()); // 设置顾客离去时间 customer.set_departure_time(t); // 顾客等待时间 wait_time.push_back( t - customer.get_arrival_time()); //所有已服务顾客等待所花费的总时间 total_wait_time += customer.total_time(); // 设置该窗口的下一个空闲时刻 Wnd[count].set_cashier_vacant( t+service_time ); cout<<"第"<<customer.get_ticket_num()+1<<"号顾客请到第"<<count+1<<"号窗口办理业务,您所花费的等待时间为" << t - customer.get_arrival_time()<<"分钟!"<<endl; }//end if }// end for cout<<"目前共有"<<wait_queue.size()<<"位顾客正在排队等待…"<<endl<<endl; }// end for cout<<"*******已下班,剩余顾客明天请早,不便之处,敬请原谅*******"<<endl<<endl; // 接受了服务的顾客总数 served_customers = wait_time.size(); double average_wait_time =(double)total_wait_time/served_customers ; // 输出统计结果 cout<<"*****************今日银行工作情况总结*********************"<<endl; for(count=0;count<cashiers;count++){ cout<<"第"<<count+1<<"号窗口接待的顾客数为"<<Wnd[count].get_cashier_customer()<<"名"<<endl; } cout<<endl; cout << "得到服务的顾客数为"<< served_customers << "名"<<endl; cout << "得到服务的顾客所需要的平均等待时间为" << setprecision(2)<<average_wait_time<<"分钟"<< endl; cout << endl; return 0; } |

近期评论