码迷,mamicode.com
首页 > 其他好文 > 详细

有限自动机与构造

时间:2015-12-30 17:02:47      阅读:113      评论:0      收藏:0      [点我收藏+]

标签:

#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\n");  
}  
void action2(state mystate,condition myconditon){  
  printf("action2 one triggered\n");  
}  
void action3(state mystate,condition myconditon){  
  printf("action3 one triggered\n");  
}  
void actiontrap(state mystate,condition myconditon){  
  printf("actiontrap one triggered\n");  
}  
  
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\n",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;  
}  

 

有限自动机与构造

标签:

原文地址:http://www.cnblogs.com/95mz/p/5089325.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!