改了点,先前没看清楚题目要求。。。
#include <iostream>
#include <queue>
#include <ctime>
using namespace std;
class CPatient
{
int appear_time; //病人出现的时间间隔
int manage_time; //病人诊病耗时
public:
CPatient():appear_time(0),manage_time(0){}
~CPatient()
{
cout<<"appear_time is "<<appear_time<<endl<<"Manage_time is: "<<manage_time<<endl<<endl;
}
friend float enqueue(int &total_time,int&total_docter_wait_time,
int&total_patient_wait_time,int&total_patientsint,const int max_time);
};
float enqueue(int &total_time,int&total_docter_wait_time,
int&total_patient_wait_time,int&total_patients,const int max_time)
{
srand((unsigned)time( NULL ));
total_time=0;
queue<CPatient> queue_patient;
while(total_time<max_time)
{
//用随机产生的数字来初始化对象
CPatient p;
p.appear_time=rand()%14+1;
p.manage_time=rand()%9+1;
//如果此人为第一人,那么总时间直接加上他的出现间隔时间和他的诊病耗时
if(queue_patient.size()==0)
{
total_time+=(p.appear_time+p.manage_time);
total_docter_wait_time+=p.appear_time;
}
//否则
else
{
//如果前一人的诊病耗时>新来的人的间隔时间(即前一人诊病完毕之前就有人来)
//那么总时间直接加上新来病人的诊病耗时
if(queue_patient.back().manage_time>p.appear_time)
total_time+=p.manage_time;
//否则要多加一个时差(第二病人来的时间减去第一病人看完病的时间)
else
{
total_time+=(p.manage_time+(p.appear_time-queue_patient.back().manage_time));
total_docter_wait_time+=(p.appear_time-queue_patient.back().manage_time);
total_patient_wait_time+=(p.appear_time-queue_patient.back().manage_time);
}
}
queue_patient.push(p);
}
total_patients=queue_patient.size();
return float(total_time)/float(queue_patient.size());
}
void main()
{
cout<<"输入模拟实验的时间长度(小时):";int MAXTIME;cin>>MAXTIME;
MAXTIME*=60;
int total=0;
int total_docter_wait_time=0;
int total_patient_wait_time=0;
int total_patients=0;
float average=enqueue(total,total_docter_wait_time,total_patient_wait_time,total_patients,MAXTIME);
cout<<"医生总等待时间: "<<total_docter_wait_time<<"分钟"<<endl;
cout<<"病人总等待时间: "<<total_patient_wait_time<<"分钟"<<endl;
cout<<"诊断病人个数: "<<total_patients<<"个人"<<endl;
cout<<"平均处理时间: "<<average<<"分钟/人"<<endl;
}