-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalc.c
More file actions
539 lines (502 loc) · 12.6 KB
/
calc.c
File metadata and controls
539 lines (502 loc) · 12.6 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
/*****************************************************************
* Linux verion of Simple Calculator using HSMs implementation
* technique, based on Hierarchical Event Processor Implementation
* guide (PSiCC2 book chapter 4, state-machine.com).
*
* My Simple Calculator
* @QuanTM
****************************************************************/
#include "hsm.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <math.h>
#include <sys/ioctl.h>
#include <termios.h>
/********* use to enter a char instantly from keyboard **********/
void enable_raw_mode()
{
struct termios term;
tcgetattr(0, &term);
term.c_lflag &= ~(ICANON | ECHO);
tcsetattr(0, TCSANOW, &term);
}
void disable_raw_mode()
{
struct termios term;
tcgetattr(0, &term);
term.c_lflag |= ICANON | ECHO;
tcsetattr(0, TCSANOW, &term);
}
/****************************************************************/
enum CalcSignals {
C_SIG,
DIGIT_0_SIG,
DIGIT_1_9_SIG,
POINT_SIG,
OPER_SIG,
EQUAL_SIG,
OFF_SIG,
NONE
};
typedef struct CalcEvent {
Event super;
char keyCode;
} CalcEvent;
typedef struct Calc {
Hsm super;
double operand1;
char operator;
} Calc;
void Calc_ctor();
static State Calc_initial (Calc *me, Event const *e);/*init. pseudostate */
static State Calc_on (Calc *me, Event const *e); /* state handler */
static State Calc_error (Calc *me, Event const *e); /* state handler */
static State Calc_ready (Calc *me, Event const *e); /* state handler */
static State Calc_result (Calc *me, Event const *e); /* state handler */
static State Calc_negated1 (Calc *me, Event const *e); /* state handler */
static State Calc_operand1 (Calc *me, Event const *e); /* state handler */
static State Calc_zero1 (Calc *me, Event const *e); /* state handler */
static State Calc_int1 (Calc *me, Event const *e); /* state handler */
static State Calc_frac1 (Calc *me, Event const *e); /* state handler */
static State Calc_opEntered(Calc *me, Event const *e); /* state handler */
static State Calc_negated2 (Calc *me, Event const *e); /* state handler */
static State Calc_operand2 (Calc *me, Event const *e); /* state handler */
static State Calc_zero2 (Calc *me, Event const *e); /* state handler */
static State Calc_int2 (Calc *me, Event const *e); /* state handler */
static State Calc_frac2 (Calc *me, Event const *e); /* state handler */
static State Calc_off (Calc *me, Event const *e); /* state handler */
static char buffer[100]; // a buffer use to store value of operands
static unsigned int count = 0;
void insert(char ch) // append a char to buffer
{
buffer[count++] = ch;
}
double getValue() // get the value of buffer then empty buffer
{
double result = 0.0;
unsigned int dot = count;
for (int i = 0; i < count; i++)
if (buffer[i] == '.') dot = i;
for (int i = dot + 1; i < count; i++)
result += (((int)buffer[i]) - 48) / pow(10.0, i - dot);
if (buffer[0] == '-')
{
for (int i = 1; i < dot; i++)
result += (((int)buffer[i]) - 48) * pow(10.0, dot - 1 - i);
result *= -1;
}
else
{
for (int i = 0; i < dot; i++)
result += (((int)buffer[i]) - 48) * pow(10.0, dot - 1 - i);
}
count = 0;
return result;
}
static Calc l_calc; // the calculator instance
void Calc_ctor()
{
printf("\nWelcome to my simple calculator..."
"\nPress any button...");
Calc *me = &l_calc;
Hsm_ctor(&me->super, (StateHandler)&Calc_initial);
}
State Calc_initial(Calc *me, Event const *e)
{
printf("\nInitialized My Calculator..."
"\nPress 'e' to start");
return TRANS(&Calc_on);
}
State Calc_on(Calc *me, Event const *e)
{
switch (e->signal)
{
case OFF_SIG:
{
printf("\nTurn off calculator...");
sleep(1);
printf("\nBye Bye!\n");
sleep(1);
exit(0);
return TRANS(&Calc_off);
}
case EQUAL_SIG:
{
printf("\nMy Calculator is ready");
return TRANS(&Calc_ready);
}
case C_SIG:
{
printf("\n");
getValue();
return TRANS(&Calc_ready);
}
}
return SUPER(&Hsm_top);
}
State Calc_ready(Calc *me, const Event *e)
{
switch (e->signal)
{
case DIGIT_0_SIG:
{
printf("\n0");
insert('0');
return TRANS(&Calc_zero1);
}
case DIGIT_1_9_SIG:
{
printf("\n%c", ((CalcEvent*)e)->keyCode);
insert(((CalcEvent*)e)->keyCode);
return TRANS(&Calc_int1);
}
case POINT_SIG:
{
insert('0');
insert('.');
printf("\n0.");
return TRANS(&Calc_frac1);
}
case OPER_SIG:
{
if (((CalcEvent*)e)->keyCode == '-')
{
printf("\n-");
insert(((CalcEvent*)e)->keyCode);
return TRANS(&Calc_negated1);
}
}
}
return SUPER(&Calc_on);
}
State Calc_zero1(Calc *me, const Event *e)
{
switch (e->signal)
{
case DIGIT_0_SIG: return HANDLED();
case DIGIT_1_9_SIG:
{
printf("%c", ((CalcEvent*)e)->keyCode);
insert(((CalcEvent*)e)->keyCode);
return TRANS(&Calc_int1);
}
case POINT_SIG:
{
printf(".");
insert('.');
return TRANS(&Calc_frac1);
}
}
return SUPER(&Calc_operand1);
}
State Calc_int1(Calc *me, const Event *e)
{
switch (e->signal)
{
case DIGIT_0_SIG: case DIGIT_1_9_SIG:
{
printf("%c", ((CalcEvent*)e)->keyCode);
insert(((CalcEvent*)e)->keyCode);
return HANDLED();
}
case POINT_SIG:
{
printf(".");
insert('.');
return TRANS(&Calc_frac1);
}
}
return SUPER(&Calc_operand1);
}
State Calc_frac1(Calc *me, const Event *e)
{
switch (e->signal)
{
case DIGIT_0_SIG: case DIGIT_1_9_SIG:
{
printf("%c", ((CalcEvent*)e)->keyCode);
insert(((CalcEvent*)e)->keyCode);
return HANDLED();
}
case POINT_SIG: return HANDLED();
}
return SUPER(&Calc_operand1);
}
State Calc_operand1(Calc *me, const Event *e)
{
switch (e->signal)
{
case OPER_SIG:
{
me->operand1 = getValue();
printf(" %c ", ((CalcEvent*)e)->keyCode);
me->operator = ((CalcEvent*)e)->keyCode;
return TRANS(&Calc_opEntered);
}
case EQUAL_SIG:
{
me->operand1 = getValue();
printf(" = %f", me->operand1);
return TRANS(&Calc_result);
}
}
return SUPER(&Calc_on);
}
State Calc_negated1(Calc *me, Event const *e)
{
switch (e->signal)
{
case OPER_SIG: return HANDLED();
}
return SUPER(&Calc_ready);
}
State Calc_opEntered(Calc *me, const Event *e)
{
switch (e->signal)
{
case OPER_SIG:
{
if (((CalcEvent*)e)->keyCode == '-')
{
printf("-");
insert(((CalcEvent*)e)->keyCode);
return TRANS(&Calc_negated2);
}
return HANDLED();
}
case DIGIT_0_SIG:
{
printf("0");
insert(((CalcEvent*)e)->keyCode);
return TRANS(&Calc_zero2);
}
case DIGIT_1_9_SIG:
{
printf("%c", ((CalcEvent*)e)->keyCode);
insert(((CalcEvent*)e)->keyCode);
return TRANS(&Calc_int2);
}
case POINT_SIG:
{
printf("0.");
insert((int)'0');
insert((int)'.');
return TRANS(&Calc_frac2);
}
}
return SUPER(&Calc_on);
}
State Calc_negated2(Calc *me, Event const *e)
{
switch (e->signal)
{
case OPER_SIG: return HANDLED();
}
return SUPER(&Calc_opEntered);
}
State Calc_zero2(Calc *me, const Event *e)
{
switch (e->signal)
{
case DIGIT_0_SIG: return HANDLED();
case DIGIT_1_9_SIG:
{
printf("%c", ((CalcEvent*)e)->keyCode);
insert(((CalcEvent*)e)->keyCode);
return TRANS(&Calc_int2);
}
case POINT_SIG:
{
printf(".");
insert('.');
return TRANS(&Calc_frac2);
}
}
return SUPER(&Calc_operand2);
}
State Calc_int2(Calc *me, const Event *e)
{
switch (e->signal)
{
case DIGIT_0_SIG: case DIGIT_1_9_SIG:
{
printf("%c", ((CalcEvent*)e)->keyCode);
insert(((CalcEvent*)e)->keyCode);
return HANDLED();
}
case POINT_SIG:
{
printf(".");
insert('.');
return TRANS(&Calc_frac2);
}
}
return SUPER(&Calc_operand2);
}
State Calc_frac2(Calc *me, const Event *e)
{
switch (e->signal)
{
case DIGIT_0_SIG: case DIGIT_1_9_SIG:
{
printf("%c", ((CalcEvent*)e)->keyCode);
insert(((CalcEvent*)e)->keyCode);
return HANDLED();
}
case POINT_SIG: return HANDLED();
}
return SUPER(&Calc_operand2);
}
State Calc_operand2(Calc *me, const Event *e)
{
switch (e->signal)
{
case OPER_SIG:
{
if (me->operator == '+') me->operand1 += getValue();
if (me->operator == '-') me->operand1 -= getValue();
if (me->operator == '*') me->operand1 *= getValue();
if (me->operator == '/')
{
if (getValue() == 0.0)
{
printf("\nMath ERROR");
return TRANS(&Calc_error);
}
me->operand1 /= getValue();
}
printf("\n%f %c ", me->operand1, ((CalcEvent*)e)->keyCode);
me->operator = ((CalcEvent*)e)->keyCode;
return TRANS(&Calc_opEntered);
}
case EQUAL_SIG:
{
if (me->operator == '+') me->operand1 += getValue();
if (me->operator == '-') me->operand1 -= getValue();
if (me->operator == '*') me->operand1 *= getValue();
if (me->operator == '/')
{
if (getValue() == 0.0)
{
printf("\nMath ERROR");
return TRANS(&Calc_error);
}
me->operand1 /= getValue();
}
printf(" = %f", me->operand1);
return TRANS(&Calc_result);
}
}
return SUPER(&Calc_on);
}
State Calc_result(Calc *me, const Event *e)
{
switch (e->signal)
{
case OPER_SIG:
{
printf("\n%f %c ", me->operand1, ((CalcEvent*)e)->keyCode);
me->operator = ((CalcEvent*)e)->keyCode;
return TRANS(&Calc_opEntered);
}
}
return SUPER(&Calc_ready);
}
State Calc_error(Calc *me, const Event *e)
{
return SUPER(&Calc_on);
}
State Calc_off(Calc *me, Event const *e)
{
return HANDLED();
}
/***************************************************************/
int main()
{
//Hsm_init((Hsm*)&l_calc, (Event*)0);
printf("Press 0...9 to enter digit\n"
"Press '.' to enter decimal point\n"
"Press 'a' to add\n"
"Press 's' to subtract\n"
"Press 'm' to multiply\n"
"Press 'd' to divide\n"
"Press 'e' to get result\n"
"Press 'c' to clear\n"
"Press ESC to turn off Calculator\n");
Calc_ctor();
enable_raw_mode();
while (1)
{
char c = getchar();
CalcEvent ce;
((Event *)&ce)->signal = NONE;
switch (c)
{
case '0':
{
ce.keyCode = '0';
((Event *)&ce)->signal = DIGIT_0_SIG;
break;
}
case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
{
ce.keyCode = c;
((Event *)&ce)->signal = DIGIT_1_9_SIG;
break;
}
case '.':
{
ce.keyCode = c;
((Event *)&ce)->signal = POINT_SIG;
break;
}
case 'c':
{
ce.keyCode = c;
((Event *)&ce)->signal = C_SIG;
break;
}
case 'a':
{
ce.keyCode = '+';
((Event *)&ce)->signal = OPER_SIG;
break;
}
case 's':
{
ce.keyCode = '-';
((Event *)&ce)->signal = OPER_SIG;
break;
}
case 'm':
{
ce.keyCode = '*';
((Event *)&ce)->signal = OPER_SIG;
break;
}
case 'd':
{
ce.keyCode = '/';
((Event *)&ce)->signal = OPER_SIG;
break;
}
case 'e':
{
ce.keyCode = '=';
((Event *)&ce)->signal = EQUAL_SIG;
break;
}
case '\33':
{
ce.keyCode = c;
((Event *)&ce)->signal = OFF_SIG;
break;
}
}
if (((Event *)&ce)->signal != NONE)
Hsm_dispatch((Hsm*)&l_calc, (Event*)&ce);
}
disable_raw_mode();
return 0;
}