-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInfix_to_Postfix_Conversion_Using_Stack.c
70 lines (67 loc) · 1.34 KB
/
Infix_to_Postfix_Conversion_Using_Stack.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
// Give an input Infix Expression, write the program to convert the given infix expression to postfix expression using Stack Operations.
// Input : Infix Expression
// Output : Postfix Expression
#include<stdio.h>
#include<string.h>
char s[20];
int top=-1;
int pri(char c)
{
switch(c)
{
case '+':
case '-':return 1;
break;
case '*':
case '/':return 2;
break;
default: return 0;
}
}
void push(char c)
{
top++;
s[top]=c;
}
char pop()
{
int a=top;
top--;
return s[a];
}
int main()
{
char str[20];
scanf("%[^\n]s",str);
for(int i=0;i<strlen(str);i++)
{
if(str[i]=='+'||str[i]=='-'||str[i]=='*'||str[i]=='/')
{
if(top==-1)
push(str[i]);
else
{
if(pri(s[top])>=pri(str[i]))
{
printf("%c",pop());
push(str[i]);
}
else
push(str[i]);
}
}
else if(str[i]=='(')
push(str[i]);
else if(str[i]==')')
{
while(s[top]!='(')
printf("%c",pop());
pop();
}
else if(str[i]>='a' && str[i]<='z')
printf("%c",str[i]);
}
while(top>=0)
printf("%c",pop());
return 0;
}