有限状态机
有限状态机
#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); }