📄 esystemdlg.cpp
字号:
// ESystemDlg.cpp : implementation file
//
#include "stdafx.h"
#include "ESystem.h"
#include "ESystemDlg.h"
#include "Monitor.h"
#include "SysArgDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
public:
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CESystemDlg dialog
CESystemDlg::CESystemDlg(CWnd* pParent /*=NULL*/)
: CDialog(CESystemDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CESystemDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CESystemDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CESystemDlg)
DDX_Control(pDX, IDC_MESSAGES, m_ctrlMessages);
DDX_Control(pDX, IDC_SYSTEMTIMER, m_ctrlTimer);
DDX_Control(pDX, IDC_ENDPASSENGERS, m_ctrlEndPassengers);
DDX_Control(pDX, IDC_PASSENGERS, m_ctrlPassengers);
DDX_Control(pDX, IDC_EDIT3, m_arrELocation[0]);
DDX_Control(pDX, IDC_EDIT5, m_arrELocation[1]);
DDX_Control(pDX, IDC_EDIT7, m_arrELocation[2]);
DDX_Control(pDX, IDC_EDIT9, m_arrELocation[3]);
DDX_Control(pDX, IDC_EDIT11, m_arrELocation[4]);
DDX_Control(pDX, IDC_EDIT13, m_arrELocation[5]);
DDX_Control(pDX, IDC_EDIT15, m_arrELocation[6]);
DDX_Control(pDX, IDC_EDIT17, m_arrELocation[7]);
DDX_Control(pDX, IDC_EDIT19, m_arrELocation[8]);
DDX_Control(pDX, IDC_EDIT21, m_arrELocation[9]);
DDX_Control(pDX, IDC_EDIT4, m_arrEPListCount[0]);
DDX_Control(pDX, IDC_EDIT6, m_arrEPListCount[1]);
DDX_Control(pDX, IDC_EDIT8, m_arrEPListCount[2]);
DDX_Control(pDX, IDC_EDIT10, m_arrEPListCount[3]);
DDX_Control(pDX, IDC_EDIT12, m_arrEPListCount[4]);
DDX_Control(pDX, IDC_EDIT14, m_arrEPListCount[5]);
DDX_Control(pDX, IDC_EDIT16, m_arrEPListCount[6]);
DDX_Control(pDX, IDC_EDIT18, m_arrEPListCount[7]);
DDX_Control(pDX, IDC_EDIT20, m_arrEPListCount[8]);
DDX_Control(pDX, IDC_EDIT22, m_arrEPListCount[9]);
DDX_Control(pDX, IDC_EDIT23, m_arrEStatus[0]);
DDX_Control(pDX, IDC_EDIT34, m_arrEStatus[1]);
DDX_Control(pDX, IDC_EDIT35, m_arrEStatus[2]);
DDX_Control(pDX, IDC_EDIT36, m_arrEStatus[3]);
DDX_Control(pDX, IDC_EDIT37, m_arrEStatus[4]);
DDX_Control(pDX, IDC_EDIT38, m_arrEStatus[5]);
DDX_Control(pDX, IDC_EDIT39, m_arrEStatus[6]);
DDX_Control(pDX, IDC_EDIT40, m_arrEStatus[7]);
DDX_Control(pDX, IDC_EDIT41, m_arrEStatus[8]);
DDX_Control(pDX, IDC_EDIT42, m_arrEStatus[9]);
DDX_Control(pDX, IDC_EDIT24, m_arrEPList[0]);
DDX_Control(pDX, IDC_EDIT25, m_arrEPList[1]);
DDX_Control(pDX, IDC_EDIT26, m_arrEPList[2]);
DDX_Control(pDX, IDC_EDIT27, m_arrEPList[3]);
DDX_Control(pDX, IDC_EDIT28, m_arrEPList[4]);
DDX_Control(pDX, IDC_EDIT29, m_arrEPList[5]);
DDX_Control(pDX, IDC_EDIT30, m_arrEPList[6]);
DDX_Control(pDX, IDC_EDIT31, m_arrEPList[7]);
DDX_Control(pDX, IDC_EDIT32, m_arrEPList[8]);
DDX_Control(pDX, IDC_EDIT33, m_arrEPList[9]);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CESystemDlg, CDialog)
//{{AFX_MSG_MAP(CESystemDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_TIMER()
ON_COMMAND(ID_CONTROL_START, OnControlStart)
ON_COMMAND(ID_CONTROL_PAUSE, OnControlPause)
ON_COMMAND(ID_CONTROL_STOP, OnControlStop)
ON_COMMAND(ID_CONTROL_EXIT, OnControlExit)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CESystemDlg message handlers
BOOL CESystemDlg::OnInitDialog()
{
srand((unsigned)time(NULL));
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
void CESystemDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CESystemDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CESystemDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CESystemDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
// 该函数是MFC提供的时钟消息处理函数。也是主循环所在的函数。
// 完成所有乘客的轮询和电梯的运行。
//避免因为消息处理的时间过长而超过一秒
KillTimer(m_iTimer);
CString str1,str2,strTemp;
m_iCount++;
str1.Format("%4d",m_iCount);
m_ctrlTimer.SetWindowText(str1);
//随机的生成乘客对象,加入到乘客链表之中
if(m_iCount<(M*60))
{
for(int i=0;i<Random(3,1);i++)
{
if(m_iPassCount<N)
{
CPassenger * pps = new CPassenger(m_iPassCount+1,L);
ASSERT(pps!=NULL);
m_iPassCount++;
str1.Format("%4d",m_iPassCount);
m_ctrlPassengers.SetWindowText(str1);
m_hMonitor->m_pltPassengers->push_back(pps);
//把乘客加入到一层的乘客队列中去
m_hMonitor->GetFloor(1)->m_pltFPassengers->push_back(pps);
m_ctrlMessages.GetWindowText(str2);
strTemp.Format("乘客P%d来到国际贸易中心.\r\n\r\n",pps->GetPID());
str2 += strTemp;
m_ctrlMessages.SetWindowText(str2);
m_ctrlMessages.LineScroll(m_ctrlMessages.GetLineCount());
TRACE("系统增加一个乘客 ID = %d.\n",m_iPassCount);
}
}
}
//询问乘客的请求
PASSENGERLT::iterator it;
m_iEndCount=0;
for(it = m_hMonitor->m_pltPassengers->begin();
it != m_hMonitor->m_pltPassengers->end();
++it)
{
CPassenger* pas;
pas = *it;
if(pas->GetStatus()!=END) m_hMonitor->m_bFinished = false;
else m_iEndCount++;
CMessage * msg;
msg = m_hMonitor->InquirePassenger(pas);
if(msg!=NULL)
{
m_ctrlMessages.GetWindowText(str2);
strTemp.Format("发送请求:\r\n 在第F%d层乘客P%d发出到第F%d层的请求.\r\n",msg->OriFloor,msg->PID,msg->ObjFloor);
str2 += strTemp;
m_ctrlMessages.SetWindowText(str2);
m_ctrlMessages.LineScroll(m_ctrlMessages.GetLineCount());
if(m_hMonitor->DispatchMsg(msg))
{
pas->SetStatus(WAITING);//消息发送成功,乘客进入等待状态
}
}
}
str1.Format("%4d",m_iEndCount);
m_ctrlEndPassengers.SetWindowText(str1);
//对电梯进行管理
for(int i=0;i<10;i++)
{
m_hMonitor->GetElevator(i)->Manager();
}
//数据显示在屏幕上
PresentData();
m_iTimer = SetTimer(1,ZHQ,NULL);
//判断程序结束的条件
if(m_hMonitor->m_bFinished) OnControlStop();
else m_hMonitor->m_bFinished = true;
CDialog::OnTimer(nIDEvent);
}
void CESystemDlg::OnControlStart()
{
// TODO: Add your command handler code here
// 仿真启动
CSysArgDlg sysdlg;
sysdlg.DoModal();
K = sysdlg.m_K;
N = sysdlg.m_N;
M = sysdlg.m_M;
L = sysdlg.m_L;
S = sysdlg.m_S;
T = sysdlg.m_T;
ZHQ = sysdlg.m_ZHQ;
m_hMonitor = new CMonitor(K,N,M,L,S,T);
ASSERT(m_hMonitor!=NULL);
m_iTimer = SetTimer(1,ZHQ,NULL);
m_iCount=0;
m_iPassCount = 0;
}
void CESystemDlg::OnControlPause()
{
// TODO: Add your command handler code here
// 仿真暂停.
KillTimer(m_iTimer);
}
void CESystemDlg::OnControlStop()
{
// TODO: Add your command handler code here
//仿真结束
KillTimer(m_iTimer);
AfxMessageBox("所有乘客已经结束乘坐!");
Report();
}
void CESystemDlg::OnControlExit()
{
// TODO: Add your command handler code here
//退出主系统
CESystemDlg::OnOK();
}
void CESystemDlg::PresentData()
{
//呈现电梯运行时数据
CString strTemp[10],str;
str = "";
if(m_ctrlMessages.GetLineCount()>100) m_ctrlMessages.SetWindowText(str);
for(int i=0;i<10;i++)
{
strTemp[i]="";
//m_arrELocation[i].GetWindowText(strTemp[i]);
strTemp[i].Format("%3d",m_hMonitor->GetElevator(i)->GetLocation());
m_arrELocation[i].SetWindowText(strTemp[i]);
//m_arrEPListCount[i].GetWindowText(strTemp[i]);
strTemp[i].Format("%3d",m_hMonitor->GetElevator(i)->GetLtPassenger()->size());
m_arrEPListCount[i].SetWindowText(strTemp[i]);
strTemp[i]="";
switch(m_hMonitor->GetElevator(i)->GetStatus())
{
case IDLE:
strTemp[i].Format("空闲");
break;
case RUN:
strTemp[i].Format("运行");
break;
case WAIT:
strTemp[i].Format("等待");
break;
default:
strTemp[i]="";
break;
}
m_arrEStatus[i].SetWindowText(strTemp[i]);
//显示电梯内的乘客链表
PASSENGERLT::iterator it;
strTemp[i]="";
//m_arrEPList[i].GetWindowText(strTemp[i]);
for(it = m_hMonitor->GetElevator(i)->GetLtPassenger()->begin();
it != m_hMonitor->GetElevator(i)->GetLtPassenger()->end();
++it)
{
CString str;
str.Format("%d-%d-%d ",(*it)->GetPID(),(*it)->GetOriFloor(),(*it)->GetObjFloor());
strTemp[i]+=str;
}
m_arrEPList[i].SetWindowText(strTemp[i]);
}
}
void CESystemDlg::Report()
{
//一次仿真结束,对数据进行收集.
FILE* fp;
fp = fopen("result.txt","wr");
char temp[81];
temp[0] = '\0';
if(fp!=NULL)
{
sprintf(temp,"本次运行时间为%5d秒\n 运行参数 K =%d;N = %d;M = %d;L = %d;S = %d;T = %d;\n\n",
m_iCount,K,N,M,L,S,T);
fwrite(temp,sizeof(char),strlen(temp),fp);
temp[0] = '\0';
sprintf(temp,"电梯标识 运行时间 空闲时间\n");
fwrite(temp,sizeof(char),strlen(temp),fp);
for(int i=0;i<10;i++)
{
temp[0] = '\0';
sprintf(temp,"%3d %4d %4d\n",
i,
m_hMonitor->GetElevator(i)->GetTotalRun(),
m_hMonitor->GetElevator(i)->GetTotalIdle());
fwrite(temp,sizeof(char),strlen(temp),fp);
}
sprintf(temp,"\n\n乘客标识 等待时间\n");
fwrite(temp,sizeof(char),strlen(temp),fp);
CPassengerList::iterator it;
int SumWaitTime = 0;
float Even = 0; //乘客平均等待时间
for(it = m_hMonitor->m_pltPassengers->begin();
it != m_hMonitor->m_pltPassengers->end();
++it)
{
CPassenger* pas;
pas = *it;
temp[0] = '\0';
sprintf(temp,"%3d %4d\n",
pas->GetPID(),
pas->GetWaitTimer());
SumWaitTime += pas->GetWaitTimer();
fwrite(temp,sizeof(char),strlen(temp),fp);
}
Even = SumWaitTime/N;
sprintf(temp,"\n\n乘客平均等待时间 = %f\n",Even);
fwrite(temp,sizeof(char),strlen(temp),fp);
}
fclose(fp);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -