-
Notifications
You must be signed in to change notification settings - Fork 2
/
postfixEvaluation.c
82 lines (78 loc) · 1.14 KB
/
postfixEvaluation.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
/*
C Program for implementing Evaluating postfix Expression
Author: Vivek K J
S3 CSA
*/
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#define MAX_SIZE 100
// Global variables
int stack[MAX_SIZE], top=-1;
// Function to push
void push(int item){
if(top==MAX_SIZE-1){
printf("Stack Overflow");
}else{
top=top+1;
stack[top] = item;
}
}
// Function to pop
int pop(){
if(top==-1){
printf("Stack Underflow");
}else{
int item = stack[top];
top = top-1;
return item;
}
}
void display(){
if(top==-1){
printf("Stack Underflow");
}else{
for(int i=0; i<=top; i++){
printf("%d \t", stack[i]);
}
}
}
// Main function
void main(){
char postfix[100];
printf("Enter Postfix Expression: ");
scanf("%s", postfix);
for(int i=0; postfix[i]!=')'; i++)
{
if(isalnum(postfix[i]))
{
push(postfix[i] - '0');
}
else
{
int res;
int a = pop();
int b = pop();
switch(postfix[i])
{
case '+':
res=b+a;
break;
case '-':
res=b-a;
break;
case '*':
res=b*a;
break;
case '/':
res=b/a;
break;
case '^':
res=b^a;
break;
}
push(res);
}
}
display();
}