1+ /*
2+ * @Author: ThearchyHelios
3+ * @Date: 2022-05-04 13:06:10
4+ * @LastEditTime: 2022-05-05 15:38:34
5+ * @LastEditors: ThearchyHelios
6+ * @Description:
7+ * @FilePath: /Projet_cowsay_L1S2/Tamagoshi-vache-refaire.c
8+ */
9+
10+ #include <stdio.h>
11+ #include <string.h>
12+ #include <stdlib.h>
13+ #include <unistd.h>
14+
15+ typedef int state ;
16+ typedef int condition ;
17+
18+ #define STATENUM 4
19+ #define STATE1 0
20+ #define STATE2 1
21+ #define STATE3 2
22+ #define STATETRAP 3
23+
24+ #define CONDITIONS 2
25+ #define CONDITION1 0
26+ #define CONDITION2 1
27+
28+ typedef void (* actiontype )(state mystate , condition mycondition );
29+ typedef struct
30+ {
31+ state next ;
32+ actiontype action ;
33+ } trasition , * ptrasition ;
34+
35+ void action1 (state mystate , condition myconditon );
36+ void action2 (state mystate , condition myconditon );
37+ void action3 (state mystate , condition myconditon );
38+ void actiontrap (state mystate , condition myconditon );
39+ trasition t1 = {
40+ STATE2 , action1 };
41+ trasition t2 = {
42+ STATE3 , action2 };
43+ trasition t3 = {
44+ STATE2 , action3 };
45+ trasition tt = {
46+ STATETRAP , actiontrap };
47+
48+ void action1 (state mystate , condition myconditon )
49+ {
50+ printf ("action1 one triggered\n" );
51+ }
52+ void action2 (state mystate , condition myconditon )
53+ {
54+ printf ("action2 one triggered\n" );
55+ }
56+ void action3 (state mystate , condition myconditon )
57+ {
58+ printf ("action3 one triggered\n" );
59+ }
60+ void actiontrap (state mystate , condition myconditon )
61+ {
62+ printf ("actiontrap one triggered\n" );
63+ }
64+
65+ ptrasition transition_table [STATENUM ][CONDITIONS ] = {
66+ /* c1, c2*/
67+ /* s1 */ & t1 ,
68+ & tt ,
69+ /* s2 */ & tt ,
70+ & t2 ,
71+ /* s3 */ & t3 ,
72+ & tt ,
73+ /* st */ & tt ,
74+ & tt ,
75+ };
76+ typedef struct
77+ {
78+ state current ;
79+ } StateMachine , * pStateMachine ;
80+
81+ state step (pStateMachine machine , condition mycondition )
82+ {
83+ ptrasition t = transition_table [machine -> current ][mycondition ];
84+ (* (t -> action ))(machine -> current , mycondition );
85+ machine -> current = t -> next ;
86+ printf ("the current state is %d\n" , t -> next );
87+ return machine -> current ;
88+ }
89+ int main (int argc , char * argv [])
90+ {
91+ StateMachine mymachine ;
92+ mymachine .current = STATE1 ;
93+ int mycon ;
94+ char ch ;
95+ while (1 )
96+ {
97+ scanf ("%d" , & mycon );
98+ step (& mymachine , mycon );
99+ }
100+ return 0 ;
101+ }
0 commit comments