-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPostfixEvaluation.c
102 lines (102 loc) · 2.09 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>
#include"ss.h"
int priority(char x){
if(x == '(')
return 0;
else if(x == '+' || x == '-')
return 1;
else if(x == '*' || x == '/')
return 2;
return -1;
}
int cal(char v1, char v2, char op){
int i1 = (int)v1-48,i2 = (int)v2-48;
if(op == '+')
i1+=i2;
else if(op == '-')
i1-=i2;
else if(op == '*')
i1*=i2;
else if(op == '/')
i1/=i2;
return i1;
}
void evaluate(char exp[]){
int fval;
char *x = exp;
stack *top = NULL;
while(*x!='\0'){
if(isalnum(*x)){
pushing(&top,*x);
}
else if(top->next!=NULL){
fval = cal(top->next->val,top->val,*x);
pop(&top);
pop(&top);
pushing(&top,fval+48);
}
else{
break;
}
x++;
}
printf("Answer: %d",fval);
}
void postfix(char exp[]){
int i=0;
char c,arr[100],*n,*x;
x = exp;
stack *top = NULL;
printf("Postfix: ");
while(*x!='\0'){
if(isalnum(*x)){
printf("%c",*x);
arr[i] = *x;
i++;
}
else if(*x == '('){
pushing(&top,*x);
}
else if(*x == ')'){
while(top->val!='('){
c = pop(&top);
if(c != '('){
printf("%c",c);
arr[i] = c;
i++;
}
}
if(top->val == '(')
pop(&top);
}
else{
while(top!=NULL && priority(top->val) >= priority(*x)){
c = pop(&top);
printf("%c",c);
arr[i] = c;
i++;
}
pushing(&top,*x);
}
x++;
}
while(top!=NULL){
c = pop(&top);
printf("%c",c);
arr[i] = c;
i++;
}
printf("\n");
evaluate(arr);
}
int main(){
char str[100],*arr;
int i = 0;
printf("Enter String: ");
scanf("%s",str);
postfix(str);
return 0;
}