-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path22_infix_to_postfix.c
65 lines (64 loc) · 1.29 KB
/
22_infix_to_postfix.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
//Govind J Nair
//S3-D
//23
#include <stdio.h>
int instackPriority(char symbol) {
switch(symbol) {
case '+':
case '-':
return 2;
case '*':
case '/':
return 4;
case '^':
return 6;
case '(':
return 0;
}
}
int incomingPriority(char symbol) {
switch(symbol) {
case '+':
case '-':
return 1;
case '*':
case '/':
return 3;
case '^':
return 5;
}
}
int isOperand(char symbol) {
if ((symbol>=97 && symbol<=122) || (symbol>=65 && symbol<=90)) {
return 1;
}
return 0;
}
int main () {
char infix[20], stack[100];
int i, top=-1;
printf("Enter the infix expression : ");
scanf(" %s", infix);
for (i=0; infix[i] != '\0'; i++) {
if (isOperand(infix[i])) {
printf("%c", infix[i]);
} else if (infix[i] == '(') {
stack[++top] = infix[i];
} else if (infix[i] == ')') {
while (stack[top] != '(') {
printf("%c", stack[top--]);
}
top--;
} else if (instackPriority(stack[top])>incomingPriority(infix[i])) {
while (instackPriority(stack[top])>incomingPriority(infix[i]) && top>-1) {
printf("%c", stack[top--]);
}
stack[++top] = infix[i];
} else {
stack[++top] = infix[i];
}
}
while (top > -1) {
printf("%c", stack[top--]);
}
}