📄 philosophyview.cpp
字号:
// PhilosophyView.cpp : CPhilosophyView 类的实现
//
#include "stdafx.h"
#include "Philosophy.h"
#include "PhilosophyDoc.h"
#include "PhilosophyView.h"
#include "afxmt.h"
#include "OptionDlg.h"
#include ".\philosophyview.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
CEvent StartTime,StartThread1,StartThread2,StartThread3,StartThread4,StartThread5,Apply,StartApply[5],StopApply[5],Pause;
UINT_PTR m_timer;
int timer=1,flag[5]={0},ex[5],tx[5],timespan=3000,eatingtime=50,thinkingtime=20,thread;
CWinThread *hWnd1,*hWnd2,*hWnd3,*hWnd4,*hWnd5,*htime,*hrand;
PLSP philo[5];
CBitmap b_bitmap[3];
CBrush color,*preserve;
CPen pen,*reserve,repen,*save;
CPoint pos[5],mpos[5],lpos[5];
bool c[5],light,mode,begin;
CString string="0",stringchair="4",et[5],tt[5];
int chock[5];
int chair;
// CPhilosophyView
IMPLEMENT_DYNCREATE(CPhilosophyView, CView)
BEGIN_MESSAGE_MAP(CPhilosophyView, CView)
// 标准打印命令
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
ON_COMMAND(ID_StartThread, OnStartthread)
ON_WM_TIMER()
ON_COMMAND(ID_StopThread, OnStopthread)
ON_COMMAND(ID_Options, OnOptions)
ON_COMMAND(ID_CONTINUE, OnContinue)
END_MESSAGE_MAP()
// CPhilosophyView 构造/销毁
CPhilosophyView::CPhilosophyView()
{
// TODO: 在此处添加构造代码
philo[0].name="马克思";
philo[0].left=0;
philo[0].right=0;
philo[0].status=1;
philo[1].name="亚里士多德";
philo[1].left=0;
philo[1].right=0;
philo[1].status=1;
philo[2].name="苏格拉底";
philo[2].left=0;
philo[2].right=0;
philo[2].status=1;
philo[3].name="柏拉图";
philo[3].left=0;
philo[3].right=0;
philo[3].status=1;
philo[4].name="孟德斯鸠";
philo[4].left=0;
philo[4].right=0;
philo[4].status=1;
chock[0]=1;
chock[1]=1;
chock[2]=1;
chock[3]=1;
chock[4]=1;
chair=4;
c[0]=c[1]=c[2]=c[3]=c[4]=light=begin=false;
mode=true;
b_bitmap[0].LoadBitmap(IDB_HUNGRY);
b_bitmap[1].LoadBitmap(IDB_THINK);
b_bitmap[2].LoadBitmap(IDB_EAT);
color.CreateSolidBrush(RGB(00,170,00));
pen.CreatePen(PS_SOLID,1,RGB(0,0,255));
repen.CreatePen(PS_SOLID,1,RGB(00,170,00));
pos[0].x=335;pos[0].y=80;
pos[1].x=350+(int)(0.95105651629515357211643933337938*110);pos[1].y=220-(int)(0.30901699437494742410229341718282*110);
pos[2].x=350+(int)(0.58778525229247312916870595463907*103);pos[2].y=220+(int)(0.80901699437494742410229341718282*103);
pos[3].x=350-(int)(0.70710678118654752440084436210485*125);pos[3].y=220+(int)(0.70710678118654752440084436210485*125);
pos[4].x=350-(int)(0.95105651629515357211643933337938*145);pos[4].y=220-(int)(0.30901699437494742410229341718282*145);
mpos[0].x=350+(int)(0.58778525229247312916870595463907*30);mpos[0].y=220-(int)(0.80901699437494742410229341718282*30);
mpos[1].x=350+(int)(0.95105651629515357211643933337938*30);mpos[1].y=220+(int)(0.30901699437494742410229341718282*30);
mpos[2].x=350;mpos[2].y=250;
mpos[3].x=350-(int)(0.95105651629515357211643933337938*30);mpos[3].y=220+(int)(0.30901699437494742410229341718282*30);
mpos[4].x=350-(int)(0.58778525229247312916870595463907*30);mpos[4].y=220-(int)(0.80901699437494742410229341718282*30);
lpos[0].x=350+(int)(0.58778525229247312916870595463907*80);lpos[0].y=220-(int)(0.80901699437494742410229341718282*80);
lpos[1].x=350+(int)(0.95105651629515357211643933337938*80);lpos[1].y=220+(int)(0.30901699437494742410229341718282*80);
lpos[2].x=350;lpos[2].y=300;
lpos[3].x=350-(int)(0.95105651629515357211643933337938*80);lpos[3].y=220+(int)(0.30901699437494742410229341718282*80);
lpos[4].x=350-(int)(0.58778525229247312916870595463907*80);lpos[4].y=220-(int)(0.80901699437494742410229341718282*80);
ex[0]=tx[0]=pos[0].x+100;
ex[1]=tx[1]=pos[1].x+100;
ex[2]=tx[2]=pos[2].x+100;
ex[3]=tx[3]=pos[3].x-170;
ex[4]=tx[4]=pos[4].x-170;
et[0]=et[1]=et[2]=et[3]=et[4]=tt[0]=tt[1]=tt[2]=tt[3]=tt[4]="0% ";
}
CPhilosophyView::~CPhilosophyView()
{
}
BOOL CPhilosophyView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: 在此处通过修改 CREATESTRUCT cs 来修改窗口类或
// 样式
return CView::PreCreateWindow(cs);
}
// CPhilosophyView 绘制
void CPhilosophyView::OnDraw(CDC* pDC)
{
CPhilosophyDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
AfxGetMainWnd()->SetWindowText(_T("哲学家问题"));
pDC->TextOut(300,30,string);
pDC->TextOut(480,30,stringchair);
pDC->Rectangle(pos[0].x+100,pos[0].y,pos[0].x+200,pos[0].y+10);
pDC->Rectangle(pos[0].x+100,pos[0].y+13,pos[0].x+200,pos[0].y+23);
pDC->Rectangle(pos[1].x+100,pos[1].y,pos[1].x+200,pos[1].y+10);
pDC->Rectangle(pos[1].x+100,pos[1].y+13,pos[1].x+200,pos[1].y+23);
pDC->Rectangle(pos[2].x+100,pos[2].y,pos[2].x+200,pos[2].y+10);
pDC->Rectangle(pos[2].x+100,pos[2].y+13,pos[2].x+200,pos[2].y+23);
pDC->Rectangle(pos[3].x-170,pos[3].y,pos[3].x-70,pos[3].y+10);
pDC->Rectangle(pos[3].x-170,pos[3].y+13,pos[3].x-70,pos[3].y+23);
pDC->Rectangle(pos[4].x-170,pos[4].y,pos[4].x-70,pos[4].y+10);
pDC->Rectangle(pos[4].x-170,pos[4].y+13,pos[4].x-70,pos[4].y+23);
pDC->SelectStockObject(BLACK_BRUSH);
pDC->Rectangle(pos[0].x+100,pos[0].y,ex[0],pos[0].y+10);
pDC->Rectangle(pos[1].x+100,pos[1].y,ex[1],pos[1].y+10);
pDC->Rectangle(pos[2].x+100,pos[2].y,ex[2],pos[2].y+10);
pDC->Rectangle(pos[3].x-170,pos[3].y,ex[3],pos[3].y+10);
pDC->Rectangle(pos[4].x-170,pos[4].y,ex[4],pos[4].y+10);
pDC->Rectangle(pos[0].x+100,pos[0].y+13,tx[0],pos[0].y+23);
pDC->Rectangle(pos[1].x+100,pos[1].y+13,tx[1],pos[1].y+23);
pDC->Rectangle(pos[2].x+100,pos[2].y+13,tx[2],pos[2].y+23);
pDC->Rectangle(pos[3].x-170,pos[3].y+13,tx[3],pos[3].y+23);
pDC->Rectangle(pos[4].x-170,pos[4].y+13,tx[4],pos[4].y+23);
pDC->SelectStockObject(NULL_BRUSH);
pDC->TextOut(pos[0].x+210,pos[0].y-5,et[0]);
pDC->TextOut(pos[1].x+210,pos[1].y-5,et[1]);
pDC->TextOut(pos[2].x+210,pos[2].y-5,et[2]);
pDC->TextOut(pos[3].x-60,pos[3].y-5,et[3]);
pDC->TextOut(pos[4].x-60,pos[4].y-5,et[4]);
pDC->TextOut(pos[0].x+210,pos[0].y+10,tt[0]);
pDC->TextOut(pos[1].x+210,pos[1].y+10,tt[1]);
pDC->TextOut(pos[2].x+210,pos[2].y+10,tt[2]);
pDC->TextOut(pos[3].x-60,pos[3].y+10,tt[3]);
pDC->TextOut(pos[4].x-60,pos[4].y+10,tt[4]);
pDC->TextOut(pos[0].x+100,pos[0].y-20,"Eating Time...");
pDC->TextOut(pos[1].x+100,pos[1].y-20,"Eating Time...");
pDC->TextOut(pos[2].x+100,pos[2].y-20,"Eating Time...");
pDC->TextOut(pos[3].x-170,pos[3].y-20,"Eating Time...");
pDC->TextOut(pos[4].x-170,pos[4].y-20,"Eating Time...");
pDC->TextOut(pos[0].x+100,pos[0].y+25,"Thinking Time...");
pDC->TextOut(pos[1].x+100,pos[1].y+25,"Thinking Time...");
pDC->TextOut(pos[2].x+100,pos[2].y+25,"Thinking Time...");
pDC->TextOut(pos[3].x-170,pos[3].y+25,"Thinking Time...");
pDC->TextOut(pos[4].x-170,pos[4].y+25,"Thinking Time...");
pDC->SelectStockObject(NULL_PEN);
preserve=pDC->SelectObject(&color);
pDC->Ellipse(250,120,450,320);
pDC->SelectObject(preserve);
reserve=pDC->SelectObject(&pen);
pDC->MoveTo(mpos[0].x,mpos[0].y);
pDC->LineTo(lpos[0].x,lpos[0].y);
pDC->MoveTo(mpos[4].x,mpos[4].y);
pDC->LineTo(lpos[4].x,lpos[4].y);
pDC->MoveTo(mpos[2].x,mpos[2].y);
pDC->LineTo(lpos[2].x,lpos[2].y);
pDC->MoveTo(mpos[1].x,mpos[1].y);
pDC->LineTo(lpos[1].x,lpos[1].y);
pDC->MoveTo(mpos[3].x,mpos[3].y);
pDC->LineTo(lpos[3].x,lpos[3].y);
pDC->SelectObject(reserve);
pDC->TextOut(220,0,"[****哲学家问题例示(信号量实现)****]");
pDC->TextOut(250,30,"时钟:");
pDC->TextOut(400,30,"椅子数:");
pDC->TextOut(330,60,philo[0].name);
pDC->DrawState(pos[0],CSize(32,32),(HBITMAP)b_bitmap[philo[0].status],0,(HBRUSH)0);
pDC->TextOut(350+(int)(0.95105651629515357211643933337938*110),220-(int)(0.30901699437494742410229341718282*110-32),philo[1].name);
pDC->DrawState(pos[1],CSize(32,32),(HBITMAP)b_bitmap[philo[1].status],0,(HBRUSH)0);
pDC->TextOut(350-(int)(0.95105651629515357211643933337938*145+30),220-(int)(0.30901699437494742410229341718282*145-35),philo[4].name);
pDC->DrawState(pos[4],CSize(32,32),(HBITMAP)b_bitmap[philo[4].status],0,(HBRUSH)0);
pDC->TextOut(350+(int)(0.58778525229247312916870595463907*103-10),220+(int)(0.80901699437494742410229341718282*103+35),philo[2].name);
pDC->DrawState(pos[2],CSize(32,32),(HBITMAP)b_bitmap[philo[2].status],0,(HBRUSH)0);
pDC->TextOut(350-(int)(0.70710678118654752440084436210485*125),220+(int)(0.70710678118654752440084436210485*125+35),philo[3].name);
pDC->DrawState(pos[3],CSize(32,32),(HBITMAP)b_bitmap[philo[3].status],0,(HBRUSH)0);
pDC->DrawState(CPoint(20,399),CSize(32,32),(HBITMAP)b_bitmap[1],0,(HBRUSH)0);
pDC->TextOut(52,410,"代表哲学家处于思考状态");
pDC->DrawState(CPoint(270,399),CSize(32,32),(HBITMAP)b_bitmap[2],0,(HBRUSH)0);
pDC->TextOut(302,410,"代表哲学家处于进食状态");
pDC->DrawState(CPoint(520,399),CSize(32,32),(HBITMAP)b_bitmap[0],0,(HBRUSH)0);
pDC->TextOut(552,410,"代表哲学家处于饥饿状态");
pDC->TextOut(0,30,"申请信号");
pDC->SelectStockObject(BLACK_PEN);
CBrush lighton,lightoff,*old;
lighton.CreateSolidBrush(RGB(0,255,0));
lightoff.CreateSolidBrush(RGB(255,0,0));
if(light)
old=pDC->SelectObject(&lighton);
else
old=pDC->SelectObject(&lightoff);
pDC->Ellipse(80,30,100,50);
pDC->SelectObject(old);
CPen *oldpen=(CPen *)pDC->SelectStockObject(BLACK_PEN);
pDC->MoveTo(pos[(timer-2)%5].x-5,pos[(timer-2)%5].y);
pDC->LineTo(pos[(timer-2)%5].x-5,pos[(timer-2)%5].y+32);
pDC->MoveTo(pos[(timer-2)%5].x+37,pos[(timer-2)%5].y);
pDC->LineTo(pos[(timer-2)%5].x+37,pos[(timer-2)%5].y+32);
pDC->SelectObject(oldpen);
Pause.SetEvent();
// TODO: 在此处为本机数据添加绘制代码
}
// CPhilosophyView 打印
BOOL CPhilosophyView::OnPreparePrinting(CPrintInfo* pInfo)
{
// 默认准备
return DoPreparePrinting(pInfo);
}
void CPhilosophyView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: 打印前添加额外的初始化
}
void CPhilosophyView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: 打印后添加清除过程
}
// CPhilosophyView 诊断
#ifdef _DEBUG
void CPhilosophyView::AssertValid() const
{
CView::AssertValid();
}
void CPhilosophyView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CPhilosophyDoc* CPhilosophyView::GetDocument() const // 非调试版本是内联的
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CPhilosophyDoc)));
return (CPhilosophyDoc*)m_pDocument;
}
#endif //_DEBUG
//TODO: 在此添加全局函数
//基本函数
void P(int *key)
{
while((*key)-1<0)
{
;
}
(*key)--;
}
void V(int *key)
{
(*key)++;
}
void eating(LPVOID Param)
{
CPhilosophyView *point=(CPhilosophyView *)Param;
CDC *pDC=point->GetDC();
CString m_temp;
pDC->SelectStockObject(BLACK_BRUSH);
int flag=((timer-2)%5)/3;
for(int i=0;i<100;i++)
{
m_temp.Format("%d%%",i+1);
pDC->TextOut(pos[(timer-2)%5].x+210*!flag-60*flag,pos[(timer-2)%5].y-5,m_temp);
pDC->Rectangle(pos[(timer-2)%5].x+100*(!flag)-170*flag,pos[(timer-2)%5].y,pos[(timer-2)%5].x+100*(!flag)-170*flag+1*(i+1),pos[(timer-2)%5].y+10);
et[(timer-2)%5]=m_temp;
ex[(timer-2)%5]=pos[(timer-2)%5].x+100*(!flag)-170*flag+1*(i+1);
::Sleep(eatingtime);
}
pDC->SelectStockObject(WHITE_BRUSH);
pDC->Rectangle(pos[(timer-2)%5].x+100*(!flag)-170*flag,pos[(timer-2)%5].y,pos[(timer-2)%5].x+200*(!flag)-70*flag,pos[(timer-2)%5].y+10);
pDC->TextOut(pos[(timer-2)%5].x+210*!flag-60*flag,pos[(timer-2)%5].y-5,"0% ");
et[(timer-2)%5]="0% ";
ex[(timer-2)%5]=pos[(timer-2)%5].x+100*(!flag)-170*flag;
pDC->CloseFigure();
}
void thinking(LPVOID Param)
{
CPhilosophyView *point=(CPhilosophyView *)Param;
CDC *pDC=point->GetDC();
CString m_temp;
pDC->SelectStockObject(BLACK_BRUSH);
int flag=((timer-2)%5)/3;
for(int i=0;i<100;i++)
{
m_temp.Format("%d%%",i+1);
pDC->TextOut(pos[(timer-2)%5].x+210*!flag-60*flag,pos[(timer-2)%5].y+10,m_temp);
pDC->Rectangle(pos[(timer-2)%5].x+100*(!flag)-170*flag,pos[(timer-2)%5].y+13,pos[(timer-2)%5].x+100*(!flag)-170*flag+1*(i+1),pos[(timer-2)%5].y+23);
tt[(timer-2)%5]=m_temp;
tx[(timer-2)%5]=pos[(timer-2)%5].x+100*(!flag)-170*flag+1*(i+1);
::Sleep(thinkingtime);
}
pDC->SelectStockObject(WHITE_BRUSH);
pDC->Rectangle(pos[(timer-2)%5].x+100*(!flag)-170*flag,pos[(timer-2)%5].y+13,pos[(timer-2)%5].x+200*(!flag)-70*flag,pos[(timer-2)%5].y+23);
pDC->TextOut(pos[(timer-2)%5].x+210*!flag-60*flag,pos[(timer-2)%5].y+10,"0% ");
tt[(timer-2)%5]="0% ";
tx[(timer-2)%5]=pos[(timer-2)%5].x+100*(!flag)-170*flag;
pDC->CloseFigure();
}
//时钟线程
UINT ThreadTimer(LPVOID Param)
{
m_timer=SetTimer((HWND)Param,20,timespan,NULL);
while(1)
{
::WaitForSingleObject(StartThread1.m_hObject,INFINITE);
hWnd1->ResumeThread();
if(timer!=1 && timer!=2)
{
thread=1;
hWnd5->SuspendThread();
}
::WaitForSingleObject(StartThread2.m_hObject,INFINITE);
thread=2;
hWnd2->ResumeThread();
hWnd1->SuspendThread();
::WaitForSingleObject(StartThread3.m_hObject,INFINITE);
thread=3;
hWnd3->ResumeThread();
hWnd2->SuspendThread();
::WaitForSingleObject(StartThread4.m_hObject,INFINITE);
thread=4;
hWnd4->ResumeThread();
hWnd3->SuspendThread();
::WaitForSingleObject(StartThread5.m_hObject,INFINITE);
thread=5;
hWnd5->ResumeThread();
hWnd4->SuspendThread();
}
return 0;
}
//哲学家线程
UINT ThreadProc1(LPVOID Param)
{
while(1)
{
::WaitForSingleObject(Apply.m_hObject,INFINITE);
P(&chair);
c[0]=true;
flag[0]=1;
Pause.SetEvent();
P(&chock[0]);
philo[0].left=1;
philo[0].status=1;
Pause.SetEvent();
P(&chock[4]);
philo[0].right=1;
philo[0].status=2;
Pause.SetEvent();
eating(Param);
V(&chock[0]);
philo[0].left=0;
philo[0].status=1;
Pause.SetEvent();
V(&chock[4]);
philo[0].right=0;
philo[0].status=1;
Pause.SetEvent();
V(&chair);
c[0]=false;
flag[0]=0;
Pause.SetEvent();
thinking(Param);
}
return 0;
}
UINT ThreadProc2(LPVOID Param)
{
while(1)
{
::WaitForSingleObject(Apply.m_hObject,INFINITE);
P(&chair);
c[1]=true;
flag[1]=1;
Pause.SetEvent();
P(&chock[1]);
philo[1].left=1;
philo[1].status=1;
Pause.SetEvent();
P(&chock[0]);
philo[1].right=1;
philo[1].status=2;
Pause.SetEvent();
eating(Param);
V(&chock[1]);
philo[1].left=0;
philo[1].status=1;
Pause.SetEvent();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -