第二周作业

  • 状态机的所有状态

等待汽车

起落杆上升

汽车通行

起落杆下降

  • 状态机所接收到的外部事件

入闸传感器:汽车进入门禁 s_in

出闸传感器:汽车通过门禁 s_out

起落杆位置传感器:起落杆到达顶部 s_up

起落杆位置传感器:起落杆到达底部 s_down

  • 状态机所产生的动作

起落杆上升或下降

通行灯亮红灯或绿灯

  • 状态机的所有状态跃迁:(原状态、新状态、触发条件、产生动作)

#include "stdafx.h"

#include <iostream>

#include <windows.h>

using namespace std;

 

bool s_in;      //汽车进入

bool s_out;     //汽车离开

bool s_up;      //起落杆上升

bool s_down;    //起落杆下降

 

void raising()

{

    cout << "起落杆上升中......" << endl;

    Sleep(3000);

    s_up = 1;

}

void dropping()

{

    cout << "汽车已通过" << endl;

    cout << "起落杆下降中......" << endl;

    Sleep(3000);

    s_down = 1;

}

void passing()

{

    cout << "汽车通过中......" << endl;

    Sleep(3000);

    s_out = 1;

}

void waiting()

{

    cout << "等待汽车状态,是否有车进入:";

    cin >> s_in;

}

void light_green()

{

    cout << "通行灯为绿灯,可以通过" << endl;

}

void light_red()

{

    cout << "通行灯为红灯,不可通过" << endl;

}

 

int main(void)

{

    while (1)

    {

        waiting();

        if (s_in)

        {

            raising();

        };

 

        if (s_up)

        {

            light_green();

            passing();

        }

        if (s_out)

        {

            dropping();

        }

        if (s_down)

        {

            light_red();

        }

    }

    return 0;

}

原文地址:https://www.cnblogs.com/magicxu/p/6143220.html