⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 simulation.h

📁 这是一个银行模拟系统
💻 H
字号:


#ifndef Simulation_H
#define Simulation_H

#include <iostream>
#include <cstdlib>
#include <time.h>
using namespace std;
#include  "LinkedQueue.h"

 
//定义一个银行柜台操作类Teller
class Teller
{
public:
       int totalCustomerCount;  //模拟需要总的顾客数;
       int totalServiceTime;  //模拟需要总的服务时间;
       int finishServiceTime; //模拟完成所需要的时间;
       Teller() :totalCustomerCount(0), totalServiceTime(0),  //初始化使totalCustomerCount=0,
		    finishServiceTime(0) {}   //totalServiceTime=0 和finishServiceTime=0;

}; //Teller结束

//file://#define/ PRINTPROCESS

class Simulation  //定义一个叫Simulation的类
{
public:
	Simulation() //不带参数的构造函数,作用是输入模拟参数
	{
              cout << endl << "输入你要模拟的参数" << endl; 
              cout << "柜台数量:"; 			  
			  cin >> tellerNum;       //tellerNum为柜台数量

              cout << "营业时间:";			  
			  cin >> simuTime;        // simuTime为营业时间

              cout << "两个顾客来到的最小间隔时间:"; 			  
			  cin >> arrivalLow;  //arrivalLow为两个顾客来到的最小间隔时间

              cout << "两个顾客来到的最大间隔时间:"; 			  
			  cin >> arrivalHigh;  //arrivalHigh为两个顾客来到的最大间隔时间

              cout << "柜台服务最短时间:"; 			  
			  cin >> serviceLow;  //serviceLow为柜台服务最短时间

              cout << "柜台服务最长时间:"; 			  
			  cin >> serviceHigh;  //serviceHigh为柜台服务最长时间

              arrivalRange = arrivalHigh - arrivalLow + 1; //两个顾客来到在最大和最小间隔时间之间

              serviceRange = serviceHigh - serviceLow + 1; //柜台服务在最短和最长时间之间 ;

              srand((unsigned)time(NULL));  // 随机产生顾客到来的时间 并且time是在头文件中定义
       }

 
       Simulation(int tellerNum, int simuTime, int arrivalLow,  //带有参数的构造函数,
		   int arrivalHigh, int serviceLow, int serviceHigh)
              : tellerNum(tellerNum), simuTime(simuTime),      //并且初始化左边对应的变量;
			  arrivalLow(arrivalLow), arrivalHigh(arrivalHigh),
              serviceLow(serviceLow), serviceHigh(serviceHigh),
              arrivalRange(arrivalHigh - arrivalLow + 1), 
			  serviceRange(serviceHigh - serviceLow + 1)
       { srand((unsigned)time(NULL)); }   //随机产生顾客到来的时间

 

       void Initialize()    //定义一个成员函数Initialize() 它的作用初始化参数变量
       {
              curTime = nextTime = 0;  //curTime为时钟,初始值为0
              customerNum = customerTime = 0; //顾客数和服务时间初始值为0;
              for (int i = 1; i <= tellerNum; i++) //初始化每个柜台对应的变量
              {
                     tellers[i].totalCustomerCount = 0; //第i个柜台总的顾客数                     
					 tellers[i].totalServiceTime = 0;   //第i个柜台总的服务时间                     
                     tellers[i].finishServiceTime = 0;  //第i个柜台完成时间
              }
              customer.MakeEmpty();     //初始化customer队列为空
       }

 
       void Run()
       {
              Initialize();   //调用成员函数 Initialize()          
              NextArrived();  //调用私有成员函数  NextArrived(); 
//#ifdef PRINTPROCESS
              cout << endl;
              cout << "tellerID";

              for (int k = 1; k <= tellerNum; k++) 
				  cout << "\tTELLER " << k;
              cout << endl;
//#endif
              for (curTime = 0; curTime <= simuTime; curTime++)
              {
                     if (curTime >= nextTime)
                     {
                            CustomerArrived();
                            NextArrived();
                     }
//#ifdef PRINTPROCESS            
                     cout << "Time: " << curTime << "      ";
//#endif
                   for (int i = 1; i <= tellerNum; i++)
                   {
                            if (tellers[i].finishServiceTime < curTime) 
								
								tellers[i].finishServiceTime = curTime;

                            if (tellers[i].finishServiceTime == curTime && !customer.IsEmpty())
                            {
                                   int t = NextService();
//#ifdef PRINTPROCESS            
                                   cout << '\t' << customerNum  << '(' 
									     << customer.GetFront() << ',' << t << ')';
//#endif
                                   CustomerDeparture();
                                   tellers[i].totalCustomerCount++;
                                   tellers[i].totalServiceTime += t;
                                   tellers[i].finishServiceTime += t;
                            }
//#ifdef PRINTPROCESS            
                            else cout << "\t        ";
//#endif

				   }

//#ifdef PRINTPROCESS
                     cout << endl;
//#endif
              }
              PrintResult();
       }


