-
Notifications
You must be signed in to change notification settings - Fork 2
/
calculator.c
479 lines (467 loc) · 12 KB
/
calculator.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
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
/* NIM/Nama : 13517050/Christopher Billy Setiawan, 13517116/Ferdy Santoso, 13517137/Vincent Budianto
* Nama file : calculator.c
* Topik : Tugas Besar II IF2220-Teori Bahasa Formal dan Otomata / Aplikasi CFG dan PDA pada Pengenalan Ekspresi Matematika
* Tanggal : 21 November 2018
* Deskripsi : Program yang dapat menerima input suatu string persoalan matematika dasar dan mengeluarkan hasil solusi dari persoalan tersebut */
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "boolean.h"
/* Context Free Grammar
* calculate:
* S -> T | S+T | S-T
* kali bagi pangkat:
* T -> F | T*F | T/F | T^F
* tanda:
* F -> (S) | -A | A | A.A | -A.A
* angka:
* A -> 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | AA
*/
//Kamus Global
char *c; //variabel untuk string
boolean cekSyntax; //variabel pengecekan syntax
boolean cekMath; //variabel pengecekan hasil
int count; //variabel pengecekan kurung buka dan kurung tutup
//Spesifikasi Fungsi dan Prosedur
double GetAngka();
/* mengembalikan nilai yang dibaca dari string */
double CPangkat();
/* mengembalikan nilai yang sudah dipangkat */
double CKaliBagi();
/* mengembalikan nilai yang sudah dikali atau dibagi */
double CTambahKurang();
/* mengembalikan nilai yang sudah ditambah atau dikurang */
int main(int argc, char *argv[])
{
//Kamus
char input[100];
double result;
int i, j;
char c1[200] = " .oooooo. oooo oooo . oooooooooooo oooooo oooo oooooooooo.";
char c2[200] = " d8P' `Y8b `888 `888 .o8 `888' `8 `888. .8' `888' `Y8b";
char c3[200] = " 888 .oooo. 888 .ooooo. oooo oooo 888 .oooo. .o888oo .ooooo. oooo d8b 888 `888. .8' 888 888";
char c4[200] = " 888 `P )88b 888 d88' `'Y8 `888 `888 888 `P )88b 888 d88' `88b `888\"\"8P 888oooo8 `888. .8' 888oooo888'";
char c5[200] = " 888 .oP'888 888 888 888 888 888 .oP`888 888 888 888 888 888 ' `888.8' 888 `88b";
char c6[200] = " `88b ooo d8( 888 888 888 .o8 888 888 888 d8( 888 888 . 888 888 888 888 `888' 888 .88P";
char c7[200] = " `Y8bood8P' `Y888\"\"8o o888o `Y8bod8P' `V88V'V8P' o888o `Y888\"\"8o '888' `Y8bod8P' d888b o888o `8' o888bood8P'";
char *v1 = "-v";
char *v2 = "--version";
char *h = "-h";
//Algoritma
if(argc >= 2)
{
if(strcmp(argv[1],h) == 0)
{
system("cls");
printf("%s\n", c1);
printf("%s\n", c2);
printf("%s\n", c3);
printf("%s\n", c4);
printf("%s\n", c5);
printf("%s\n", c6);
printf("%s\n\n", c7);
printf(" Thank you for using our Calculator FVB !\n");
printf(" to use this program :\n");
printf(" 1. Run calculator.exe\n");
printf(" 2. input your Problem Equation\n");
printf(" 3. get your Result!\n");
printf(" 4. if (your equation is not valid) {the result will be \"SYNTAX ERROR\"}\n");
printf(" 5. else if (your equation can't be fathom by our program) {the result will be \"MATH ERROR\"}\n");
printf(" 6. Type 'exit' to close the program\n\n");
printf(" Arguments :\n");
printf(" -h \t\t\t\t Show help\n");
printf(" --version -v \t\t\t Show the version of program\n\n");
printf(" CREDIT BY :\n");
printf(" 1. Christopher Billy Setiawan / 13517050\n");
printf(" 2. Ferdy Santoso / 13517116\n");
printf(" 3. Vincent Budianto / 13517137\n");
}
else if(strcmp(argv[1],v1) == 0 || strcmp(argv[1],v2) == 0)
{
system("cls");
printf("%s\n", c1);
printf("%s\n", c2);
printf("%s\n", c3);
printf("%s\n", c4);
printf("%s\n", c5);
printf("%s\n", c6);
printf("%s\n\n", c7);
printf(" Calculator 2.0 (2018 Nov 19 22:35) \n");
printf(" Made By Christopher Billy Setiawan, Ferdy Santoso, Vincent Budianto \n");
printf(" Informatics Engineering Institute Technology of Bandung 2018 \n");
}
return 0;
}
else
{
system("cls");
for(i = 0; i < 8; i++)
{
printf(" _____________________________________________________________________________________________________________________________________________ \n");
switch (i)
{
case 1 :
{
printf("|%s |\n",c1);
break;
}
case 2 :
{
printf("|%s |\n",c1);
printf("|%s |\n",c2);
break;
}
case 3 :
{
printf("|%s |\n",c1);
printf("|%s |\n",c2);
printf("|%s |\n",c3);
break;
}
case 4 :
{
printf("|%s |\n",c1);
printf("|%s |\n",c2);
printf("|%s |\n",c3);
printf("|%s |\n",c4);
break;
}
case 5 :
{
printf("|%s |\n",c1);
printf("|%s |\n",c2);
printf("|%s |\n",c3);
printf("|%s |\n",c4);
printf("|%s |\n",c5);
break;
}
case 6 :
{
printf("|%s |\n",c1);
printf("|%s |\n",c2);
printf("|%s |\n",c3);
printf("|%s |\n",c4);
printf("|%s |\n",c5);
printf("|%s |\n",c6);
break;
}
case 7 :
{
printf("|%s |\n",c1);
printf("|%s |\n",c2);
printf("|%s |\n",c3);
printf("|%s |\n",c4);
printf("|%s |\n",c5);
printf("|%s |\n",c6);
printf("|%s |\n",c7);
break;
}
}
for(j = (7 - i); j > 0; j--)
{
printf("| |\n");
}
printf("|____________________________________________________________________________________________________________________________________________|\n");
if(i != 7)
{
sleep(1);
system("cls");
}
else
{
sleep(2);
}
}
system("cls");
do
{
system("cls");
printf(" _____________________________________________________________________________________________________________________________________________ \n");
printf("|%s |\n",c1);
printf("|%s |\n",c2);
printf("|%s |\n",c3);
printf("|%s |\n",c4);
printf("|%s |\n",c5);
printf("|%s |\n",c6);
printf("|%s |\n",c7);
printf("|____________________________________________________________________________________________________________________________________________|\n");
cekSyntax = false;
cekMath = false;
count = 0;
printf(" Masukan Input :\n >> ");
scanf("%s", input);
if (strcmp(input, "exit") == 0)
{
system("cls");
printf(" ______________________________________________________________________ \n");
printf("| |\n");
printf("| CREDIT |\n");
printf("| |\n");
printf("| 13517050 - Christopher Billy Setiawan |\n");
printf("| 13517116 - Ferdy Santoso |\n");
printf("| 13517137 - Vincent Budianto |\n");
printf("|______________________________________________________________________|\n");
sleep(2);
exit(0);
}
else
{
c = input;
result = CTambahKurang();
if (*c == ')')
{
count--;
}
else if (*c == '(')
{
count++;
}
if ((cekSyntax) || (count != 0))
{
printf(" SYNTAX ERROR\n");
}
else if (cekMath)
{
printf(" MATH ERROR\n");
}
else
{
printf(" Result : %.2f\n",result);
}
}
}
while (getch());
}
}
//Realisasi Fungsi dan Prosedur
double GetAngka()
/* mengembalikan nilai yang dibaca dari string */
{
//Kamus
double floating;
double number;
double weight;
int i, j, loop;
//Algoritma
number = 0;
if ((*c >= '0') && (*c <= '9'))
{
while ((*c >= '0') && (*c <= '9'))
{
number = (number * 10) + (*c - '0');
c++;
}
}
else if (*c == '-')
{
++c;
if ((*c == '.') || (*c == '+') || (*c == '-') || (*c == '*') || (*c == '/') || (*c == '^') || (*c == ')') || (*c == '\0'))
{
cekSyntax = true;
}
else
{
while ((*c >= '0') && (*c <= '9'))
{
number = (number * 10) + (*c - '0');
c++;
}
number *= -1;
}
}
else if (*c == '(')
{
count++;
c++;
if ((*c == '.') || (*c == '+') || (*c == '*') || (*c == '/') || (*c == '^') || (*c == ')') || (*c == '\0'))
{
cekSyntax = true;
}
else
{
number = CTambahKurang();
if (*c == ')')
{
count--;
c++;
if ((*c == '.') || ((*c >= '0') && (*c <= '9')))
{
cekSyntax = true;
}
}
}
}
if (*c == '.')
{
cekSyntax = false;
++c;
if ((*c == '.') || (*c == '+') || (*c == '-') || (*c == '*') || (*c == '/') || (*c == '^') || (*c == '(') || (*c == ')') || (*c == '\0'))
{
cekSyntax = true;
}
else
{
loop = 0;
weight = 1;
while ((*c >= '0') && (*c <= '9'))
{
weight /= 10;
floating = (*c - '0') * weight;
if (number > 0)
{
number += floating;
}
else if (number < 0)
{
number -= floating;
}
else if (number == 0)
{
c--;
c--;
for (i = 0; i <= loop; i++)
{
c--;
}
if (*c == '-')
{
number -= floating;
}
else
{
number += floating;
}
c++;
c++;
for (j = 0; j <= loop; j++)
{
c++;
}
}
loop++;
c++;
}
}
}
return (number);
}
double CPangkat()
/* mengembalikan nilai yang sudah dipangkat */
{
//Kamus
double exp1;
double exp2;
//Algoritma
exp1 = GetAngka();
while (*c == '^')
{
++c;
if ((*c == '.') || (*c == '+') || (*c == '-') || (*c == '*') || (*c == '/') || (*c == '^') || (*c == ')') || (*c == '\0'))
{
cekSyntax = true;
}
else
{
exp2 = CPangkat();
if ((exp1 < 0) && ((exp2 > 0) && (exp2 < 1)))
{
cekMath = true;
}
else
{
exp1 = pow(exp1, exp2);
}
}
}
return (exp1);
}
double CKaliBagi()
/* mengembalikan nilai yang sudah dikali atau dibagi */
{
//Kamus
double pro1;
double pro2;
//Algoritma
pro1 = CPangkat();
while ((*c == '*') || (*c == '/'))
{
if (*c == '*')
{
++c;
if ((*c == '.') || (*c == '+') || (*c == '-') || (*c == '*') || (*c == '/') || (*c == '^') || (*c == ')') || (*c == '\0'))
{
cekSyntax = true;
}
else
{
pro2 = CPangkat();
pro1 *= pro2;
}
}
if (*c == '/')
{
++c;
if ((*c == '.') || (*c == '+') || (*c == '-') || (*c == '*') || (*c == '/') || (*c == '^') || (*c == ')') || (*c == '\0'))
{
cekSyntax = true;
}
else if (*c == '0')
{
cekMath = true;
}
else
{
pro2 = CPangkat();
if (pro2 == 0)
{
cekMath = true;
}
else
{
pro1 /= pro2;
}
}
}
}
return (pro1);
}
double CTambahKurang()
/* mengembalikan nilai yang sudah ditambah atau dikurang */
{
//Kamus
double sum1;
double sum2;
//Algoritma
sum1 = CKaliBagi();
while ((*c == '+') || (*c == '-'))
{
if (*c == '+')
{
++c;
if ((*c == '.') || (*c == '+') || (*c == '-') || (*c == '*') || (*c == '/') || (*c == '^') || (*c == ')') || (*c == '\0'))
{
cekSyntax = true;
}
else
{
sum2 = CKaliBagi();
sum1 += sum2;
}
}
if (*c == '-')
{
++c;
if ((*c == '.') || (*c == '+') || (*c == '-') || (*c == '*') || (*c == '/') || (*c == '^') || (*c == ')') || (*c == '\0'))
{
cekSyntax = true;
}
else
{
sum2 = CKaliBagi();
sum1 -= sum2;
}
}
}
return (sum1);
}