/home/toolbox/public_html/solutions/3/325/maybe.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <sys/types.h>
4 #include <sys/stat.h>
5 #include <fcntl.h>
6 #include <stdint.h>
7 #include <math.h>
8 #include <stdlib.h>
9 #include <ctype.h>
10
11 #define TRUE (1 == 1)
12 #define FALSE (1 != 1)
13
14 #define DEBUG if (FALSE)
15
16
17 /*
18 * Author: Isaac Traxler
19 * Date: 2021-12-8
20 * Purpose: fun
21 * Problem: 325 - Identifying Legal Pascal Real Constants
22 */
23
24 /*
25 * This template reads lines of data at a time until end of file.
26 */
27
28
29
30 /* 1 2 34 5 6 7 8
31 * [+-]digits[.digits][e[+-]digits]whitespace
32 *
33 * | start | num1 | num2 | num3 | num4 | num5 | num6 | num7 | num8
34 * whitespace | start | error | num8 | error | num8 | error | error | num8 | num8
35 * sign | num1 | error | error | error | error | num6 | error | error | error
36 * digit | num2 | num2 | num2 | num4 | num4 | num7 | num7 | num7 | error
37 * period | error | error | num3 | error | error | error | error | error | error
38 * exp | error | error | num5 | error | num5 | error | error | error | error
39 * StringEnd | error | error | error | error | valid | error | error | valid | valid
40 * other | error | error | error | error | error | error | error | error | error
41 */
42
43
44 #define MAX_LINE 2049
45 #define START 0
46 #define NUM1 1
47 #define NUM2 2
48 #define NUM3 3
49 #define NUM4 4
50 #define NUM5 5
51 #define NUM6 6
52 #define NUM7 7
53 #define NUM8 8
54 #define ERROR -1
55 #define VALID 100
56 #define WHITESPACE 0
57 #define SIGN 1
58 #define DIGIT 2
59 #define PERIOD 3
60 #define EXP 4
61 #define STRINGEND 5
62 #define OTHER 6
63 char line[MAX_LINE];
64 int slen;
65 int ptr;
66 int state;
67 int table[7][9] =
68 {
69 { START, ERROR, NUM8, ERROR, NUM8, ERROR, ERROR, NUM8, NUM8 },
70 { NUM1, ERROR, ERROR, ERROR, ERROR, NUM6, ERROR, ERROR, ERROR },
71 { NUM2, NUM2, NUM2, NUM4, NUM4, NUM7, NUM7, NUM7, ERROR },
72 { ERROR, ERROR, NUM3, ERROR, ERROR, ERROR, ERROR, ERROR, ERROR },
73 { ERROR, ERROR, NUM5, ERROR, NUM5, ERROR, ERROR, ERROR, ERROR },
74 { ERROR, ERROR, ERROR, ERROR, VALID, ERROR, ERROR, VALID, VALID },
75 { ERROR, ERROR, ERROR, ERROR, ERROR, ERROR, ERROR, ERROR, ERROR }
76 };
77
78 void init()
79 {
80 /* FUNCTION init */
81 } /* FUNCTION init */
82
83 void dump()
84 {
85 /* FUNCTION dump */
86 } /* FUNCTION dump */
87
88 int getInput()
89 {
90 /* FUNCTION getInput */
91 int dataReadFlag;
92
93 fgets(line, MAX_LINE, stdin);
94 line[strlen(line)-1] = 0;
95 dataReadFlag = (0 != strcmp("*", line));
96 slen = strlen(line);
97 return (dataReadFlag);
98 } /* FUNCTION getInput */
99
100 int what(char x)
101 {
102 /* FUNCTION what */
103 int toReturn;
104
105 switch (x)
106 {
107 /* switch */
108 case 0:
109 toReturn = STRINGEND;
110 break;
111 case '0':
112 case '1':
113 case '2':
114 case '3':
115 case '4':
116 case '5':
117 case '6':
118 case '7':
119 case '8':
120 case '9':
121 toReturn = DIGIT;
122 break;
123 case '+':
124 case '-':
125 toReturn = SIGN;
126 break;
127 case 'E':
128 case 'e':
129 toReturn = EXP;
130 break;
131 case '.':
132 toReturn = PERIOD;
133 break;
134 case ' ':
135 toReturn = WHITESPACE;
136 break;
137 default:
138 toReturn = OTHER;
139 } /* switch */
140 return toReturn;
141 } /* FUNCTION what */
142
143 void cleanLine()
144 {
145 /* FUNCTON cleanLine */
146 int strt;
147 int i;
148 int j;
149
150 slen = strlen(line);
151 DEBUG printf(" 1 line [%s] %d\n", line, slen);
152 for (i=slen-1; (0<=i) && (' ' == line[i]); i--)
153 {
154 /* find end of string */
155 line[i] = 0;
156 } /* find end of string */
157 slen = strlen(line);
158 DEBUG printf(" 2 line [%s] %d\n", line, slen);
159 strt=0;
160 for (i=0; (slen>i) && (' ' == line[i]); i++)
161 {
162 strt = i + 1;
163 }
164 for (i=strt,j=0; (slen>=i); i++,j++)
165 {
166 /* shift line back */
167 line[j] = line[i];
168 } /* shift line back */
169 slen = strlen(line);
170 DEBUG printf(" 3 line [%s] %d\n", line, slen);
171 } /* FUNCTON cleanLine */
172
173 void process()
174 {
175 /* FUNCTION process */
176 int tmp;
177 char result[3] ="";
178
179 DEBUG printf("[%s]\n", line);
180 state = START;
181 ptr = 0;
182 while ((ERROR != state) && (VALID != state))
183 {
184 /* process line */
185 tmp = what(line[ptr]);
186 DEBUG printf("(%c %d) (state %d) => %d\n", line[ptr], tmp, state, table[tmp][state]);
187 state = table[tmp][state];
188 ptr++;
189 } /* process line */
190 if (ERROR == state)
191 {
192 result[0]='i';
193 result[1]='l';
194 result[2]=0;
195 }
196 cleanLine();
197 printf("%s is %slegal.\n", line, result);
198 } /* FUNCTION process */
199
200 int main()
201 {
202 /* main */
203 int moreToDo;
204
205 init();
206 moreToDo = getInput();
207 while (moreToDo)
208 {
209 /* while */
210 process();
211 moreToDo = getInput();
212 } /* while */
213
214 return EXIT_SUCCESS;
215 } /* main */
216