Skip to content

Commit 884bae2

Browse files
Merge pull request matthewsamuel95#686 from pockemon/patch1
Added infix to postfix and prefix conversion using stack
2 parents c9026a8 + 7b45a65 commit 884bae2

File tree

2 files changed

+209
-0
lines changed

2 files changed

+209
-0
lines changed
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
#include<stdio.h>
2+
#include<string.h>
3+
#define size 100
4+
int top=-1;
5+
char stack[size];
6+
int is_operator(char symbol);
7+
char pop();
8+
void push(char item);
9+
int precendence(char symbol);
10+
11+
int main()
12+
{
13+
char infix[size],postfix[size],item;
14+
char temp;
15+
int i=0,j=0;
16+
printf("enter infix expression\n");
17+
scanf("%s",infix);
18+
while(infix[i]!='\0')
19+
{
20+
item=infix[i];
21+
if(item=='(')
22+
{
23+
push(item);
24+
}
25+
else if(item>='a'&& item<='z'||item>='A'&& item<='Z')
26+
{
27+
postfix[j]=item;
28+
j++;
29+
}
30+
else if(is_operator(item)==1)
31+
{
32+
temp=pop();
33+
while(is_operator(temp)==1 && precendence(temp)>=precendence(item))
34+
{
35+
postfix[j]=temp;
36+
j++;
37+
temp=pop();
38+
}
39+
push(temp);
40+
push(item);
41+
}
42+
else if(item==')')
43+
{
44+
temp=pop();
45+
while(temp!='(')
46+
{
47+
postfix[j]=temp;
48+
j++;
49+
temp=pop();
50+
}
51+
}
52+
else
53+
{
54+
printf("\n invalid\n");
55+
exit(0);
56+
}
57+
i++;
58+
}
59+
while(top>-1) //for no paranthesis
60+
{
61+
postfix[j]=pop();
62+
j++;
63+
}
64+
postfix[j]='\0';
65+
printf("the postfix expression is\n");
66+
printf("%s",postfix);
67+
return 0;
68+
}
69+
70+
void push(char item)
71+
{
72+
if(top>=size-1)
73+
printf("stack overfloaw\n");
74+
else
75+
{
76+
top++;
77+
stack[top]=item;
78+
}
79+
}
80+
81+
int is_operator(char symbol)
82+
{
83+
if(symbol=='^'||symbol=='+'||symbol=='-'||symbol=='*'||symbol=='/')
84+
return 1;
85+
else
86+
return 0;
87+
88+
}
89+
90+
char pop()
91+
{
92+
char item;
93+
item=stack[top];
94+
top--;
95+
return item;
96+
}
97+
98+
int precendence(char symbol)
99+
{
100+
if(symbol=='^')
101+
return 3;
102+
else if(symbol=='/'||symbol=='*')
103+
return 2;
104+
else if(symbol=='+'||symbol=='-')
105+
return 1;
106+
else
107+
return 0;
108+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
#include<stdio.h>
2+
#include<string.h>
3+
#define size 100
4+
int top=-1;
5+
char stack[size];
6+
int is_operator(char symbol);
7+
char pop();
8+
void push(char item);
9+
int precendence(char symbol);
10+
int main()
11+
{
12+
char infix[size],prefix[size],item;
13+
char temp;
14+
int i=0,j=0,len;
15+
printf("enter infix expression\n");
16+
gets(infix);
17+
len=strlen(infix);
18+
for(i=len;i>=0;i--)
19+
{
20+
item=infix[i];
21+
if(item==')')
22+
{
23+
push(item);
24+
}
25+
else if(item>='a'&& item<='z'||item>='A'&& item<='Z')
26+
{
27+
prefix[j]=item;
28+
j++;
29+
}
30+
else if(is_operator(item)==1)
31+
{
32+
temp=pop();
33+
while(is_operator(temp)==1 && precendence(temp)>=precendence(item))
34+
{
35+
prefix[j]=temp;
36+
j++;
37+
temp=pop();
38+
}
39+
push(temp);
40+
push(item);
41+
}
42+
else if(item=='(')
43+
{
44+
temp=pop();
45+
while(temp!=')')
46+
{
47+
prefix[j]=temp;
48+
j++;
49+
temp=pop();
50+
}
51+
}
52+
}
53+
while(top>-1)
54+
{
55+
prefix[j]=pop();
56+
j++;
57+
}
58+
prefix[j]='\0';
59+
printf("the prefix expression is\n");
60+
printf("%s",strrev(prefix));
61+
return 0;
62+
}
63+
64+
void push(char item)
65+
{
66+
if(top>=size-1)
67+
printf("stack overfloaw\n");
68+
else
69+
{
70+
top++;
71+
stack[top]=item;
72+
}
73+
}
74+
75+
int is_operator(char symbol)
76+
{
77+
if(symbol=='^'||symbol=='+'||symbol=='-'||symbol=='*'||symbol=='/')
78+
return 1;
79+
else
80+
return 0;
81+
}
82+
83+
char pop()
84+
{
85+
char item;
86+
item=stack[top];
87+
top--;
88+
return item;
89+
}
90+
91+
int precendence(char symbol)
92+
{
93+
if(symbol=='^')
94+
return 3;
95+
else if(symbol=='/'||symbol=='*')
96+
return 2;
97+
else if(symbol=='+'||symbol=='-')
98+
return 1;
99+
else
100+
return 0;
101+
}

0 commit comments

Comments
 (0)