吃水果问题,c++版
来源:互联网 发布:2016淘宝类目排行榜 编辑:程序博客网 时间:2024/04/19 23:52
#include <iostream>
#include <signal.h>
#include <ctime>
#include <cstdlib>
#include <windows.h>
using namespacestd;
int apple=0;
int orange=0;
bool father_lag;
bool mother_lag;
bool son_lag;
bool daugther_lag;
void father()
{
apple++;
cout<<"father放了一个苹果,";
}
void mother()
{
orange++;
cout<<"mother放了一个橘子,";
}
void son()
{
orange--;
cout<<"son 吃了一个橘子,";
}
void daugther()
{
apple--;
cout<<"daugther吃了一个苹果,";
}
void print()
{
if(father_lag==true)
cout<<"father进程处于等待状态;"<<endl;
if(mother_lag==true)
cout<<"mother进程处于等待状态;"<<endl;
if(son_lag==true)
cout<<"son进程处于等待状态;"<<endl;
if(daugther_lag==true)
cout<<"daugther进程处于等待状态;"<<endl;
if((father_lag==false)&&(mother_lag==false)&&(son_lag==false)&&(daugther_lag==false)!=true)
cout<<endl;
}
int main()
{
intk;
for(k=0;k<10;k++)
{
cout<<"第"<<k+1<<"次操作"<<endl;
srand((unsigned)time(NULL));
inti=rand()%2;
if(i==0)
{
cout<<"father进程被调用"<<endl;
Sleep(1000);
father();
father_lag=false;
mother_lag=true;
print();
}
else
{
cout<<"mother进程被调用"<<endl;
Sleep(1000);
mother();
mother_lag=false;
father_lag=true;
print();
}
father_lag=false;
mother_lag=false;
if(orange)
{
cout<<"son进程被调用"<<endl;
Sleep(1000);
son();
son_lag=false;
daugther_lag=true;
print();
}
else
{
cout<<"daugther进程被调用"<<endl;
Sleep(1000);
daugther();
daugther_lag=false;
son_lag=true;
print();
}
daugther_lag=false;
son_lag=false;
}
return0;