-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path13_infix_to_postfix.c
More file actions
85 lines (74 loc) · 1.5 KB
/
13_infix_to_postfix.c
File metadata and controls
85 lines (74 loc) · 1.5 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
#include<stdio.h>
#include<string.h>
#define MAX 50
char stack[MAX];
int top = -1;
char final_string[MAX];
// Function to check Operator Precedence
void precedenceChecker(char opr)
{
if(opr == '/' || opr == '*' || opr == '%')
if(stack[top] == '+' || stack[top] == '-' || stack[top] == '(')
push(opr);
else
{
final_string[strlen(final_string)] = stack[top--];
stack[++top] = opr;
}
else
if(stack[top] == '/' || stack[top] == '*' || stack[top] == '%')
{
pop(top);
stack[++top] = opr;
}
else if(stack[top] == '(')
push(opr);
else
{
final_string[strlen(final_string)] = stack[top--];
stack[++top] = opr;
}
}
// Function to Push into the Stack
void push(char opr)
{
stack[++top] = opr;
}
// Function to Pop from Stack
void pop(int curr_index)
{
int index;
for (index = curr_index; index > -1; index--)
{
if (stack[top] == '(')
{
top--;
break;
}
else
final_string[strlen(final_string)] = stack[top--];
}
}
int main()
{
char strng[MAX];
int index;
printf("Enter Infix String : ");
gets(strng);
printf("Postfix String : ");
push('(');
strng[strlen(strng)] = ')';
for(index = 0; index < strlen(strng); index++)
{
if(strng[index] == '(')
push(strng[index]);
else if((strng[index] >= 'a' && strng[index] <= 'z') || (strng[index] >= 'A' && strng[index] <= 'Z'))
final_string[strlen(final_string)] = strng[index];
else if((strng[index] == ')'))
pop(index);
else
precedenceChecker(strng[index]);
}
puts(final_string);
return 0;
}