www.pudn.com > 哲学家进餐问题.rar > 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" 
 
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) 
{ 
	//{{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 
} 
 
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 
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 
	 
	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; 
	 
	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() 
{ 
	for(int i = 0;iThinkerCnt;    
	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; 
		Sleep(800 + 20*(m_ph->m_speed) + GetRandTime()); 
		 
		//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 
				#ifdef DEBUGING 
				TRACE("the %d ID is eating\n ",ID); 
				#endif 
 
				m_ph->thinkerStatus[ID] = EATING_STATUS; 
				 
				m_ph->tropicStatus[(ID + THINKER_NUM -1)%THINKER_NUM] = 1; 
				m_ph->tropicStatus[ID] = 1; 
 
				::InvalidateRect(m_ph->m_view,m_ph->userect[ID],TRUE); 
				::InvalidateRect(m_ph->m_view,m_ph->Idlerect[ID],TRUE); 
				::InvalidateRect(m_ph->m_view,m_ph->Idlerect[(ID + THINKER_NUM -1)%THINKER_NUM],TRUE); 
				::InvalidateRect(m_ph->m_view,m_ph->thinkerRect[ID],TRUE); 
								 
 
				Sleep(800 +25*(m_ph->m_speed) + GetRandTime()); 
				 
 
				//cast the tropic and continue to think 
					m_ph->thinkerStatus[ID] = THINKING_STATUS; 
				m_ph->tropicStatus[(ID + THINKER_NUM -1)%THINKER_NUM] = 0; 
				m_ph->tropicStatus[ID] = 0; 
				::InvalidateRect(m_ph->m_view,m_ph->userect[ID],TRUE); 
				::InvalidateRect(m_ph->m_view,m_ph->Idlerect[ID],TRUE); 
				::InvalidateRect(m_ph->m_view,m_ph->Idlerect[(ID + THINKER_NUM -1)%THINKER_NUM],TRUE); 
				::InvalidateRect(m_ph->m_view,m_ph->thinkerRect[ID],TRUE); 
				ReleaseMutex(m_ph->tropics[(ID + THINKER_NUM -1)%THINKER_NUM]); 
				ReleaseMutex(m_ph->tropics[ID]); 
				 
				TRACE(" the %d ID is exiting ",ID); 
				 
				 
		}	 
	m_ph->ThinkerCnt--; 
	m_ph->thinkerStatus[ID] = NORMAL_STATUS; 
	::InvalidateRect(m_ph->m_view,m_ph->thinkerRect[ID],TRUE); 
	if(m_ph->ThinkerCnt == -1) 
		ResetEvent(m_ph->ThreadExit); 
	return 1; 
} 
 
void CEatingDlg::OnBtnBegin()  
{ 
	int i; 
	if(!m_HasInit) 
		InitStruct(); 
 
	for(i = 0;i