📄 eatingdlg.cpp
字号:
// eatingDlg.cpp : implementation file
//
#include "stdafx.h"
#include "eating.h"
#include "eatingDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define DEBUGING
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
#include "math.h"
#include "SpecialDlg.h"
#include ".\eatingdlg.h"
extern CPoint ta5[5]; //定义五个变量表示哲学家所在位置
extern CPoint tropic5[5];
extern CPoint usetropic[5];
extern int radius;
extern CPoint CenterPoint;
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)
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()
/////////////////////////////////////////////////////////////////////////////
// CEatingDlg dialog
CEatingDlg::CEatingDlg(CWnd* pParent /*=NULL*/)
: CDialog(CEatingDlg::IDD, pParent)
, m_Clock(0)
{
//{{AFX_DATA_INIT(CEatingDlg)
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CEatingDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CEatingDlg)
DDX_Control(pDX, IDC_SLIDER1, m_AdjustSpeed);
DDX_Control(pDX, IDOK, m_okBtn);
DDX_Control(pDX, IDC_BTN_STOP, m_stopBtn);
DDX_Control(pDX, IDC_BTN_BEGIN, m_beginBtn);
DDX_Control(pDX, IDC_STATIC_VIEW, m_wndView);
//}}AFX_DATA_MAP
DDX_Control(pDX, IDC_RADIO1, m_Strategy1);
DDX_Control(pDX, IDC_LIST1, m_Process);
DDX_Text(pDX,IDC_EDIT1,m_Clock);
DDX_Control(pDX, IDC_LIST2, m_Result);
}
BEGIN_MESSAGE_MAP(CEatingDlg, CDialog)
//{{AFX_MSG_MAP(CEatingDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BTN_BEGIN, OnBtnBegin)
ON_BN_CLICKED(IDC_BTN_STOP, OnBtnStop)
ON_WM_DESTROY()
ON_NOTIFY(NM_OUTOFMEMORY, IDC_SLIDER1, OnOutofmemorySlider1)
ON_NOTIFY(NM_CUSTOMDRAW, IDC_SLIDER1, OnCustomdrawSlider1)
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
//}}AFX_MSG_MAP
ON_BN_CLICKED(IDOK, OnBnClickedOk)
ON_BN_CLICKED(IDC_RADIO1, OnBnClickedRadio1)
ON_BN_CLICKED(IDC_RADIO2, OnBnClickedRadio2)
ON_BN_CLICKED(IDC_RADIO3, OnBnClickedRadio3)
ON_BN_CLICKED(IDC_BUTTON2, OnBnClickedButton2)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CEatingDlg message handlers
BOOL CEatingDlg::OnInitDialog()
{
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)
{
pSysMenu->EnableMenuItem(SC_CLOSE,MF_BYCOMMAND|MF_DISABLED|MF_GRAYED);
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
m_Strategy=1;
m_Strategy1.SetCheck(1);
Clock=0; //全局时钟清零
InitStruct(); //初始化结构体
m_thinker.m_speed = 20;
m_wndView.SetThinkersInfo(&m_thinker);
m_wndView.SetGradientColor(RGB(0,0,0));
font.CreatePointFont(160,"华文行楷");
m_wndView.SetFont(&font);
m_AdjustSpeed.SetRange(0,100);
m_AdjustSpeed.SetPos(80);
//m_AdjustSpeed.SetTic(10);
m_AdjustSpeed.SetTicFreq(200);
// TODO: Add extra initialization here
CRect rect;
int i;
radius = 160;
m_wndView.GetClientRect(&rect);
CenterPoint = rect.CenterPoint();
for(i = 0;i<5;i++)
{
ta5[i] = CPoint(radius*cos((90 + i*72)*3.141526/180),
radius*sin((90 + i*72)*3.141526/180));
ta5[i].x += CenterPoint.x;
ta5[i].y = CenterPoint.y - ta5[i].y;
}
for(i = 0;i<5;i++)
{
tropic5[i] = CPoint((radius-10)*cos((126 + i*72)*3.1415926/180),
(radius-10)*sin((126 + i*72)*3.1415926/180));
tropic5[i].x += CenterPoint.x;
tropic5[i].y = CenterPoint.y - tropic5[i].y;
}
for(i = 0;i<5;i++)
{
usetropic[i] = CPoint((radius-10)*cos((90 + i*72)*3.141526/180),
(radius-10)*sin((90 + i*72)*3.141526/180));
usetropic[i].x += CenterPoint.x;
usetropic[i].y = CenterPoint.y - usetropic[i].y;
}
m_thinker.thinkerRect[0].SetRect(ta5[0].x,ta5[0].y - 60,
ta5[0].x,ta5[0].y - 60);
m_thinker.thinkerRect[0].InflateRect(60,20);
m_thinker.thinkerRect[1].SetRect(ta5[1].x-85,ta5[1].y,
ta5[1].x-85,ta5[1].y);
m_thinker.thinkerRect[1].InflateRect(60,20);
m_thinker.thinkerRect[2].SetRect(ta5[2].x-20,ta5[2].y+50,
ta5[2].x-20,ta5[2].y+50);
m_thinker.thinkerRect[2].InflateRect(60,20);
m_thinker.thinkerRect[3].SetRect(ta5[3].x+10,ta5[3].y+50,
ta5[3].x+10,ta5[3].y+50);
m_thinker.thinkerRect[3].InflateRect(60,20);
m_thinker.thinkerRect[4].SetRect(ta5[4].x+85,ta5[4].y,
ta5[4].x+85,ta5[4].y);
m_thinker.thinkerRect[4].InflateRect(60,20);
m_stopBtn.EnableWindow(FALSE);
m_HasInit = TRUE;
m_time=0; //运行次数与重复删除法相关
for(int k=0;k<5;k++)
{
Total_Think[k]=0;
Total_Eat[k]=0;
Total_Wait[k]=0;
}
return TRUE; // return TRUE unless you set the focus to a control
}
void CEatingDlg::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 CEatingDlg::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 CEatingDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CEatingDlg::InitStruct()
{
m_thinker.thinkerStatus[0] = THINKING_STATUS;
m_thinker.tropics[0] = CreateMutex(NULL,FALSE,NULL); //初始化MUTEX变量
m_thinker.tropicStatus[0] = 1; //筷子空闲
m_thinker.thinkerStatus[1] = EATING_STATUS;
m_thinker.tropics[1] = CreateMutex(NULL,FALSE,NULL); //初始化MUTEX变量
m_thinker.tropicStatus[1] = 1; //筷子空闲
m_thinker.thinkerStatus[2] = THINKING_STATUS;
m_thinker.tropics[2] = CreateMutex(NULL,FALSE,NULL); //初始化MUTEX变量
m_thinker.tropicStatus[2] = 1; //筷子空闲
m_thinker.thinkerStatus[3] = EATING_STATUS;
m_thinker.tropics[3] = CreateMutex(NULL,FALSE,NULL); //初始化MUTEX变量
m_thinker.tropicStatus[3] = 1; //筷子空闲
m_thinker.thinkerStatus[4] = THINKING_STATUS;
m_thinker.tropics[4] = CreateMutex(NULL,FALSE,NULL); //初始化MUTEX变量
m_thinker.tropicStatus[4] = 0; //筷子空闲
m_thinker.m_view = m_wndView.GetSafeHwnd();
m_thinker.ThinkerCnt = -1;
m_thinker.ThreadExit = CreateEvent(NULL,TRUE,FALSE,NULL); //初始化为没有被使用,可用
}
int GetRandTime(void)
{
srand((unsigned)time(NULL));
return rand()%500;
}
UINT Eating(PVOID pParam)
{
pThinkers m_ph = (pThinkers)pParam;
int ID = m_ph->ThinkerCnt;
while(WaitForSingleObject(m_ph->ThreadExit,0) != WAIT_OBJECT_0)
{
// Thinking
#ifdef DEBUGING
TRACE("the %d ID is thinking\n ",ID);
#endif
//::InvalidateRgn(m_ph->m_view,m_ph->rect,TRUE);
m_ph->thinkerStatus[ID] = THINKING_STATUS;
int m=GetRandTime();
Sleep(800 + 20*(m_ph->m_speed) + m);
//hungry
#ifdef DEBUGING
TRACE("the %d ID is hungry \n",ID);
#endif
m_ph->thinkerStatus[ID] = HUNGRY_STATUS;
::InvalidateRect(m_ph->m_view,m_ph->thinkerRect[ID],TRUE);
while(TRUE)
{
if(WaitForSingleObject(m_ph->tropics[(ID + THINKER_NUM -1)%THINKER_NUM],60+GetRandTime()) == WAIT_OBJECT_0) //得到了左边的筷子
{
if(WaitForSingleObject(m_ph->tropics[ID],100) != WAIT_OBJECT_0)
ReleaseMutex(m_ph->tropics[(ID + THINKER_NUM -1)%THINKER_NUM]);//如果没有得到右边的筷子,就释放自己获得的左边的筷子,等待
else
break;
}
Sleep(600 + 10*(m_ph->m_speed)+GetRandTime());
}
//begin to eating
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -