有限状态机

喜欢而已 提交于 2020-01-06 03:16:38

有限状态机

 

#include<iostream>
#include<vector>
using namespace std;

typedef struct Context{
    int iNext;
}Context_t;



void processor(Context_t &context,vector<int>& requests,vector<int>& results){
    switch(context.iNext){
    case 1:
    {
        cout<<"11111111111111"<<endl;
        requests.push_back(1);
        cout<<"22222222222222"<<endl;        
        requests.push_back(2);
        context.iNext = 2;
    }
    break;
    case 2:
    {
        if(results.size()<2){
            //cout<<"get resulsts error"<<endl;
            //return;
        }
        for(size_t i=0;i<results.size();i++){
            cout<<"get resulsts"<<results[i]<<endl;
        }
        requests.push_back(2);
        cout<<"33333333333333333333"<<endl;
        context.iNext = 3;
    }
    break;    
    case 3:
    {
        if(results.size()<1){
            //cout<<"get results error"<<endl;
            //return;
        }
        for(size_t i=0;i<results.size();i++){
            cout<<"get results"<<results[i]<<endl;
        }
        context.iNext = -1;
    }
    break;
    }
}



int main()
{
    vector<int> requests;
    vector<int> results;
    Context_t context;
    context.iNext = 1;
    do
    {    
        processor(context,requests,results);
        results.clear();
        for(size_t i=0;i<requests.size();i++){
            results.push_back(i);
        }
        requests.clear();
    }while(context.iNext != -1);
}

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!