-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInfixToPostfix.cpp
205 lines (182 loc) · 4.47 KB
/
InfixToPostfix.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
196
197
198
199
200
201
202
203
204
205
// This program will help to convert a infix to postfix expression
#include <iostream>
#include <string>
#include <vector>
#include <array>
using namespace std;
struct Operators
{
char symbol;
int precedence;
};
struct Node
{
char data;
Node *next;
int m_precedence;
};
class Stack
{
private:
Node *top; // Pointer to the top of the stack
public:
Stack() : top(nullptr) {}
~Stack()
{
while (!isEmpty())
{
pop();
}
}
void push(char value, int precedence = 0) // Time Complexity is O(1)
{
if (isFull())
{
cout << "Stack Overflow..." << endl;
return;
}
Node *newNode = new Node();
newNode->data = value;
newNode->m_precedence = precedence;
newNode->next = top;
top = newNode;
}
void pop() // Time Complexity is O(1)
{
if (isEmpty())
{
return;
}
Node *temp = top;
top = top->next;
delete temp;
}
char peek(int index)// Time Complexity is O(n)
{
if (index < 0)
{
cout << "Invalid index. Index must be non-negative.\n";
return '\0'; // Return null character for invalid index
}
Node *current = top;
int count = 0;
while (current != nullptr)
{
if (count == index)
{
return current->data;
}
current = current->next;
count++;
}
}
int getPrecedence()
{
return isEmpty() ? -1 : top->m_precedence;
}
bool isEmpty()
{
return top == nullptr;
}
bool isFull()
{
Node *temp = new (nothrow) Node(); // Try to allocate memory
if (temp == nullptr)
{
return true;
}
delete temp; // Free the temporary node
return false;
}
char getChar()
{
return isEmpty() ? '\0' : top->data;
}
};
bool parenthesisMatch(Stack *s, string_view equation)// Time Complexity is O(n)
{
for (char ch : equation)
{
if (ch == '(' || ch == '[' || ch == '{')
{
s->push(ch);
}
else if (ch == ')' || ch == ']' || ch == '}')
{
if (s->isEmpty() ||
(ch == ')' && s->getChar() != '(') ||
(ch == ']' && s->getChar() != '[') ||
(ch == '}' && s->getChar() != '{'))
{
return false;
}
s->pop();
}
}
return s->isEmpty();
}
bool checkExpression(string_view s)
{
string valid = "-+/&^* 1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ{}[]()";
return s.find_first_not_of(valid) != string::npos;
}
void InfixToPostfix(string &exp)// Time Complexity is O(n)
{
// you can increase this as you like. Add more operators if you want to for more advance expression
array<Operators, 5> arr = {
Operators{'+', 1},
Operators{'-', 1},
Operators{'/', 2},
Operators{'*', 2},
Operators{'^', 3}};
Stack operators;
for (char ch : exp)
{
bool isOperator = false;
for (const auto &op : arr)
{
if (ch == op.symbol)
{
while (!operators.isEmpty() && operators.getPrecedence() >= op.precedence)
{
cout << operators.getChar();
operators.pop();
}
operators.push(ch, op.precedence);
isOperator = true;
break;
}
}
if (!isOperator)
{
cout << ch;
}
}
while (!operators.isEmpty())
{
cout << operators.getChar();
operators.pop();
}
cout << endl;
}
int main()
{
Stack paren;
string equation;
do
{
cout << "Enter an expression: ";
getline(cin >> ws, equation);
bool invalid = checkExpression(equation) || !parenthesisMatch(&paren, equation);
if (invalid)
{
cout << "Invalid. The entered expression isn't a valid mathematical expression.\n";
}
else
{
break;
}
} while (true);
InfixToPostfix(equation);
return 0;
}