Skip to content

Commit 7281678

Browse files
Create Infix_to_Postfix_Conversion_Using_Stack.c
1 parent c890295 commit 7281678

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Give an input Infix Expression, write the program to convert the given infix expression to postfix expression using Stack Operations.
2+
3+
// Input : Infix Expression
4+
5+
// Output : Postfix Expression
6+
7+
#include<stdio.h>
8+
#include<string.h>
9+
char s[20];
10+
int top=-1;
11+
int pri(char c)
12+
{
13+
switch(c)
14+
{
15+
case '+':
16+
case '-':return 1;
17+
break;
18+
case '*':
19+
case '/':return 2;
20+
break;
21+
default: return 0;
22+
}
23+
}
24+
void push(char c)
25+
{
26+
top++;
27+
s[top]=c;
28+
}
29+
char pop()
30+
{
31+
int a=top;
32+
top--;
33+
return s[a];
34+
}
35+
int main()
36+
{
37+
char str[20];
38+
scanf("%[^\n]s",str);
39+
for(int i=0;i<strlen(str);i++)
40+
{
41+
if(str[i]=='+'||str[i]=='-'||str[i]=='*'||str[i]=='/')
42+
{
43+
if(top==-1)
44+
push(str[i]);
45+
else
46+
{
47+
if(pri(s[top])>=pri(str[i]))
48+
{
49+
printf("%c",pop());
50+
push(str[i]);
51+
}
52+
else
53+
push(str[i]);
54+
}
55+
}
56+
else if(str[i]=='(')
57+
push(str[i]);
58+
else if(str[i]==')')
59+
{
60+
while(s[top]!='(')
61+
printf("%c",pop());
62+
pop();
63+
}
64+
else if(str[i]>='a' && str[i]<='z')
65+
printf("%c",str[i]);
66+
}
67+
while(top>=0)
68+
printf("%c",pop());
69+
return 0;
70+
}

0 commit comments

Comments
 (0)