-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinfix_to_postfix.c
82 lines (79 loc) · 2 KB
/
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>
#define max 20
int stk[max], top = -1;
/* this program is best example to understand that any change in an array in any function in C is a global change.
There's no need to return changed array. When we pass array to a function, original array is passed not copy.*/
void push(char op){
if (top == max - 1)
printf("\nOverflow");
else
stk[++top] = op;
}
int pop(){
if (top == -1)
printf("\nUnderflow");
else
return stk[top--];
}
int priority(char op){
if (op == '+' || op == '-')
return 0;
else if (op == '/' || op == '%' || op == '*')
return 1;
}
void infix_to_postfix(char source[], char target[]){
int i = 0, j = 0; // for traversing through two strings
char temp;
while(source[i] != '\0'){
if (source[i] == '('){
push(source[i]);
i++;
}
else if (source[i] == ')'){
while(top != -1 && stk[top] != '('){ // until opening bracket is encountered
target[j] = pop();
j++;
}
if (top == -1){
printf("\nWrong Expression");
exit(1);
}
temp = pop();
i++;
}
else if (source[i] == '+' || source[i] == '/' || source[i] == '%' || source[i] == '-' || source[i] == '*'){
while(top != -1 && stk[top] != '(' && priority(stk[top]) >= priority(source[i])){ // operators higher or equal in priority
target[j] = pop();
j++;
}
push(source[i]);
i++;
}
else if (isalpha(source[i]) || isdigit(source[i])){
target[j] = source[i];
j++;
i++;
}
else{
printf("\nWrong Expression");
exit(1);
}
}
while(top != -1 && stk[top] != '('){ // for all the remaining operators in the stack
target[j] = pop();
j++;
}
}
int main(){
char infix[20], postfix[20];
strcpy(postfix, "");
printf("Enter the infix expression : ");
gets(infix);
infix_to_postfix(infix, postfix);
printf("\nThe postfix expression is : ");
puts(postfix);
return 0;
}