-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInfix_to_Postfix_conversion_using_stack.c
More file actions
90 lines (75 loc) · 1.56 KB
/
Infix_to_Postfix_conversion_using_stack.c
File metadata and controls
90 lines (75 loc) · 1.56 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
86
87
88
89
90
// infix to postfix using stack
#include <stdio.h>
#include <string.h>
#define MAX 50
char infix_exp[MAX], postfix_exp[MAX], stack[MAX];
int top = -1;
int isempty()
{
return top == -1;
}
int isfull()
{
return top == MAX - 1;
}
int precedence(char op)
{
if (op == '^')
{
return 3;
}
else if (op == '*' || op == '/')
{
return 2;
}
else if (op == '+' || op == '-')
{
return 1;
}
else
{
return -1;
}
}
void infixToPostfix(char infix[])
{
int i, j, k;
for (i = 0, j = 0; i < strlen(infix); i++)
{
if (!isfull() && infix[i] == '(')
{
stack[++top] = infix[i];
}
else if (infix[i] >= 'a' && infix[i] <= 'z' || infix[i] >= 'A' && infix[i] <= 'Z')
{
postfix_exp[j++] = infix[i];
}
else if (infix[i] == ')')
{
while (!isempty() && stack[top] != '(')
{
postfix_exp[j++] = stack[top--];
}
top--;
}
else if (infix[i] == '+' || infix[i] == '-' || infix[i] == '*' || infix[i] == '/' || infix[i] == '^')
{
while (!isempty() && precedence(stack[top]) >= precedence(infix[i]))
{
postfix_exp[j++] = stack[top--];
}
stack[++top] = infix[i];
}
}
while (top > -1)
{
postfix_exp[j++] = stack[top--];
}
puts(postfix_exp);
}
int main()
{
printf("Enter an Infix Expression.\n");
gets(infix_exp);
infixToPostfix(infix_exp);
}