/
final_calc.c
362 lines (266 loc) · 8.5 KB
/
final_calc.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
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <math.h>
#include "list.c"
#define BigArray 600
///////////////////////////////////////////////////////////////
/////////////%%%%%% Prototyping Functions %%%%%%%%/////////////
//////////////////////////////////////////////////////////////
//other functions
bool isDigit(char c);
bool isOperator(char c);
char *StringFormatter(char string[]);
Node_t *tokenizer(char string[]);
double convDouble(char string[]);
int isNumeric(char array[]);
int precChecker(char tokens[]);
double Parser(Node_t* LinkedList);
//grammar functions
static double statement_list();
static double statement();
static double E();
static double E_tail(double value);
static double T();
static double T_tail(double value);
static double F();
//global variables
double global_term_val = 0;
double global_e_val = 0;
Node_t *global_current;
//character storage
char allOPS[] = "+-*/%()";
//////////////////////////////////////////////////////////////////////
////////%%%%%%%%%%%% Digit or Operator Checker %%%%%%%%%%%%%%/////////
//////////////////////////////////////////////////////////////////////
bool isDigit(char c){
int val = c - '0';
if ((val >= 0 && val <= 9) || c == '.'){
return true;
}
return false;
}
bool isOperator(char c){
int charlen = strlen(allOPS);
for(int i = 0 ; i< charlen; i++){
if (allOPS[i] == c){
return true;
}
}
return false;
}
//////////////////////////////////////////////////////////////////////
///////%%%%%%%%% String Formatter /w Single Spaces %%%%%%%%%%%////////
//////////////////////////////////////////////////////////////////////
//for formatting any string into numbers then spaces then operators then numbers and so on //
char *StringFormatter(char string[]){
char *newString = (char*)malloc(800 * sizeof(char));
if (!newString) {
return NULL;
}
int len = strlen(string);
int i = 0;
int desti = 0;
char *space = " ";
while (i<len) {
if (isDigit(string[i])){
newString[desti] = string[i];
desti++;
}
else if ((i+1 < len) && ((string[i] == '+' && string[i+1] == '+') || (string[i] == '-' && string[i+1] == '-'))){
newString[desti] = ' ';
desti++;
newString[desti] = string[i];
desti++;
newString[desti] = string[i];
desti++;
newString[desti] = ' ';
desti++;
i++;
}
else if (isOperator(string[i])){
newString[desti] = ' ';
desti++;
newString[desti] = string[i];
desti++;
newString[desti] = ' ';
desti++;
}
i++;
}
newString[desti] = '\0';
return newString;
}
//////////////////////////////////////////////////////////////////////
///////%%%%%%%%% String Tokenizer using LinkedList %%%%%%%%%%%////////
//////////////////////////////////////////////////////////////////////
//to tokenize everything//
//takes in the formatted string and returns a list//
Node_t *tokenizer(char string[]){
Node_t *token_list = NULL;
const char space[2] = " ";
char *id_token;
id_token = strtok(string, space);
token_list = initList(id_token,token_list);
while(id_token != NULL){
id_token = strtok(NULL, space);
if (id_token == NULL)
break;
addNode(id_token,token_list);
}
return token_list;
}
//////////////////////////////////////////////////////////////////////
///////////%%%%%%%%%%% Converting to a double %%%%%%%%%%%%////////////
/////////////////////////////////////////////////////////////////////
double convDouble(char string[]){
double num;
sscanf(string, "%lf", &num);
return num;
}
//////////////////////////////////////////////////////////////////////
///////%%%%%%% Checks if we can convert it to a double %%%%%%%%///////
//////////////////////////////////////////////////////////////////////
//if numeric return NOT 0
int isNumeric(char array[]){
if(array==NULL || array == '\0'){
return 0;
}
char *num;
strtod(array, &num);
return *num == '\0';
}
//////////////////////////////////////////////////////////////////////
////////////////%%%%%%%%%%%%% Grammar %%%%%%%%%%%%%%//////////////////
//////////////////////////////////////////////////////////////////////
void next_token(){
if ( global_current->next != NULL ){
global_current = global_current->next;
}
}
static double statement_list(){
printf("Inside statement_list \n");
if ( isNumeric(global_current->val) != 0 ){
double temp = statement();
return temp;
}
if (strcmp(global_current->val,"(") == 0){
double temp = statement();
return temp;
}
}
static double statement(){
printf("Inside statement\n");
return E();
}
static double E(){
printf("Inside E\n");
return E_tail(T());
}
static double T(){
printf("Inside T\n");
return T_tail(F());
}
static double F(){
printf("Inside F\n");
if( strcmp(global_current->val,"(") == 0 ){
next_token();
double hold = E();
if ( strcmp(global_current->val,")") == 0 ){
next_token();
return hold;
}
else{
printf("Error\n");
}
}
if ( isNumeric(global_current->val) != 0){
double hold = convDouble(global_current->val);
next_token();
return hold;
}
}
static double T_tail(double val){
printf("Inside T_tail\n");
if ( strcmp(global_current->val,"*") == 0 ){
next_token();
return val * T_tail(F());
}
else if ( strcmp(global_current->val,"/") == 0 ){
next_token();
return val / T_tail(F());
}
return val;
}
static double E_tail(double val){
printf("Inside E_tail\n");
if ( strcmp(global_current->val,"+") == 0 ){
next_token();
return E_tail(val + T());
}
else if ( strcmp(global_current->val,"-") == 0 ){
next_token();
return E_tail(val - T());
}
return val;
}
//////////////////////////////////////////////////////////////////////
////////////////%%%%%%%%%%%%% Parser %%%%%%%%%%%%%%///////////////////
//////////////////////////////////////////////////////////////////////
double Parser(Node_t* LinkedList){
Node_t *current = LinkedList;
global_current = current;
double result = statement_list();
return result;
}
//////////////////////////////////////////////////////////////////////
///////%%%%%%%%% Main Method for Testing WhatsUp!! %%%%%%%%%%%////////
//////////////////////////////////////////////////////////////////////
int main(int argc, char *argv[]){
if (argc != 2){
printf("usage: %s filename\n",argv[0]);
}
else{
FILE *file = fopen(argv[1],"r");
FILE *outfile = fopen("out.txt","w");
if(file == 0){
printf("Could not open file\n");
}
else{
printf("Reading file");
char x;
while( (x = fgetc(file)) != EOF ){
int i = 0;
char Buffer[BigArray];
while( x !=';'){
Buffer[i] = x;
printf("%c\n",Buffer[i]);
if (x == '\n'){
continue;
}
i++;
x = fgetc(file);
}
Buffer[i] = '\0';
char *form = StringFormatter(Buffer);
char formattedString[BigArray];
for(int i =0; i<strlen(form);i++){
formattedString[i]=form[i];
}
printf("%s\n",formattedString);
char printArr[BigArray];
strcpy(printArr,formattedString);
Node_t *tokenList = NULL;
tokenList = tokenizer(formattedString);
printList(tokenList);
double mini_results = Parser(tokenList);
printf("%s\n",printArr);
fprintf(outfile, "%s %s %f\n", printArr, "=", mini_results);
x = fgetc(file);
}
fclose(file);
fclose(outfile);
}
}
}