-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPostfixOperate.c
More file actions
54 lines (48 loc) · 781 Bytes
/
PostfixOperate.c
File metadata and controls
54 lines (48 loc) · 781 Bytes
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
#include <stdio.h>
#include <stdlib.h>
int stack[20];
int top = -1;
void push(int x);
int pop();
int main() {
char exp[20];
char* e;
int n1, n2, n3, num;
printf("Enter the expression :: ");
scanf_s("%s", exp, sizeof(exp));
e = exp;
while (*e != '\0') {
if (isdigit(*e)) { //Determine if character is numeric
num = *e - 48;
push(num);
}
else {
n1 = pop();
n2 = pop();
switch (*e) {
case '+':
n3 = n1 + n2;
break;
case '-':
n3 = n2 - n1;
break;
case '*':
n3 = n2 * n1;
break;
case '/':
n3 = n2 / n1;
break;
}
push(n3);
}
e++; // address ++
}
printf("\nThe result of expression %s = %d\n\n", exp, pop());
return 0;
}
void push(int x) {
stack[++top] = x;
}
int pop() {
return stack[top--];
}