• 大小: 8.02MB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2023-11-03
  • 语言: 其他
  • 标签:

资源简介

m/m/1理发排队仿真,随机产生理发人进理发店进行理发,理发师根据来的先后顺序进行理发,从而算出各个理发人的时间,离开时间,理发师的利率效率,

资源截图

代码片段和文件信息

#include “EventTable.h“

void Delay(int time)
{
clock_t now = clock();
while ( clock()-now}

EventTable::EventTable()
{
_simulateTime = 150;
_waitNum = 0;
_currentTime = 0;
_totalWaitTime = 0;
_totalWorkTime = 0;
_totalConsumerNum = 0;
_maxLine = 0;
}

EventTable::EventTable(int time)
{
_simulateTime = time;
_waitNum = 0;
_currentTime = 0;
_totalWaitTime = 0;
_totalWorkTime = 0;
_totalConsumerNum = 0;
_maxLine = 0;
}

int EventTable::poissonRand(float lamda)
{
if (lamda==0)
{
return 0;
}
srand((unsigned int)time(0) + rand());
int result = 0;
while(result==0)
{
float r = rand() / float(RAND_MAX);
result = (int)(-log(r) * lamda);
}
return result;
}

void EventTable::makeTable()
{
Event e0 e1;
e0.type = 0;
e0.time = 0;
e1.type = 1;
e1.time = 0;
int tmpTime = 0;
int index = 0;
while (tmpTime<_simulateTime)
{
e0.index = index;
e0.time += poissonRand(5);
_arriveEvent.push_back(e0);
e1.index = index++;
e1.time = poissonRand(4);
_leaveEvent.push_back(e1);
tmpTime = e0.time;
}
_arriveEvent.erase(_arriveEvent.end()-1);
_leaveEvent.erase(_leaveEvent.end()-1);
}

void EventTable::simulate()
{
_currentTime = 0;
bool barberBusy = false;
int startWorkTime = 0;
int endWorkTime = 0;
int lastEventTime = 0;
int currentConsumer = 0;
int benchNum = 0;
//  vector bench;
Event event;// lastEvent;
/* vector::iterator iter;*/
while (_currentTime<_simulateTime && 0!=_leaveEvent.size())
{
if (_arriveEvent.size()>0 && _arriveEvent[0].time<=_leaveEvent[0].time)
event = _arriveEvent[0];
else if (currentConsumer==0)
event = _arriveEvent[0];
else
event = _leaveEvent[0];
_currentTime = event.time<=_simulateTime ? event.time : _simulateTime;
_totalWaitTime += benchNum * (_currentTime - lastEventTime);
lastEventTime = _currentTime;
//  if (_currentTime > _simulateTime)
//  break;
cout << “当前时间:“ << _currentTime << endl;
switch(event.type)
{
case 0:
cout << “第“ << event.index+1 << “位顾客到达。“;
if (barberBusy)
{
benchNum++;
if (benchNum > _maxLine)
_maxLine = benchNum;
//cout << “有“ << benchNum << “位顾客在等待。“ << endl;
}
else
{
benchNum = (benchNum==0) ? 0 : (--benchNum);
//cout << “第“ << event.index+1 << “位顾客在理发“ << endl;
barberBusy = true;
startWorkTime = _currentTime;
currentConsumer++;
_leaveEvent[0].time += event.time;
}
cout << “第“ << currentConsumer << “位顾客在理发,“ ;
cout << “有“ << benchNum << “位顾客在等待。“ << endl;
_arriveEvent.erase(_arriveEvent.begin());
break;
case 1:
_totalConsumerNum++;
barberBusy = false;
endWorkTime = _currentTime;
_totalWorkTime += endWorkTime - startWorkTime;
cout << “第“ << event.index+1 << “位顾客离开。“ ;
if (benchNum)
{
benchNum--;
barberBusy = true;
startWorkTime = _currentTime;
_leaveEvent[1].time += _c

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2015-07-07 10:19  simulate\
     目录           0  2015-07-07 10:27  simulate\simulate\
     目录           0  2015-07-07 10:19  simulate\simulate\Debug\
     文件       65024  2015-07-07 10:22  simulate\simulate\Debug\simulate.exe
     文件      492716  2015-07-07 10:22  simulate\simulate\Debug\simulate.ilk
     文件      945152  2015-07-07 10:22  simulate\simulate\Debug\simulate.pdb
     文件        1807  2015-07-07 10:19  simulate\simulate\UpgradeLog.xml
     目录           0  2015-07-07 10:19  simulate\simulate\_UpgradeReport_Files\
     文件        3348  2015-07-07 10:19  simulate\simulate\_UpgradeReport_Files\UpgradeReport.css
     文件       12505  2015-07-07 10:19  simulate\simulate\_UpgradeReport_Files\UpgradeReport.xslt
     文件          69  2015-07-07 10:19  simulate\simulate\_UpgradeReport_Files\UpgradeReport_Minus.gif
     文件          71  2015-07-07 10:19  simulate\simulate\_UpgradeReport_Files\UpgradeReport_Plus.gif
     目录           0  2015-07-07 10:26  simulate\simulate\ipch\
     目录           0  2015-07-07 10:26  simulate\simulate\ipch\simulate-5a5e3db9\
     文件    27656192  2015-07-07 10:26  simulate\simulate\ipch\simulate-5a5e3db9\simulate-2bbc8870.ipch
     目录           0  2015-07-07 10:19  simulate\simulate\simulate\
     目录           0  2015-07-07 10:22  simulate\simulate\simulate\Debug\
     文件        6308  2015-07-07 10:19  simulate\simulate\simulate\Debug\BuildLog.htm
     文件       24538  2015-07-07 10:22  simulate\simulate\simulate\Debug\CL.read.1.tlog
     文件        1102  2015-07-07 10:22  simulate\simulate\simulate\Debug\CL.write.1.tlog
     文件      175397  2015-07-07 10:22  simulate\simulate\simulate\Debug\EventTable.obj
     文件        1418  2015-07-07 10:22  simulate\simulate\simulate\Debug\cl.command.1.tlog
     文件           2  2015-07-07 10:22  simulate\simulate\simulate\Debug\link-cvtres.read.1.tlog
     文件           2  2015-07-07 10:22  simulate\simulate\simulate\Debug\link-cvtres.write.1.tlog
     文件        1324  2015-07-07 10:22  simulate\simulate\simulate\Debug\link.command.1.tlog
     文件        3058  2015-07-07 10:22  simulate\simulate\simulate\Debug\link.read.1.tlog
     文件         674  2015-07-07 10:22  simulate\simulate\simulate\Debug\link.write.1.tlog
     文件       50301  2015-07-07 10:22  simulate\simulate\simulate\Debug\main.obj
     文件           2  2015-07-07 10:19  simulate\simulate\simulate\Debug\mt.command.1.tlog
     文件          63  2015-07-07 10:19  simulate\simulate\simulate\Debug\mt.dep
     文件           2  2015-07-07 10:19  simulate\simulate\simulate\Debug\mt.read.1.tlog
............此处省略29个文件信息

评论

共有 条评论

相关资源