关闭

chapter16test6

标签: c++
98人阅读 评论(0) 收藏 举报
分类:

这一题的意思让咱们把原来的class queue删掉,在头文件写#include<queue>,就可以直接用人家标准的进站出站和各种功能,我看了好久没明白,后来在大神的程序中明白了,大神就是厉害,水货,加油啦

customer.h

#ifndef customer_H_
#define customer_H_
class customer
{
private:
long arrive;
int protime;
public:
customer(){ arrive = protime = 0; }
void set(long when);
long when()const{ return arrive; }
int ptime()const{ return protime; }
};
typedef customer Item;


#endif
void customer::set(long when)
{
protime = rand() % 3 + 1;
arrive = when;
}


user.cpp

#include<iostream>
#include<cstdlib>
#include<ctime>
#include<queue>
#include"customer.h"
const int MIN=60;
bool newcomer(double x);
int main()
{
using namespace std;
srand(time(0));
cout << "Case study :Bank of Heather automatic teller .\n";
cout << "Enter maxism of queue :";
int qs; cin >> qs;
queue<customer>line; /////qs ???
cout << "Enter the simulation hours :";
int hour; cin >> hour;
long cyclelimit = hour*MIN;
cout << "Enter the average customer per hour :";
double perhour; cin >> perhour;
double min_cost = MIN / perhour;

Item temp;
long turnaways = 0;
long customers = 0;
long served = 0;
long sum_line = 0;
long wait_time = 0;
long line_wait = 0;


for (int cycle = 0; cycle < cyclelimit; cycle++)
{
if (newcomer(min_cost))
{
if (line.size()==qs)
turnaways++;
else
{
customers++;
temp.set(cycle);
line.push(temp);
}
}
if (wait_time <= 0 && !line.empty())
{
wait_time = temp.ptime();
line_wait += cycle - temp.when();
line.pop();
served++;
}
if (wait_time > 0)
wait_time--;
sum_line += line.size();
}
if (customers > 0)
{
cout << "Customers accepted :" << customers << endl;
cout << "  Customers served :" << served << endl;
cout << "Customers turnaway :" << turnaways << endl;
cout.precision(2);
cout.setf(ios_base::fixed, ios_base::floatfield);
cout << "Average queue size :";
cout << (double)sum_line / cyclelimit << endl;
cout << "Average wait time:";
cout << (double)line_wait / served << " minutes\n";
}
else
cout << "No customer !\n";
cout << "Finished .\n";
return 0;
}


bool newcomer(double x)
{
return (rand()*x / RAND_MAX < 1);
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:50680次
    • 积分:2907
    • 等级:
    • 排名:第12185名
    • 原创:250篇
    • 转载:0篇
    • 译文:1篇
    • 评论:23条
    最新评论