forked from rathoresrikant/HacktoberFestContribute
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinfix2postfix.c
110 lines (104 loc) · 2.16 KB
/
infix2postfix.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/* This Program converts infix expression to postfix expression */
#include <stdio.h>
#include <string.h>
#define MAX_SIZE 100
// Top of the stack
int top = -1;
// Array representing stack
char stack[MAX_SIZE];
// Checks whether stack is empty or not
int isStackEmpty(void){
return (top < 0) ? 1 : 0;
}
// Checks whether stack is full or not
int isStackFull(void){
return (top == MAX_SIZE) ? 1 : 0;
}
// Push elements in the stack
void pushStack(char item){
if(isStackFull()){
printf("Stack Overflow!\n");
return;
}
top++;
stack[top] = item;
}
// Pop elements in the stack
char popStack(void){
if(isStackEmpty()){
printf("Stack Underflow!\n");
return -1;
}
char item = stack[top];
top--;
return item;
}
// Function to input the infix expression
void scanExp(char *str){
char ch,i;
for(i=0; (ch=getchar()) != '\n'; i++){
str[i] = ch;
}
str[i] = '\0';
}
// Function to check whether the character is an operator
int isOperator(char ch){
switch(ch){
case '^':
case '/':
case '*':
case '+':
case '-':
return 1;
default:
return 0;
}
}
// Function converts the infix expression to postfix expression
void infix2postfix(char *infix, char *postfix){
// Operator Predecence
int operators[256];
operators['^'] = 0;
operators['*'] = 1;
operators['/'] = 1;
operators['+'] = 2;
operators['-'] = 2;
infix[strlen(infix)] = ')';
int i, j;
pushStack('(');
for(i=0,j=0;!isStackEmpty();){
if(!isOperator(infix[i]) && infix[i] != '(' && infix[i] != ')'){
postfix[j]=infix[i];
i++;
j++;
}else if(infix[i] == '('){
pushStack(infix[i]);
i++;
}else if(isOperator(infix[i])){
while(isOperator(stack[top]) && operators[stack[top]] <= operators[infix[i]]){
char operator = popStack();
postfix[j]=operator;
j++;
}
pushStack(infix[i]);
i++;
}else if(infix[i] == ')'){
char item;
while((item=popStack()) != '('){
postfix[j] = item;
j++;
}
i++;
}
}
postfix[j] = '\0';
}
int main(void){
char infix[100], postfix[100];
printf("Enter the infix expression : ");
scanExp(infix);
printf("Infix Expression : %s\n", infix);
infix2postfix(infix, postfix);
printf("Postfix Expression : %s\n", postfix);
return 0;
}