-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrpn.c
More file actions
333 lines (296 loc) · 6.86 KB
/
rpn.c
File metadata and controls
333 lines (296 loc) · 6.86 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
#define _GNU_SOURCE
#include <ctype.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <regex.h>
#include <assert.h>
// Input struct
typedef enum InputState_t { NUMBER, OPERAND, DISPLAY, DELETE, EXIT, INVALID } InputState_t;
typedef struct Input_t {
InputState_t state;
union {
double value;
char operand;
};
} Input_t;
// Double linked list
typedef struct Node_t {
double value;
struct Node_t *prev, *next;
} Node_t;
typedef struct List_t {
Node_t *head, *tail;
uintmax_t count;
} List_t;
// Helper functions
Node_t *createNode(double value) {
Node_t *ret = (Node_t *) malloc(sizeof(Node_t));
assert(ret);
*ret = (Node_t) {
.value = value
};
return ret;
};
void deleteNode(Node_t *node) {
free(node);
}
void pushList(List_t *list, Node_t *node) {
if(list->count == 0) {
*list = (List_t) {
.head = node,
.tail = node,
.count = 1
};
return ;
}
list->tail->next = node;
node->prev = list->tail;
list->tail = node;
list->count++;
}
Node_t *popList(List_t *list) {
if (list->count == 0) {
return NULL;
}
else if (list->count == 1) {
Node_t *curr = list->tail;
*list = (List_t){};
return curr;
}
Node_t *curr = list->tail;
list->tail = curr->prev;
list->tail->next = NULL;
list->count--;
return curr;
}
void displayNode(const Node_t *node) {
printf("\t%g\n", node->value);
}
void displayList(const List_t *list) {
printf ("Table of current values: \n");
for(Node_t *curr = list->head; curr != NULL; curr = curr->next) {
displayNode(curr);
}
}
// computational functions
double add(double x, double y) {
return x + y;
}
double sub(double x, double y) {
return x - y;
}
double mult(double x, double y) {
return x * y;
}
double divd(double x, double y) {
return x / y;
}
void operation(List_t *list, char operand) {
if(list->count < 2) {
printf("Not enough numbers to perform operation on.\n");
return ;
}
Node_t *curr = popList(list);
Node_t *prev = popList(list);
double total;
switch(operand) {
case '+':
total = add(prev->value, curr->value);
pushList(list, createNode(total));
break;
case '-':
total = sub(prev->value, curr->value);
pushList(list, createNode(total));
break;
case '*':
total = mult(prev->value, curr->value);
pushList(list, createNode(total));
break;
case '/':
if(curr->value == 0) {
printf("Math Error: division by zero");
pushList(list, prev);
pushList(list, curr);
return ;
}
total = divd(prev->value, curr->value);
pushList(list, createNode(total));
break;
default:
pushList(list, prev);
pushList(list, curr);
printf("Invalid operand\n");
return ;
}
free(prev);
free(curr);
}
void operate(Input_t input) {
static List_t list;
switch(input.state) {
case NUMBER:
Node_t *newValue = createNode(input.value);
pushList(&list, newValue);
break;
case DELETE:
if(list.count == 0) {
printf("No more elements!!\n");
break;
}
popList(&list);
break;
case INVALID:
printf("Invalid operation \n");
break;
case DISPLAY:
displayList(&list);
break;
case OPERAND:
operation(&list, input.operand);
break;
default:
printf("Invalid State\n");
break;
}
}
void printRegError(int errcode, const regex_t *regex) {
size_t total = regerror(errcode, regex, NULL, 0) + 1;
char buffer[total];
regerror(errcode, regex, buffer, total);
printf("%s\n", buffer);
}
bool isNumber(const char *input) {
static regex_t numReg;
static bool compiled = false;
static const char *regex = "^\\s*[+-]?([0-9]+\\.?[0-9]*|\\.[0-9]+)\\s*$";
if(!compiled) {
int err = regcomp(&numReg, regex, REG_EXTENDED);
if(err) {
printRegError(err, &numReg);
}
assert(err == 0);
}
return regexec(&numReg, input, 0, NULL, 0) == 0;
}
bool isOperand(const char *input) {
static regex_t operandReg;
static bool compiled = false;
static const char *regex = "^\\s*[+*/-]\\s*$";
if(!compiled) {
int err = regcomp(&operandReg, regex, REG_EXTENDED);
if(err) {
printRegError(err, &operandReg);
}
assert(err == 0);
}
return regexec(&operandReg, input, 0, NULL, 0) == 0;
}
bool isDisplay(const char *input) {
static regex_t displayReg;
static bool compiled = false;
static const char *regex = "^\\s*display\\s*$";
if(!compiled) {
int err = regcomp(&displayReg, regex, REG_EXTENDED | REG_ICASE);
if(err) {
printRegError(err, &displayReg);
}
assert(err == 0);
}
return regexec(&displayReg, input, 0, NULL, 0) == 0;
}
bool isDelete(const char *input) {
static regex_t deleteReg;
static bool compiled = false;
static const char *regex = "^\\s*delete\\s*$";
if(!compiled) {
int err = regcomp(&deleteReg, regex, REG_EXTENDED | REG_ICASE);
if(err) {
printRegError(err, &deleteReg);
}
assert(err == 0);
}
return regexec(&deleteReg, input, 0, NULL, 0) == 0;
}
bool isExit(const char *input) {
static regex_t exitReg;
static bool compiled = false;
static const char *regex = "^\\s*exit\\s*$";
if(!compiled) {
int err = regcomp(&exitReg, regex, REG_EXTENDED | REG_ICASE);
if(err) {
printRegError(err, &exitReg);
}
assert(err == 0);
}
return regexec(&exitReg, input, 0, NULL, 0) == 0;
}
char getOperand(const char *input) {
char curr;
const char *temp = input;
do {
curr = *(temp++);
}while(isspace(curr));
return curr;
}
Input_t getInput() {
char *userInput = NULL;
size_t len;
getline(&userInput, &len, stdin);
Input_t ret;
if(isNumber(userInput)) {
ret = (Input_t) {
.state = NUMBER,
.value = strtod(userInput, NULL)
};
} else if (isOperand(userInput)) {
ret = (Input_t) {
.state = OPERAND,
.operand = getOperand(userInput)
};
} else if (isDisplay(userInput)) {
ret = (Input_t) {
.state = DISPLAY
};
} else if (isDelete(userInput)) {
ret = (Input_t) {
.state = DELETE
};
} else if (isExit(userInput)) {
ret = (Input_t) {
.state = EXIT
};
} else {
ret = (Input_t) {
.state = INVALID
};
}
free(userInput);
return ret;
}
void printMenu() {
printf("Your input\n");
printf("\t1. Number\n");
printf("\t2. Operand (+,-,*,/)\n");
printf("\t3. <display>, to display the current stack layout\n");
printf("\t4. <delete>, to delete previous number\n");
printf("\t5. <exit>, to quit the program\n");
}
void mainLoop() {
while(true) {
printMenu();
Input_t inp = getInput();
if(inp.state == EXIT) {
break;
}
operate(inp);
}
}
int main() {
printf ("\n\nWelcome to RPN (Reverse Polish Notation) calculator!!\n");
mainLoop();
printf ("\n\nGood Bye !! \n\n");
return EXIT_SUCCESS;
}