-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInfix_to_postfix_conversion_of_an_expression.c
170 lines (158 loc) · 4.4 KB
/
Infix_to_postfix_conversion_of_an_expression.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// Program to Convert an Infix notation expression to Postfix notation expression.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
struct node
{
char term;
struct node *next;
};
struct llstack
{
int count;
struct node *top;
};
struct node* createNode(char ch)
{
struct node *np;
np = (struct node*)malloc(sizeof(struct node));
np->next = NULL;
np->term = ch;
return(np);
}
struct llstack* createEmptyStack(void)
{
struct llstack *stack;
stack = (struct llstack*)malloc(sizeof(struct llstack));
stack->top = NULL;
stack->count = 0;
return stack;
}
void push(struct llstack *stack, char ch)
{
struct node *np;
np = createNode(ch);
np->next = stack->top;
stack->top = np;
stack->count++;
}
char peek(struct llstack *stack)
{
if( stack->top != NULL )
{
char ch;
ch = (stack->top)->term;
return ch;
}
}
char pop(struct llstack *stack)
{
if( stack->top != NULL )
{
char ch;
struct node *np;
ch = (stack->top)->term;
np = stack->top;
stack->top = (stack->top)->next;
free(np);
stack->count--;
return ch;
}
}
int priority(char operator)
{
if( operator=='+' || operator=='-' )
return(1);
else if( operator=='*' || operator=='/' || operator=='%' )
return(2);
else if( operator=='^' )
return(3);
else
return(-1);
}
int belong( char *list, char ch )
{
int i=0;
while( list[i] != '\0' )
{
if( list[i] == ch )
return(1);
i++;
}
return(0);
}
void StringConcat( char *str, char ch )
{
int i=0;
while( str[i] != '\0' )
i++;
str[i] = ch;
str[i+1] = '\0';
}
//char operands[] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','\0'};
//char operators[] = {'+','-','/','*','%','^','\0'};
int main(void)
{
int exp_len, i=0;
char *infix_exp, *postfix_exp;
struct llstack *stack;
char operands[] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','\0'};
char operators[] = {'+','-','/','*','%','^','\0'};
//---------------------------------------------------------------------------------------------
stack = createEmptyStack();
//---------------------------------------------------------------------------------------------
printf("Enter the length(no of char) of the infix expression that you want to input: ");
scanf("%d",&exp_len);
infix_exp = (char*)calloc( exp_len+5 , sizeof(char) );
printf("Type your Infix Expression, to get it converted to Postfix Expression(for brackets, use only parenthesis in input):\n");
fflush(stdin);
gets(infix_exp);
//---------------------------------------------------------------------------------------------
push(stack, '(');
StringConcat(infix_exp, ')');
exp_len = strlen( infix_exp );
postfix_exp = (char*)calloc( exp_len , sizeof(infix_exp) );
postfix_exp[0] = '\0';
//---------------------------------------------------------------------------------------------
while( stack->top != NULL )
{
if( infix_exp[i] == '(' )
{
push(stack,'(');
}
else if( belong( operands , infix_exp[i]) )
{
StringConcat( postfix_exp , infix_exp[i] );
}
else if( belong(operators, infix_exp[i]) )
{
while( belong( operators , peek(stack) ) )
{
if( priority( peek(stack) ) >= priority( infix_exp[i] ) )
{
StringConcat( postfix_exp , pop(stack) );
}
else
{
break;
}
}
push( stack , infix_exp[i] );
}
else if( infix_exp[i] == ')' )
{
while( peek(stack) != '(' )
{
StringConcat( postfix_exp , pop(stack) );
}
pop(stack);
}
i++;
}
printf("\n\nPostfix Conversion of the above expression is : \n\n");
puts(postfix_exp);
printf("\n");
getch();
return(0);
}