       void PtintSimuPara()
       { //输出主要的模拟参数

              cout << endl << "模拟参数" << endl;

              cout << "柜台数量: " << tellerNum 
				    <<"\t营业时间:" << simuTime << endl;

              cout << "两个顾客来到的最小间隔时间:" << arrivalLow << endl;

              cout << "两个顾客来到的最大间隔时间:" << arrivalHigh << endl;;

              cout << "柜台服务最短时间:" << serviceLow << endl;

              cout << "柜台服务最长时间:" << serviceHigh << endl;
       }


       void PrintResult()
       { //打印模拟结果
              int tSN = 0; //累加器,计算柜台总的顾客数
              long tST = 0; //累加器,计算柜台总的服务时间
              cout << endl;
              cout << "-------------模拟结果-------------------";
              cout << endl << "tellerID\tServiceNum\tServiceTime\tAverageTime" << endl;
              for (int i = 1; i <= tellerNum; i++)
              { //输出每个柜台的总的顾客数,总的服务时间,平均服务时间
                     cout << "TELLER " << i;
                     cout << '\t' << tellers[i].totalCustomerCount << "        "; 
					 tSN += tellers[i].totalCustomerCount;

                     cout << '\t' << tellers[i].totalServiceTime << "       "; 
					 tST += (long)tellers[i].totalServiceTime;

                     cout << '\t';

                     if (tellers[i].totalCustomerCount) //平均服务时间
                            cout << (float)tellers[i].totalServiceTime/(float)tellers[i].totalCustomerCount;
                     else cout << 0;
                     cout << "        " << endl;
              }

              cout << "TOTAL   \t" << tSN << "       \t" << tST << "       \t";

              if (tSN) cout << (float)tST/(float)tSN; 			 
			  else cout << 0;
              cout << "        " << endl;
              // 输出顾客总数,没有受服务的顾客数
              cout << "Customer Number:\t" << customerNum << "\tno Service:\t" 
				   << customerNum - tSN << endl;
              //顾客需要等待的时间和顾客需要的平均等待时间
              cout <<   "Customer WaitTime:\t" << customerTime << "\tAvgWaitTime:\t";

             if (tSN) cout << (float)customerTime/(float)tSN; 
			 else 
				 cout << 0;
			 cout<<"\n";			 
			 cout<<"-----------------------------------------------------------------------\n";
             cout << endl;
       }
private:
       int tellerNum;
       int simuTime;
       int curTime, nextTime;
       int customerNum;
       long customerTime;
       int arrivalLow, arrivalHigh, arrivalRange;
       int serviceLow, serviceHigh, serviceRange;
       Teller tellers[21];
       LinkedQueue<int>customer;	 
       void NextArrived()
       {  //计算下一个顾客到来的时间
             nextTime += arrivalLow + rand() % arrivalRange;

       }

        int NextService()
       {
            return serviceLow + rand() % serviceRange;

       }

        void CustomerArrived()
	   {
		   customerNum++;
		   customer.EnQueue(nextTime);
       }
       void CustomerDeparture()
       {
		   customerTime += (long)curTime - (long)customer.DeQueue();

       }
};

#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -