-
Notifications
You must be signed in to change notification settings - Fork 0
/
10_PostfixEval.c
89 lines (80 loc) · 2.22 KB
/
10_PostfixEval.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
/*
10. Write a program to evaluate postfix expressions.
*/
#include <stdio.h>
#include <ctype.h>
#include <math.h>
#define MAX 20
int stack[20], top = -1;
void isEmpty() { // Check if the stack is empty
if(top == -1) { // If the stack is empty, print underflow
printf("STACK UNDERFLOW");
}
}
void isFull() { // Check if the stack is full.
if(top == MAX) { // If the stack is full, print overflow
printf("STACK OVERFLOW");
}
}
void push(int x) { // Push an element into the stack
if(top == MAX) { // If the stack is full
isFull(); // Print overflow
}
else { // Else push the element into the stack
stack[++top] = x;
}
}
int pop() { // Pop an element from the stack
if(top == -1) { // If the stack is empty
isEmpty(); // Print underflow
}
else { // Else pop the element from the stack
return stack[top--];
}
}
int postEval(char exp[]) { // Evaluate the postfix expression
char *e;
int num, n1, n2, tn;
e = exp;
while(*e != '\0') {
if(isdigit(*e)) { // If the character is a digit
num = *e - 48; // Convert the character to integer
push(num);
}
else {
n1 = pop();
n2 = pop();
switch(*e) {
case '+':
tn = n1 + n2;
break;
case '-':
tn = n1 - n2;
break;
case '*':
tn = n1 * n2;
break;
case '/':
tn = n1 / n2;
break;
case '^':
tn = pow(n1, n2);
break;
default:
printf("\nOperator Unknown!");
return 0;
}
push(tn);
}
e++;
}
return pop();
}
int main() {
int ch = 1;
char exp[MAX];
printf("Enter the expression: ");
scanf("%s", exp);
printf("\nThe Value of entered postfix %s expression is: %d\n", exp, postEval(exp));
return 0;
}