forked from TARANG0503/DSA-Practice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpostfix_expression_evaluation.c
89 lines (73 loc) · 1.39 KB
/
postfix_expression_evaluation.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
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
#define size 100
int stack[size];
int top = -1;
void push(int x)
{
if(top == size-1)
{
printf("Stack Overflow\n");
}
else
{
stack[++top] = x;
}
}
int pop()
{
if(top == -1)
{
return 0;
}
else
{
int ele;
ele = stack[top--];
return ele;
}
}
int main()
{
char arr[50];
int i=0, op1, op2;
printf("Enter the Postfix expression\n");
scanf("%s",arr);
for(i=0; arr[i]!='\0'; i++)
{
if(isdigit(arr[i]))
{
push(arr[i]-'0');
}
else
{
op2 = pop();
op1 = pop();
switch(arr[i])
{
case '+':
push(op1 + op2);
break;
case '-':
push(op1 - op2);
break;
case '*':
push(op1 * op2);
break;
case '/':
push(op1 / op2);
break;
case '^':
push(op1 ^ op2);
break;
default:
printf("\nInvalid operation\n");
break;
}
}
}
printf("\nThe result is %d",stack[top]);
return 0;
}