-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinfix_to_postfixsuccesful.c
97 lines (95 loc) · 1.96 KB
/
infix_to_postfixsuccesful.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
// Online C compiler to run C program online
#include <stdio.h>
#include<stdbool.h>
#define size 100
char stack[size];
int top=-1;
void Push(char val){
if(top==size-1){
printf("stack overflow");
}
else{
top++;
stack[top]=val;
}
}
char pop(){
if(top==-1){
return -1;
}
else{
int res=stack[top];
top--;
return res;
}
}
int Top(){
if(top==-1){
return -1;
}
else{
int res=stack[top];
return res;
}
}
bool isempty(){
return top==-1;
}
int precedence(char c){
if(c=='^')return 3;
else if(c=='*' ||c=='/')return 2;
else if(c=='+' ||c=='-')return 1;
else return -1;
}
void Infix_to_Postfix(char infix[]){
char postfix[100];
int i=0;
int j=0;
while(infix[i]!='\0'){
if(infix[i]>='a' && infix[i]<='z' || infix[i]>='A' && infix[i]<='Z'){
postfix[j]=infix[i];
i++;
j++;
}
else if(infix[i]=='('){
Push(infix[i]);
i++;
}
else if(infix[i]==')'){
while(Top()!='('){
postfix[j]=pop();
j++;
}
pop();
i++;
}
else{
if(precedence(infix[i])>precedence(Top())){
Push(infix[i]);
i++;
}
else{
while(precedence(infix[i])<=precedence(Top())){
postfix[j]=pop();
j++;
}
Push(infix[i]);
i++;
}
// Push(infix[i]);
}
}
while(!isempty()){
postfix[j]=pop();
j++;
}
postfix[j]='\0';
for(i = 0 ; postfix[i]!='\0' ; i++){
printf("%c ",postfix[i]);
}
}
int main() {
char infix[100];
scanf("%s",infix);
Infix_to_Postfix(infix);
}