Bank Service Simulation ——队列的应用

      之前上学期间的作业,但当时没有用类来封装起来,所以打算重写,结果发现自己之前要写4,5个小时,现在居然两个小时就搞定了,但还有缺点,test函数太长,算法的核心部分其实没有封装起来,跪求大神指点。

以下是代码实现:

 

//RANDOM.h

#ifndef RANDOM_H
#define RANDOM_H

 
class Random 
{
public:
   Random(bool pseudo = true);
//    Declare random-number generation methods here.
 
   int random_integer(int low, int high);
   double random_real();
   int poisson(double mean);
 
private:
   int reseed(); //  Re-randomize the seed.
   int seed,
       multiplier, add_on; //  constants for use in arithmetic operations
};

#endif

 

//RANDOM.cpp

#include <limits.h>
const int max_int = INT_MAX;
#include <math.h>
#include <time.h>
#include "Random.h"

Random::Random(bool pseudo)
/* 
 
Post: The values of seed, add_on, and multiplier 
are initialized.  The seed is initialized randomly only if
pseudo == false.
 
*/
{
   if (pseudo) seed = 1;
   else seed = time(NULL) % max_int;
   multiplier = 2743;
   add_on = 5923;
}
 
double Random::random_real()
/* 
 
Post: A random real number between 0 and 1 is returned.
 
*/
{
   double max = max_int + 1.0;
   double temp = reseed();
   if (temp < 0) temp = temp + max;
   return temp / max;
}
 
int Random::random_integer(int low, int high)
/* 
 
Post: A random integer between low and high (inclusive)
is returned.
 
*/
{
   if (low > high) return random_integer(high, low);
   else return ((int) ((high - low + 1) * random_real())) + low;
}
 
int Random::poisson(double mean)
/* 
 
Post:
A random integer, reflecting a Poisson distribution
with parameter mean, is returned.
 
*/
{
   double limit = exp(-mean);
   double product = random_real();
   int count = 0;
   while (product > limit) {
      count++;
      product *= random_real();
   }
   return count;
}
 
int Random::reseed()
/* 
 
Post:
The seed is replaced by a psuedorandom successor.
 
*/
{
   seed = seed * multiplier + add_on;
   return seed;
}

 

//Service.h

#ifndef SERVICESYSTEM_H
#define SERVICESYSTEM_H

struct Customer
{
	int num;
	int served_time;
	int wating_time;
};

class Service
{
public:
	//构造函数
	Service();
	//判断服务窗口是否处于空闲状态
	bool Is_idle() const;   
	//当前服务顾客的信息,即更新每个时刻的状态
	void Customer_Information(Customer a);
	//重载
	void Customer_Information();
	//当前处理人数的叠加
	void Customer_add();
	//窗口处理服务的总人数
	int Customer_amount() const;
	//窗口累积服务的总时间
	int Customer_Serve_time() const;
	//顾客的等待时间
	int Customer_Wait_time() const;
	//输出当前的状态
	void Service_state();
private:
	bool state;
	Customer customer; 
	int amount;
	int servicing_time;  //服务顾客的总时间累积
};

#endif

 

//Service.cpp

#include "Service.h"
#include <iostream>
using namespace std;

Service::Service()
{
	state = true;   //起初处于空闲状态
	amount = 0;
	servicing_time = 0;
}

bool Service::Is_idle() const
{
	return state;
}

void Service::Customer_add()
{
	amount++;
}

void Service::Customer_Information(Customer a)
{
	customer = a;
	Customer_add();
	state = false;
	customer.served_time--;
	servicing_time++;
	Service_state();
	if(!customer.served_time)
		state = true;
}

void Service::Customer_Information()
{
	customer.served_time--;
	servicing_time++;   //每次所服务顾客时间的减少,同时也是服务时间的累积
	Service_state();
	if(!customer.served_time)
		state = true;
}

int Service::Customer_amount() const
{
	return amount;
}

int Service::Customer_Serve_time() const
{
	return servicing_time;
}

int Service::Customer_Wait_time() const
{
	return customer.wating_time;
}
void Service::Service_state()
{
	if(state)
		cout << "处于空闲状态。" << endl;
	else
	{
		cout << "正在处理号码为" << customer.num << "的业务。" << endl;
	}
}

 

