forked from thisisshub/HacktoberFest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPOSTFIXE.C
60 lines (59 loc) · 1.08 KB
/
POSTFIXE.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
#include<stdio.h>
#include<conio.h>
#define SIZE 50
int top = -1 ;
int stack[SIZE] ;
void push(int value)
{
if(top == SIZE-1)
{
printf("cannot insert!!") ;
}
else
{
top++ ;
stack[top] = value ;
}
}
void evaluateTop(char op)
{
switch(op)
{
case '+' : stack[top-1] = stack[top-1] + stack[top] ;
break ;
case '-' : stack[top-1] = stack[top-1] - stack[top] ;
break ;
case '*' : stack[top-1] = stack[top-1] * stack[top] ;
break ;
case '/' : stack[top-1] = stack[top-1] / stack[top] ;
break ;
//case '%' : stack[top] = stack[top-1] stack[top] ;
// break ;
default : printf("Enter valid operator!!") ;
}
top = top - 1 ;
}
void main()
{
char postFix[SIZE] ;
int i, value, k = 0 ;
clrscr() ;
printf("Enter a postfix expression: ") ;
scanf("%s", postFix) ;
for(i = 0 ; i < strlen(postFix) ; i++)
{
if(isalpha(postFix[i]))
{
printf("Enter the value of %c", 97+k) ;
scanf("%d", &value) ;
push(value) ;
k++ ;
}
else
{
evaluateTop(postFix[i]) ;
}
}
printf("\nThe answer after evaluation is: %d", stack[top]) ;
getch() ;
}