📄 os.cpp
字号:
#define NULL 0
#include<iostream>
#include<time.h>
#include <unistd.h>
#include<stdio.h>
using namespace std;
const int TimeOfSmoking=2;
int SmokerSqueue[3]={0,0,0};int value=0,ssin=0,ssout=0;
void P(int i)
{
value--;
if(value<0)
{
SmokerSqueue[ssin]=i;
ssin--;
if(ssin==-1)ssin=2;
}
}
int V()
{
int temp;
value++;
if(value<=0)
{
temp=SmokerSqueue[ssout];
SmokerSqueue[ssout]=0;
ssout--;
if(ssout==-1)ssout=2;
}
return (temp);
}
void seller(int &thing1,int &thing2)
{
srand((unsigned)time(NULL));
int i;
i=rand()%3+1;
thing1=(i+1)%3+1;
thing2=(i-1)%3+1;
}
void PrintQueue()
{
cout<<"The waiting queue is(head--tail):";
for(int i=ssout;;i--)
{
if(i==-1)i=2;
if(SmokerSqueue[i]!=0)cout<<SmokerSqueue[i]<<" ";
if(i==ssin)break;
}
cout<<endl;
}
bool smoker(int i,int thing1,int thing2)
{
cout<<"The "<<i<<"th smoker is matching the things"<<endl;
if(i!=thing1&&i!=thing2)
{
PrintQueue();
cout<<"The "<<i<<"th smoker is smoking..."<<endl;
cout<<endl;
sleep(TimeOfSmoking);
P(i);
return(true);
}
else
{
PrintQueue();
P(i);
return(false);
}
}
int main()
{
int thing1,thing2;
P(1);P(2);P(3);seller(thing1,thing2);
cout<<"The things are:"<<thing1<<" "<<thing2<<endl;
while(true)
{
if(smoker(V(),thing1,thing2)){seller(thing1,thing2);cout<<"Now the things are:"<<thing1<<" "<<thing2<<endl;}
else continue;
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -