-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathC_STATE.c
151 lines (144 loc) · 3.34 KB
/
C_STATE.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/**
* Drew Rife and Brad Olah
*
* A state machine in C
*/
#include "C_STATE.h"
int main(int argc, char **argv) {
currentState = start;
enum inputType entries;
int (* stateFunction)(void); // function pointer for each State
char *input = argv[1];
int lengthOfInput = strlen(input);
int i;
input[lengthOfInput]='\0';
for(i = 0;i<=lengthOfInput;i++){
character = input[i];
printf("Input char = %c \n", character);
stateFunction = state[currentState];
entries = stateFunction();
}
printf("\nThe calculated value is: %.2f\n",value);
return 0;
}
/**
* Checks to see what the input is
* @param input [what the user enters]
* @return [result]
*/
enum inputType GetInputType(char input){
enum inputType result;
if(isdigit(input)){
result = isDigit;
}else if(input == '+'){
result = isPosSign;
}else if(input == '-'){
result = isNegSign;
}else if(input == '.'){
result = isPoint;
}else if(input == '\0'){
result = isTerminator;
}else{
result = isInvalid;
}
return result;
}
/**
* Should start in StartState every time.
* Evaluates the input character and
* decides where to transition from it
* @return currentState
*/
int StartState(){
printf("In Start state with input of %c\n", character);
enum inputType iType = GetInputType(character);
if(iType == isDigit){
sign = 1;
value = character-'0';
currentState = integer;
}else if(iType == isPosSign){
sign = 1;
currentState = integer;
}else if(iType == isNegSign){
sign = -1;
currentState = integer;
}else if(iType == isPoint){
point = .1;
currentState = decimal;
}else if(iType == isTerminator){
value = 0;
currentState = end;
}else{
printf("Invalid character: %c\n",character);
value =0;
currentState = end;
}
}
/**
* If the input is an digit then it is transitioned
* to this state.
* @return [currentState]
*/
int IntegerState(){
printf("In integer state with input of %c\n", character);
enum inputType iType = GetInputType(character);
if(iType == isDigit){
value = (value*10)+character-'0';
currentState = integer;
}else if(iType == isPosSign){
printf("Invalid character: %c\n",character);
value = 0;
currentState = end;
}else if(iType == isNegSign){
printf("Invalid character: %c\n",character);
value = 0;
currentState = end;
}else if(iType == isPoint){
point = .1;
currentState = decimal;
}else if(iType == isTerminator){
value = sign * value;
currentState = end;
}else{
printf("Invalid character: %c\n",character);
value =0;
currentState = end;
}
}
/**
* If the input is a decimal point then it
* moves to this state
* @return [currentState]
*/
int DecimalState(){
printf("In decimal state with input of %c\n", character);
enum inputType iType = GetInputType(character);
if(iType == isDigit){
value += point * (character-'0');
point /= 10;
currentState = decimal;
}else if(iType == isPosSign){
printf("Invalid character: %c\n",character);
value = 0;
currentState = end;
}else if(iType == isNegSign){
printf("Invalid character: %c\n",character);
value = 0;
currentState = end;
}else if(iType == isPoint){
printf("Invalid character: %c\n",character);
value = 0;
currentState = end;
}else if(iType == isTerminator){
value = sign * value;
currentState = end;
}else{
printf("Invalid character: %c\n",character);
value =0;
currentState = end;
}
}
/**
* finished
*/
int EndState(){}