答案:先说明 不是我写的,给你找的
你可以参考这个,用栈和队列。
http://topic.csdn.net/t/20050407/21/3918333.html
#include<stdlib.h>
#include<iostream.h>
#include<time.h>
#include<fstream.h>
#include<iomanip.h>
int j=0,r=0;
/*建立票据的类,上面包含编号,客户到达银行时间以及要办理的业务*/
class fare
{
public:
int num;
char *oper;
int time;
int time1,time2;
/*票的编号是顺序产生的*/
void getnum(int n)
{
num=n;
}
/*客户到达银行时间是随机产生的*/
void gettime()
{
srand(j++); /*利用j控制长生从小到大不断变化的随机种子*/
r=((rand()-38)*5/8)%480; /*不同的数学组合可以控制随机数的产生。*/
time1=r/60+8;
time2=r%60;
}
/*客户办理何种业务也是随即产生的*/
void getoper()
{
oper=new char[10];
r=r%4;
switch(r)
{
case 0:oper="save";time=5;break;
case 1:oper="fetch";time=5;break;
case 2:oper="loss";time=10;break;
case 3:oper="repay";time=20;break;
}
}
/*显示票据内容*/
void display()
{
cout<<"号码"<<num<<" 到达时间:"<<time1<<":"<<time2;
cout<<" 办理业务:"<<oper<<" 所需时间:"<<time<<endl;
}
/*赋值函数*/
void get(fare p)
{
num=p.num;
oper=p.oper;
time=p.time;
time1=p.time1;
time2=p.time2;
}
};
/*建立银行窗口类,记录票据所有内容以及业务办理时间,同时计算办理类型总数*/
class window
{
public:
fare f;
int time3,time4;
int count,save,fetch,loss,repay;
int wait;
window()
{
count=save=fetch=loss=repay=time3=time4=0;
}
/*累计各种业务办理的数目*/
void add()
{
switch(*f.oper)
{
case 's':save++;break;
case 'f':fetch++;break;
case 'l':loss++;break;
case 'r':repay++;break;
}
}
void worktime()
{
if((time3*60+time4)<=(f.time1*60+f.time2))
{
time3=f.time1;
time4=f.time2;
}
}
/*计算等待时间*/
void getwait()
{
wait=(time3*60+time4)-(f.time1*60+f.time2);
}
};
/*判断用户应该到那个窗口办理业务*/
int min(window p1,window p2)
{
if((p1.time3*60+p1.time4)<=(p2.time3*60+p2.time4))
return 1;
else return 0;
}
int panduan(window q[],int n)
{
int j=0;
for(int i=1;i<n;i++)
{
if(!min(q[j],q[i])) j=i;
}
return j;
}
void main()
{
int n;
cout<<"请输入到银行办理业务的人数:";
cin>>n; /*n取200左右,且小于234(跟随机序列有关)*/
/*建立一天内用户流量的文件*/
fstream file("data.txt",ios::out|ios::app);
fare *a;
file<<"号码"<<" 到达时间:"<<" 办理业务:"<<" 所需时间:"<<endl;
a=new fare[n];
for(int i=0;i<n;i++)
{
a[i].getnum(i);
a[i].getoper();
a[i].gettime();
file<<setw(3)<<a[i].num<<setw(6)<<a[i].time1<<":"<<a[i].time2;
file<<setw(12)<<a[i].oper<<setw(8)<<a[i].time<<endl;
}
window win[4];
fstream win0("win0.txt",ios::out|ios::app);
fstream win1("win1.txt",ios::out|ios::app);
fstream win2("win2.txt",ios::out|ios::app);
fstream win3("win3.txt",ios::out|ios::app);
win0<<"号码"<<" 到达时间"<<" 办理业务"<<" 办理时间"<<endl;
win1<<"号码"<<" 到达时间"<<" 办理业务"<<" 办理时间"<<endl;
win2<<"号码"<<" 到达时间"<<" 办理业务"<<" 办理时间"<<endl;
win3<<"号码"<<" 到达时间"<<" 办理业务"<<" 办理时间"<<endl;
/*建立四个银行工作窗口文件记录业务办理情况*/
int w[4]={0,0,0,0},sumwait,sum;
double await;
for(i=0;i<n;i++)
{
int j=panduan(win,4);
if((win[j].time3*60+win[j].time4)>16*60) break;/*判断是否已经到了下班时间*/
win[j].f.get(a[i]);
win[j].count++;
win[j].add();
win[j].worktime();
win[j].getwait();
w[j]=w[j]+win[j].wait;
/*写入数据*/
switch(j)
{
case 0:win0<<setw(3)<<win[0].f.num<<setw(6)<<win[0].f.time1<<":"<<win[0].f.time2<<setw(12)<<win[0].f.oper<<setw(8)<<win[0].time3<<":"<<win[0].time4<<endl;break;
case 1:win1<<setw(3)<<win[1].f.num<<setw(6)<<win[1].f.time1<<":"<<win[1].f.time2<<setw(12)<<win[1].f.oper<<setw(8)<<win[1].time3<<":"<<win[1].time4<<endl;break;
case 2:win2<<setw(3)<<win[2].f.num<<setw(6)<<win[2].f.time1<<":"<<win[2].f.time2<<setw(12)<<win[2].f.oper<<setw(8)<<win[2].time3<<":"<<win[2].time4<<endl;break;
case 3:win3<<setw(3)<<win[3].f.num<<setw(6)<<win[3].f.time1<<":"<<win[3].f.time2<<setw(12)<<win[3].f.oper<<setw(8)<<win[3].time3<<":"<<win[3].time4<<endl;break;
}
/*确定窗口下次业务工作时间*/
win[j].time3=win[j].time3+(win[j].f.time+win[j].time4)/60;
win[j].time4=(win[j].f.time+win[j].time4)%60;
}
win0<<"办理的客户数目为:"<<win[0].count<<endl;
win0<<"取款业务数目:"<<win[0].fetch<<endl;
win0<<"挂失业务数目:"<<win[0].loss<<endl;
win0<<"还贷业务数目:"<<win[0].repay<<endl;
win0<<"存款业务数目:"<<win[0].save<<endl;
win1<<"办理的客户数目为:"<<win[1].count<<endl;
win1<<"取款业务数目:"<<win[1].fetch<<endl;
win1<<"挂失业务数目:"<<win[1].loss<<endl;
win1<<"还贷业务数目:"<<win[1].repay<<endl;
win1<<"存款业务数目:"<<win[1].save<<endl;
win2<<"办理的客户数目为:"<<win[2].count<<endl;
win2<<"取款业务数目:"<<win[2].fetch<<endl;
win2<<"挂失业务数目:"<<win[2].loss<<endl;
win2<<"还贷业务数目:"<<win[2].repay<<endl;
win2<<"存款业务数目:"<<win[2].save<<endl;
win3<<"办理的客户数目为:"<<win[3].count<<endl;
win3<<"取款业务数目:"<<win[3].fetch<<endl;
win3<<"挂失业务数目:"<<win[3].loss<<endl;
win3<<"还贷业务数目:"<<win[3].repay<<endl;
win3<<"存款业务数目:"<<win[3].save<<endl;
sumwait=w[0]+w[1]+w[2]+w[3];
sum=win[0].count+win[0].count+win[0].count+win[0].count;
await=sumwait/sum;
cout<<await<<endl;
file<<"到达银行客户总数:"<<n<<endl;
file<<"成功办了业务的客户总数:"<<sum<<endl;
file<<"客户平均逗留时间:"<<await<<endl;
file.close();
win0.close();
win1.close();
win2.close();
win3.close();
}
上一个:C/C++ 关键字const的定义?
下一个:DOS应该怎么学?还有C++