chapter12test5

来源:互联网 发布:爱淘宝红包 1元 0.5元 编辑:程序博客网 时间:2024/06/08 04:34

题目的意思是让咱们尝试在不断改变每小时顾客人数,得到等候时间小于1分钟那个人数值。我在原来的程序基础上增加了一个while(),如下:

queue.h

#ifndef QUEUE_H_
#define QUEUE_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;


class queue
{
private:
struct node{ Item item; struct node *next; };
enum{ size = 10};
node *front;
node *rear;
int items;
int const qsize;
queue(const queue &q) :qsize(0){}
queue &operator=(const queue&q){ return *this; }
public:
queue(int qs = size);
~queue();
bool isempty()const;
bool isfull()const;
int cout()const;
bool enqu(const Item &item);
bool dequ(Item &item);
};


#endif


queue.cpp

#include<iostream>
#include"queue.h"
#include<cstdlib>
queue::queue(int qs) :qsize(qs)
{
front = rear = nullptr;
items = 0;
}
queue::~queue()
{
node *temp;
while (front != nullptr)
{
temp = front; front = temp->next; delete front;
}
}
bool queue::isempty()const
{
return items == 0;
}
bool queue::isfull()const
{
return items == size;
}
int queue::cout()const
{
return items;
}
bool queue::enqu(const Item &item)
{
if (isfull())
return false;
node *add = new node;
add->item = item;
add->next = nullptr;
items++;
if (front == nullptr)
front = add;
else
rear->next = add;
rear = add;
return true;
}
bool queue::dequ(Item &item)
{
if (isempty())
return false;
item = front->item;
items--;
node *temp = front;
front = front->next;
delete temp;
if (rear == nullptr)
rear = nullptr;
return true;
}


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


user.cpp

#include<iostream>
#include<cstdlib>
#include"queue.h"
#include<ctime>
int const MIN = 60;
bool newcome(double x);
int main()
{
using namespace std;
using std::ios_base;
std::srand(std::time(0));


cout << "Case study :Bank of Heather automatic Teller.\n";
cout << "Enter maximum of queue :";
int qs;
cin >> qs;
queue line(qs);


cout << "Enter the simulation hour :";
int hour;
cin >> hour;
long cyclet = hour*MIN;
Item temp;
long turnaway = 0;
long customer = 0;
long served = 0;
long sum_line = 0;
int wait_time = 0;
double line_wait=0;


cout << "Enter the average number of people per hour :";
double perhour;
while (cin >> perhour)
{
double percost = MIN / perhour;
for (int cycle = 0; cycle < cyclet; cycle++)
{
if (newcome(percost))
{
if (line.isfull())
turnaway++;
else
{
customer++;
temp.set(cycle);
line.enqu(temp);
}
}
if (wait_time <= 0 && !line.isempty())
{
line.dequ(temp);
wait_time = temp.ptime();
line_wait = line_wait + (cycle - temp.when());
served++;
}
if (wait_time > 0)
wait_time--;
sum_line = sum_line + line.cout();
}
double wait_mean ;
wait_mean = (double)line_wait / served;
if (wait_mean<1.0)
break;
else
{
line_wait = 0; wait_time = 0; sum_line = 0;
turnaway = 0; customer = 0;served = 0;
cout << "Enter the average number of people per hour :";
}
}   


if (customer > 0)
{
cout << "Costomers accepted :" << customer << endl;
cout << "Customers served :" << served << endl;
cout << "Customers turnaway :" << turnaway << endl;
cout << "Average queue size :";
cout.precision(2);
cout.setf(ios_base::fixed, ios_base::floatfield);
cout << (double)sum_line / cyclet << endl;
cout << "Average wait time :";
cout << (double)line_wait / served<<endl;
}
else
cout << "No customers .\n";
cout << "Done !";
return 0;
}


bool newcome(double x)
{
return (std::rand()*x / RAND_MAX < 1);
}


0 0
原创粉丝点击