main166.cpp
queue的说明请看:http://www.360doc.com/content/13/0912/17/3373961_314003806.shtml
#include <iostream> #include <queue> #include <cstdlib> #include <ctime> using namespace std; //我懒得分开多个了,直接从前面的复制过来了 class Customer { private: long arrive; int processtime; public: Customer(){arrive=processtime=0;}; void set(long when){ processtime=rand()%3+1; arrive=when; } long when()const{ return arrive; } int ptime() { return processtime; } friend ostream&operator<<(ostream &os,const Customer &c)//用于检测 { static int j=0; os<<j++<<" arrive:"<<c.arrive<<" need time:"<<c.processtime<<endl; return os; } }; typedef Customer Item; const int MIN_PER_HR=60; bool newCustomer(double x)//每隔x次,rand()/RAND_max会有一次值<1 { return rand()*x/RAND_MAX<1; } void main166() { srand(time(0));//初始化rand(); cout<<"Case Study:Bank of Heather Automatic Teller"<<endl; cout<<"Enter maximum size of queue:"; int qs; cin>>qs; queue<Item> line;//queue模板没有大小一说 cout<<"Enter the number of simulation hours:"; int hours; cin>>hours; long cyclelimit=MIN_PER_HR*hours;//循环的分钟数 cout<<"Enter the average number of customers per hour:";//一个小时来的人数 double perhour; cin>>perhour; double min_per_cust; min_per_cust=MIN_PER_HR/perhour;//平均多少分钟来一个人 Item temp; long turnaways=0; long customers=0; //long served=0; long served=0; long sum_line=0; int wait_time=0; long line_wait=0; for(int cycle=0;cycle<cyclelimit;cycle++)//cycle每循环一次,代表过了一分钟 { //line.s(); if(newCustomer(min_per_cust)) { if(line.size()==qs)//这样就代表满了 turnaways++;//拒绝服务???//恩,应该就是拒绝服务的人数 else { customers++;//队列人数//应该是顾客人数+1而不是队列人数+1 temp.set(cycle);//cycle是到达时间 line.push(temp);//入队列 } } if(wait_time<=0&&line.size()!=0)//用户处理完了业务 { line.pop();//出队列,不会返回值 //cout<<"after the delete"<<line;//test wait_time=temp.ptime();//wait_time是该客户处理业务所用时间 line_wait+=cycle-temp.when();//cycle-temp.when();是该客户一共在队列中等了多久 //一开始wait_time初始化是0,然后进入这里之后,又重新设置了使其=processtime //line_wait是该队列一共等了多久??哦,应该是所有客户的等待时间 //line_wait是客户等待总时间 served++;//服务人数+1 } if(wait_time>0)//正在处理业务的处理时间-1,因为过了一分钟 wait_time--;//话说这wait_time是谁设置了?一直=0? //wait_time=temp.ptime()这里随机设置了wait-time sum_line+=line.size();//sum_line又是神马东东?? //sum_line是队伍总长度,每分钟计算一次队伍长度 } if(customers>0) { cout<<"customers accepted:"<<customers<<endl; cout<<" customers served:"<<served<<endl; cout<<" turnaways:"<<turnaways<<endl; cout<<"average queue size:"; cout.precision(2); cout.setf(ios_base::fixed,ios_base::floatfield); cout<<(double)sum_line/cyclelimit<<endl; cout<<"average wait time:"<<(double)line_wait/served<<" minutes"<<endl; }//我机器上面是18人最接近1分钟,队列长度10,时间100 else cout<<"No coustomer!"<<endl; cout<<"Done!"<<endl; system("pause"); }
——————————————————————————————————————————————————
//写的错误或者不好的地方请多多指导,可以在下面留言或者给我发邮件,指出我的错误以及不足,以便我修改,更好的分享给大家,谢谢。
转载请注明出处:https://www.royalchen.com/
author:royalchen
Email:royalchen@royalchen.com
———————————————————————————————————————————————————