include
using namespace std;
bool Undercart_UP=1,Undercart_DOWN=0;
bool green=1,red=0;
bool carIn = false, carOut = false;
bool light = red;
bool Undercart= Undercart_DOWN;
bool switchUndercartUp()
{
cout << "......杆上升......" << endl;
Undercart=Undercart_UP ;
return Undercart;
}
bool switchUndercartDOWN()
{
cout << "......杆下降......" << endl;
Undercart=Undercart_DOWN ;
return Undercart;
}
void turnGreen()
{
cout << "--------------------------绿灯,请通行------------------------" << endl;
light = green;
}
void turnRed()
{
cout << "-----------------------红灯,禁止通行--------------------------" << endl;
light = red;
}
int main()
{
while (1)
{
cout << "是否有车进入(1代表进入,其余代表没有):";
cin >> carIn;
if(carIn!=1)
{
cout<<"无车进入!等待中...";
cout << "是否有车进入(1代表进入,其余代表没有):";
cin >> carIn;
}
if (carIn == 1)
{
if (switchUndercartUp()) turnGreen();
}
while (light)
{
cout << "车是否离开:";
cin >> carOut;
if(carOut!=1)
{
cout<<"车还未离开!等待中...";
cout << "车是否离开:";
cin >> carIn;
}
if (carOut == 1)
{
if (!switchUndercartDOWN()) turnRed();
}
}
}
return 0;
}
运行结果: