zoukankan      html  css  js  c++  java
  • 1203 有限自动机与构造

    #include<stdio.h>  
    #include<unistd.h>  
    #include<stdlib.h>  
    typedef int state;  
    typedef int condition;  
      
    #define STATENUM 4  
    #define STATE1 0  
    #define STATE2 1  
    #define STATE3 2  
    #define STATETRAP 3  
      
    #define CONDITIONS 2  
    #define CONDITION1 0  
    #define CONDITION2 1  
      
    typedef void (* actiontype)(state mystate,condition mycondition);  
    typedef struct{  
      state next;  
      actiontype action;  
    }trasition, *ptrasition;  
      
    void action1(state mystate,condition myconditon);  
    void action2(state mystate,condition myconditon);  
    void action3(state mystate,condition myconditon);  
    void actiontrap(state mystate,condition myconditon);  
    trasition t1={  
      STATE2,action1  
    };  
    trasition t2={  
      STATE3,action2  
    };  
    trasition t3={  
      STATE2,action3  
    };  
    trasition tt={  
      STATETRAP,actiontrap  
    };  
      
    void action1(state mystate,condition myconditon){  
      printf("action1 one triggered ");  
    }  
    void action2(state mystate,condition myconditon){  
      printf("action2 one triggered ");  
    }  
    void action3(state mystate,condition myconditon){  
      printf("action3 one triggered ");  
    }  
    void actiontrap(state mystate,condition myconditon){  
      printf("actiontrap one triggered ");  
    }  
      
    ptrasition transition_table[STATENUM][CONDITIONS] = {  
    /*      c1,  c2*/  
    /* s1 */&t1, &tt,  
    /* s2 */&tt, &t2,  
    /* s3 */&t3, &tt,  
    /* st */&tt, &tt,  
    };  
    typedef struct  
    {  
        state current;  
    } StateMachine, * pStateMachine;  
       
    state step(pStateMachine machine, condition mycondition)  
    {  
        ptrasition t = transition_table[machine->current][mycondition];  
        (*(t->action))(machine->current, mycondition);  
        machine->current = t->next;  
        printf("the current state is %d ",t->next );  
        return machine->current;  
    }  
    int main(int argc, char *argv[])  
    {  
      StateMachine mymachine;  
      mymachine.current=STATE1;  
      int mycon;  
      char ch;  
      while(1){  
        scanf("%d",&mycon);   
        step(&mymachine,mycon);  
      }  
      return 0;  
    }  
  • 相关阅读:
    Nginx虚拟主机配置实例(Nginx VirtualHost Example)
    考研机试 33.密码翻译
    考研机试 26.10进制 VS 2进制
    考研机试 25.剩下的树
    input默认值设置
    http 304优化,了解客户端缓存
    (转)8款在线CSS优化工具/组织和压缩CSS
    照片从模糊到清晰的渐变加载显示方法
    JS压缩工具Closure Compiler 和 YUICompressor的对比
    【转】高性能web开发 如何加载JS,JS应该放在什么位置?
  • 原文地址:https://www.cnblogs.com/wangjunjie123/p/5017403.html
Copyright © 2011-2022 走看看