//---------------------------------------------------------------------------
#include <time.h>
#include <vcl.h>
#pragma hdrstop
#include "eating.h"
#include "Unit2.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
struct data
{
int state; //哲学家状态,0表示思考,1表示等待另外的筷子,2表示吃饭
int lasttime; //持续吃饭的时间
}pho[5];
struct wait
{
int num;
struct wait *next;
}w_pho;
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
int i;
for (i=0;i<5;i++)
{
pho[i].state=0;
pho[i].lasttime=3;
}
}
//---------------------------------------------------------------------------
int ran; //表示随机工作模式
int chance; //随机选取一个哲学家拿筷子
int chop[5]={1,1,1,1,1}; //筷子的显示,0表示已经拿了,1表示没拿
int count=4; //只允许4个人拿筷子
int waitnum; //等待队列人数
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
if(MessageDlg("你是否真的想要退出呢?",
mtConfirmation,TMsgDlgButtons()<<mbYes<<mbNo,0)==mrYes)
Close();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
randomize();
Label1->Caption ="开饭拉~~开饭拉~!\n大家先不要着急~\n先发筷子~!";
Timer1->Enabled=false;
Timer2->Enabled=false;
//初始化
Timer1->Enabled=true;
Timer2->Enabled=true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
Timer1->Enabled=false;
Timer2->Enabled=false;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormClick(TObject *Sender)
{
Canvas->TextOutA(688,24,Time());
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Exit1Click(TObject *Sender)
{
Button2Click(Form1);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::N2Click(TObject *Sender)
{
InfoDlg->ShowModal();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::HELP2Click(TObject *Sender)
{
MessageDlg("你上课没听课吗??快去看书!!!!",mtError,TMsgDlgButtons() << mbOK,0);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button4Click(TObject *Sender)
{
Timer1->Enabled=true;
Timer2->Enabled=true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Timer2Timer(TObject *Sender)
{
if(pho[0].state==0)
{
photo00->Visible=true;
photo01->Visible=false;
photo02->Visible=false;
}
if(pho[0].state==1)
{
photo00->Visible=false;
photo01->Visible=true;
photo02->Visible=false;
}
if(pho[0].state==2)
{
photo00->Visible=false;
photo01->Visible=false;
photo02->Visible=true;
}
if(pho[1].state==0)
{
photo10->Visible=true;
photo11->Visible=false;
photo12->Visible=false;
}
if(pho[1].state==1)
{
photo10->Visible=false;
photo11->Visible=true;
photo12->Visible=false;
}
if(pho[1].state==2)
{
photo10->Visible=false;
photo11->Visible=false;
photo12->Visible=true;
}
if(pho[2].state==0)
{
photo20->Visible=true;
photo21->Visible=false;
photo22->Visible=false;
}
if(pho[2].state==1)
{
photo20->Visible=false;
photo21->Visible=true;
photo22->Visible=false;
}
if(pho[2].state==2)
{
photo20->Visible=false;
photo21->Visible=false;
photo22->Visible=true;
}
if(pho[3].state==0)
{
photo30->Visible=true;
photo31->Visible=false;
photo32->Visible=false;
}
if(pho[3].state==1)
{
photo30->Visible=false;
photo31->Visible=true;
photo32->Visible=false;
}
if(pho[3].state==2)
{
photo30->Visible=false;
photo31->Visible=false;
photo32->Visible=true;
}
if(pho[4].state==0)
{
photo40->Visible=true;
photo41->Visible=false;
photo42->Visible=false;
}
if(pho[4].state==1)
{
photo40->Visible=false;
photo41->Visible=true;
photo42->Visible=false;
}
if(pho[4].state==2)
{
photo40->Visible=false;
photo41->Visible=false;
photo42->Visible=true;
}
if(chop[0]==1) chop0->Visible=true;
if(chop[0]==0) chop0->Visible=false;
if(chop[1]==1) chop1->Visible=true;
if(chop[1]==0) chop1->Visible=false;
if(chop[2]==1) chop2->Visible=true;
if(chop[2]==0) chop2->Visible=false;
if(chop[3]==1) chop3->Visible=true;
if(chop[3]==0) chop3->Visible=false;
if(chop[4]==1) chop4->Visible=true;
if(chop[4]==0) chop4->Visible=false;
}
//---------------------------------------------------------------------------
void dectime()
{
struct wait *p;
int i,j; //i为控制变量,j为转移变量
p=&w_pho;
for(i=0;i<waitnum;i++)
{
p=p->next;
j=p->num;
if(pho[j].state==2)
{
pho[j].lasttime--;
}
}
}
void signal(int *countt)
{
struct wait *p,*p1,*p2;
int i,j,k,l=0; //i为控制变量,j,k为转移变量 ,l为开关
p=w_pho.next;
k=waitnum;
p1=&w_pho;
for(i=0;i<k;i++)
{
l=0;
j=p->num;
if((pho[j].state==2)&&(pho[j].lasttime<=0))
{
l=1;
chop[j%5]=1;
chop[(j+1)%5]=1;
pho[j].state=0;
count++;
waitnum--;
pho[j].lasttime=3; //设置为初始值
p1->next=p->next;
}
if(l==0)
p1=p;
else ;
p=p->next;
}
}
checkchance(int *chan)
{
struct wait *p;
int i,j;
p=w_pho.next;
if(waitnum==0)
return 1;
else
{
for(i=0;i<waitnum;i++)
{
if(p->num==*chan)
{
j=*chan;
if(pho[j].state==0)
{
if((chop[j]==1)&&(count>1))
{
count--;
chop[j]=0;
pho[j].state=1;
}
return 0;
}
if(pho[j].state==1)
{
if(chop[(j+1)%5]==1)
{
chop[(j+1)%5]=0;
pho[j].state=2;
}
return 0;
}
if(pho[j].state==2)
return 0;
}
p=p->next;
}
}
return 1;
}
int wait(int *ph,int *dat)
{
int i;
struct wait *p,*p1;
p=&w_pho;
for(i=0;i<waitnum;i++)
p=p->next;
p1=(struct wait *)malloc(sizeof(struct wait));
p1->num=*ph;
p1->next=p->next;
p->next=p1;
waitnum++;
if(*dat-1<=0)
return 0;
i=*ph;
if(pho[i].state==0)
{
if(chop[i%5]==1)
{
chop[i%5]=0;
pho[i].state=1;
count--;
}
return 0;
}
return 0;
}
void request()
{
chance=random(5);
if(checkchance(&chance))
wait(&chance,&count);
}
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
ran=random(3); //设定随机的工作模式
for(ran=0;ran<3;ran++)
{
request(); //请求申请
}
dectime(); //搜索减少吃饭人lastime时间
signal(&count);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::TrackBar1Change(TObject *Sender)
{
T