-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpostfix.c
101 lines (84 loc) · 1.54 KB
/
postfix.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
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#define MAX 25
char a[MAX],b[MAX],stack[MAX];
int top=-1;
void push(char c){
if(top>MAX){
printf("An Error Occured! Please update MAX for better Perfomance.\n");
exit(0);
}
else{
top++;
stack[top]=c;
}
}
char pop(){
if(top==-1) {
printf("An Error Occured!\n");
exit(0);
}
else{
char a=stack[top];
top--;
return a;
}
}
void display(){
for(int i=0;b[i]!='\0';i++){
printf("%c",b[i]);
}
}
void insert(){
scanf("%s",a);
}
int priority(char c){
if(c=='+' ||c=='-') return 1;
else if(c=='*' ||c=='/') return 2;
else if(c=='^') return 3;
else if(c=='(') return 0;
}
int isoperator(char x){
if(x=='*'||x=='^'||x=='/'||x=='-'||x=='+') return 1;
else return 0;
}
void convert(){
push('(');
int i;
for (i=0;a[i]!='\0';i++);
a[i++]=')';
a[i]='\0';
int j=0;
for (i=0;a[i]!='\0';i++){
if(isalpha(a[i])){
b[j++]=a[i];
}
else if(a[i]=='(') push(a[i]);
else if(a[i]==')'){
while(stack[top]!='('){
b[j++]=pop();
}
char z=pop();
}
else if(isoperator(a[i])){
char c=a[i];
char d=stack[top];
while(priority(c)<=priority(d)){
b[j++]=pop();
}
push(c);
}
}
b[j]='\0';
}
void main(){
while(1){
printf("Enter the infix expression:");
insert();
convert();
printf("The postfix expression is:");
display();
printf("\n");
}
}