-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinfixtopostfix.c
More file actions
87 lines (81 loc) · 1.59 KB
/
infixtopostfix.c
File metadata and controls
87 lines (81 loc) · 1.59 KB
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
/* c program to implement conversion of infix expression to postfix*/
#include<stdio.h>
#include<stdbool.h>
char infix[50];
char stack[50];
int top=-1;
void push(char ele)
{
stack[++top]=ele;
}
char pop()
{
return stack[top--];
}
bool isOperator(char op)
{
if(op=='+'|| op=='-'|| op=='*'|| op=='/'|| op=='^'|| op=='(')
return true;
else
return false;
}
int precedence(char op)
{
if(op=='+'||op=='-')
return 1;
else if(op=='*'||op=='/')
return 2;
else if(op=='^')
return 3;
else if(op=='(')
return 0;
}
void postfixConversion()
{
char ele;
int i=0;
while(infix[i]!='\0')
{
if(isOperator(infix[i]))
{
if(infix[i]=='(')
push(infix[i]);
else if(precedence(infix[i])>precedence(stack[top]))
push(infix[i]);
else
{
while(precedence(stack[top])>=precedence(infix[i]))
{
ele=pop();
printf("%c",ele);
}
push(infix[i]);
}
}
else if (infix[i]==')')
{
while(stack[top]!='(')
{
ele=pop();
printf("%c",ele);
}
pop();
}
else
{
printf("%c", infix[i]);
}
i++;
}
while(top!=-1)
{
printf("%c",pop());
}
}
void main()
{
printf("Enter your INFIX expression:\n");
scanf("%[^\n]",infix);
printf("POSTFIX expression:\n");
postfixConversion();
}