-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathinfixtoprefix.cpp
195 lines (174 loc) · 4.51 KB
/
infixtoprefix.cpp
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include <bits/stdc++.h>
using namespace std;
//definition of functions
struct Stack *create (int max);
int stackFull (struct Stack *stack);
int stackEmpty (struct Stack *stack);
void pushElement (struct Stack *stack, int item);
int popElement (struct Stack *stack);
int peekElement (struct Stack *stack);
int checkOperand (char ch);
int precedence (char ch);
int postfix (char *expression);
void reverse (char *exp);
void brackets (char *exp);
void conversionInfixToPrefix (char *exp);
// A structure to represent a stack
struct Stack
{
int top;
int maxSize;
int *array;
};
int main ()
{
int n = 10;
cout << "The infix expression is: \n";
char expression[] = "(P+(Q*R)/(S-T))";
cout << expression << "\n";
conversionInfixToPrefix (expression);
cout << "The prefix expression is: \n";
cout << expression;
return 0;
}
//stack implementation
struct Stack * create (int max)
{
struct Stack *stack = (struct Stack *) malloc (sizeof (struct Stack));
stack->maxSize = max;
stack->top = -1;
stack->array = (int *) malloc (stack->maxSize * sizeof (int));
return stack;
}
// Checking with this function is stack is full or not
int stackFull (struct Stack *stack)
{
if (stack->top == stack->maxSize - 1)
{
cout << "Will not be able to push maxSize reached\n";
}
// We know array index from 0 and maxSize starts from 1
return stack->top == stack->maxSize - 1;
}
// if Stack is empty when top is equal to -1 and return true
int stackEmpty (struct Stack *stack)
{
return stack->top == -1;
}
// Push function it inserts value in stack and increments stack top by 1
void pushElement (struct Stack *stack, int item)
{
if (stackFull (stack))
return;
stack->array[++stack->top] = item;
}
// pop Function it remove an item from stack and decreases top by 1
int popElement (struct Stack *stack)
{
if (stackEmpty (stack))
return INT_MIN;
return stack->array[stack->top--];
}
// Function to return the top from stack without removing it
int peekElement (struct Stack *stack)
{
if (stackEmpty (stack))
return INT_MIN;
return stack->array[stack->top];
}
// A function check the given character is operand
int checkOperand (char ch)
{
return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z');
}
// Fucntion to compare precedence if return larger value means higher precedence
int precedence (char ch)
{
switch (ch)
{
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
case '^':
return 3;
}
return -1;
}
// The function for infix to postfix conversion
int postfix (char *expression)
{
int i, j;
struct Stack *stack = create (strlen (expression));
if (!stack)
return -1;
for (i = 0, j = -1; expression[i]; ++i)
{
// checking the character we scanned is operand or not
if (checkOperand (expression[i]))
expression[++j] = expression[i];
// if we scan character push it to the stack
else if (expression[i] == '(')
pushElement (stack, expression[i]);
//if we scan character we need to pop and print from the stack
else if (expression[i] == ')')
{
while (!stackEmpty (stack) && peekElement (stack) != '(')
expression[++j] = popElement (stack);
if (!stackEmpty (stack) && peekElement (stack) != '(')
return -1; // invalid expression
else
popElement (stack);
}
else // if an operator
{
while (!stackEmpty (stack)
&& precedence (expression[i]) <=
precedence (peekElement (stack)))
expression[++j] = popElement (stack);
pushElement (stack, expression[i]);
}
}
// if all first expression characters are scanned
// adding all left elements from stack to expression
while (!stackEmpty (stack))
expression[++j] = popElement (stack);
expression[++j] = '\0';
return 0;
}
void reverse (char *exp)
{ //reverse function for expression
int size = strlen (exp);
int j = size, i = 0;
char temp[size];
temp[j--] = '\0';
while (exp[i] != '\0')
{
temp[j] = exp[i];
j--;
i++;
}
strcpy (exp, temp);
}
void brackets (char *exp)
{
int i = 0;
while (exp[i] != '\0')
{
if (exp[i] == '(')
exp[i] = ')';
else if (exp[i] == ')')
exp[i] = '(';
i++;
}
}
void conversionInfixToPrefix (char *exp)
{
int size = strlen (exp);
reverse (exp);
brackets (exp);
postfix (exp);
reverse (exp);
}