//test.cpp

#include <iostream>
#include <queue>
#include <vector>
#include <list>
#include "Random.h"
#include "Service.h"
using namespace std;
int main()
{
	Random r(true);//Sample could be repeated.
	//初建5个服务窗口,同时调用默认构造函数
	Service windows[5] = {Service(),Service(),Service(),Service(),Service()};
	int Time;
	int record = 0;
	int Total_amount = 0;
	double Total_Serve_time = 0;
	double Total_Wait_time = 0;
	queue<Customer> Queue;
	//没办法,他要求要得出顾客等待的平均时间
	list<Customer> Waiting;
	list<Customer>::iterator itr ;  //迭代器
	int i;
	cout << "********欢迎使用银行模拟排队系统********" << endl;
	cout << "*************制作人:中大黑熊*************" << endl;
	cout << "******请输入你所要模拟的时间单位数目:t=?";
	cin >> Time;
	cout << endl << "以下是每个时间单位的模拟过程:" << endl << endl;
	for(i = 1 ;i <= Time;i++)
	{
		cout << "以下是第" << i << "个时间单位的实时信息:" << endl;
		int j;
		int time_num;    //每个时间单位来排队的人数
		time_num = r.poisson(2);  //Generate a sample of Poisson distribution of mean 5.
		if(!time_num)
		{
			for(j = 0; j < 5;j++)
			{
				cout << "窗口" << j+1;
				if(windows[j].Is_idle())
				{
					if(!Queue.empty())
					{
						Customer temp = Queue.front();
						Queue.pop();
						windows[j].Customer_Information(temp);
					}
					else
					{
						windows[j].Service_state();
					}
				}
				else
				{
					windows[j].Customer_Information();
				}
			}
			cout << "目前等待的顾客人数为:" << Queue.size() << endl << endl; 
		}
		else
		{
			Customer * p = new Customer[time_num];
			for(j = 0;j < time_num;j++)
			{
				p[j].num = ++record;
				p[j].wating_time = 0;  //初始化顾客的等待时间
				p[j].served_time = r.poisson(2);  //随机模拟顾客所需要的服务时间
				//将他们入队
				Customer temp = p[j];
				Queue.push(temp);
				Waiting.push_back(temp);
			}
			delete [ ]p;  //清空动态内存,In case of the memory leak
			//接下来处理窗口
	//		cout << Queue.size() << endl;
			for(j = 0; j < 5;j++)
			{
				cout << "窗口" << j+1;
				if(windows[j].Is_idle())
				{
					if(!Queue.empty())
					{
						Customer temp = Queue.front();
						Customer wtemp = Waiting.front();
						temp.wating_time = wtemp.wating_time;
						Queue.pop();
						Waiting.pop_front();
						windows[j].Customer_Information(temp);
					}
					else
					{
						windows[j].Service_state();
					}
				}
				else
				{
					windows[j].Customer_Information();
				}
				
			}
			cout << "目前等待的顾客人数为:" << Queue.size() << endl << endl;
		}
		for(itr = Waiting.begin() ;itr != Waiting.end() ;itr++)
		{
			(*itr).wating_time++;
		}
	}
	cout << "****" << Time << "个服务时间结束,以下是统计信息:" << "****" << endl;
	for(i = 0;i < 5;i++)
	{
		cout << "窗口" << i+1 << "处理的顾客数为:" << windows[i].Customer_amount() << endl;
		Total_amount += windows[i].Customer_amount();
		Total_Serve_time += windows[i].Customer_Serve_time();
		Total_Wait_time += windows[i].Customer_Wait_time();
	}
	for(itr = Waiting.begin() ;itr != Waiting.end() ;itr++)
	{
		Total_Wait_time += (*itr).wating_time;
	}
	cout << "柜台服务的顾客总数为:" << Total_amount << endl;
	cout << "顾客服务的平均时间为:" << (double)Total_Serve_time/(double)Total_amount << endl;
	cout << "顾客等待的平均时间为:" << (double)Total_Wait_time / (double)(Total_amount + Waiting.size() ) << endl;
  return 0;
}

 

posted @ 2013-02-06 00:46  中大黑熊  阅读(247)  评论(0编辑  收藏  举报