/home/toolbox/public_html/solutions/3/325/a.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 | error | 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 char tline[MAX_LINE];
65 int slen;
66 int ptr;
67 int state;
68 int table[7][9] =
69 {
70 { START, ERROR, ERROR, ERROR, NUM8, ERROR, ERROR, NUM8, NUM8 },
71 { NUM1, ERROR, ERROR, ERROR, ERROR, NUM6, ERROR, ERROR, ERROR },
72 { NUM2, NUM2, NUM2, NUM4, NUM4, NUM7, NUM7, NUM7, ERROR },
73 { ERROR, ERROR, NUM3, ERROR, ERROR, ERROR, ERROR, ERROR, ERROR },
74 { ERROR, ERROR, NUM5, ERROR, NUM5, ERROR, ERROR, ERROR, ERROR },
75 { ERROR, ERROR, ERROR, ERROR, VALID, ERROR, ERROR, VALID, VALID },
76 { ERROR, ERROR, ERROR, ERROR, ERROR, ERROR, ERROR, ERROR, ERROR }
77 };
78
79 void init()
80 {
81 /* FUNCTION init */
82 } /* FUNCTION init */
83
84 void dump()
85 {
86 /* FUNCTION dump */
87 } /* FUNCTION dump */
88
89 void cleanLine(char line[])
90 {
91 /* FUNCTON cleanLine */
92 int strt;
93 int i;
94 int j;
95
96 slen = strlen(line);
97 DEBUG printf(" 1 line [%s] %d\n", line, slen);
98 for (i=slen-1; (0<=i) && (' ' == line[i]); i--)
99 {
100 /* find end of string */
101 line[i] = 0;
102 } /* find end of string */
103 slen = strlen(line);
104 DEBUG printf(" 2 line [%s] %d\n", line, slen);
105 strt=0;
106 for (i=0; (slen>i) && (' ' == line[i]); i++)
107 {
108 strt = i + 1;
109 }
110 for (i=strt,j=0; (slen>=i); i++,j++)
111 {
112 /* shift line back */
113 line[j] = line[i];
114 } /* shift line back */
115 slen = strlen(line);
116 DEBUG printf(" 3 line [%s] %d\n", line, slen);
117 } /* FUNCTON cleanLine */
118
119 int getInput()
120 {
121 /* FUNCTION getInput */
122 int dataReadFlag;
123
124 fgets(line, MAX_LINE, stdin);
125 line[strlen(line)-1] = 0;
126 strcpy(tline, line);
127 cleanLine(tline);
128 dataReadFlag = (0 != strcmp("*", tline));
129 slen = strlen(line);
130 return (dataReadFlag);
131 } /* FUNCTION getInput */
132
133 int what(char x)
134 {
135 /* FUNCTION what */
136 int toReturn;
137
138 switch (x)
139 {
140 /* switch */
141 case 0:
142 toReturn = STRINGEND;
143 break;
144 case '0':
145 case '1':
146 case '2':
147 case '3':
148 case '4':
149 case '5':
150 case '6':
151 case '7':
152 case '8':
153 case '9':
154 toReturn = DIGIT;
155 break;
156 case '+':
157 case '-':
158 toReturn = SIGN;
159 break;
160 case 'E':
161 case 'e':
162 toReturn = EXP;
163 break;
164 case '.':
165 toReturn = PERIOD;
166 break;
167 case ' ':
168 toReturn = WHITESPACE;
169 break;
170 default:
171 toReturn = OTHER;
172 } /* switch */
173 return toReturn;
174 } /* FUNCTION what */
175
176 void process()
177 {
178 /* FUNCTION process */
179 int tmp;
180 char result[3] ="";
181
182 DEBUG printf("[%s]\n", line);
183 state = START;
184 ptr = 0;
185 while ((ERROR != state) && (VALID != state))
186 {
187 /* process line */
188 tmp = what(line[ptr]);
189 DEBUG printf("(%c %d) (state %d) => %d\n", line[ptr], tmp, state, table[tmp][state]);
190 state = table[tmp][state];
191 ptr++;
192 } /* process line */
193 if (ERROR == state)
194 {
195 result[0]='i';
196 result[1]='l';
197 result[2]=0;
198 }
199 cleanLine(line);
200 printf("%s is %slegal.\n", line, result);
201 } /* FUNCTION process */
202
203 int main()
204 {
205 /* main */
206 int moreToDo;
207
208 init();
209 moreToDo = getInput();
210 while (moreToDo)
211 {
212 /* while */
213 process();
214 moreToDo = getInput();
215 } /* while */
216
217 return EXIT_SUCCESS;
218 } /* main */
219