forked from raj457036/Simple-Tools
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprefixAndPostfixConvertor.html
More file actions
330 lines (300 loc) · 15.7 KB
/
Copy pathprefixAndPostfixConvertor.html
File metadata and controls
330 lines (300 loc) · 15.7 KB
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>Infix to Postfix or Infix to Prefix - Converter & Evaluator</title>
<meta name="keywords" content="Postfix, Prefix, Infix, Notation, Convertor, Evaluator">
<meta name="description"
content="A utilty for the conversion of infix to prefix or postfix notation with detailed steps." />
<script src="ds.js">
</script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"
crossorigin="anonymous">
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-9019718369052772"
crossorigin="anonymous"></script>
<style>
/* Custom CSS */
body {
font-family: Arial, sans-serif;
line-height: 1.6;
color: #333;
}
.navbar {
background-color: #f8f9fa;
box-shadow: 0 2px 4px rgba(0, 0, 0, .1);
}
.navbar-brand {
font-weight: bold;
color: #007bff !important;
}
.nav-link {
color: #333 !important;
}
.nav-link:hover {
color: #007bff !important;
}
.jumbotron {
background-color: #e9ecef;
padding: 2rem 1rem;
margin-bottom: 2rem;
border-radius: .3rem;
}
.footer {
background-color: #f8f9fa;
padding: 1rem 0;
margin-top: 2rem;
border-top: 1px solid #e9ecef;
}
</style>
</head>
<body>
<!-- Navbar -->
<nav class="navbar navbar-expand-lg navbar-light">
<div class="container">
<a class="navbar-brand" href="index.html">Simple Tools</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavDropdown"
aria-controls="navbarNavDropdown" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNavDropdown">
<ul class="navbar-nav ml-auto">
<li class="nav-item">
<a class="nav-link" href="index.html">Home</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" role="button"
data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Tools
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
<a class="dropdown-item" href="calculator.html">Calculator</a>
<a class="dropdown-item" href="prefixAndPostfixConvertor.html">Infix -> Postfix/Prefix</a>
<a class="dropdown-item" href="prefixAndPostfixEvaluator.html">Postfix/Prefix Evaluator</a>
<a class="dropdown-item" href="Clock.html">Beautiful Clock</a>
<a class="dropdown-item" href="RandomFPS.html">FPS Simulator</a>
</div>
</li>
<li class="nav-item">
<a class="nav-link" href="https://blog.springsfern.in">Blog</a>
</li>
<li class="nav-item">
<a class="nav-link" href="about.html">About Us</a>
</li>
<li class="nav-item">
<a class="nav-link" href="contact.html">Contact Us</a>
</li>
<li class="nav-item">
<a class="nav-link" href="privacy.html">Privacy Policy</a>
</li>
</ul>
</div>
</div>
</nav>
<div class="container mt-4">
<div class="jumbotron mt-3 text-center">
<div class="alert alert-warning alert">
<b>Note:</b> Do not use spaces in expression. A+B/C or 4+5/3 <br class="d-md-none">not A + B / C or
4 +
5 / 3
</div>
<h4><span class="badge badge-pill badge-success"> Updated (26 Feb 2023)</span></h4>
<h1 class="lead" style="font-size:10vmin">Infix -> Postfix & Prefix </h1>
<p class="lead">This is a simple infix to prefix or postfix Converter.</p>
<hr class="my-4">
<p>Enter the Infix expression below in box and press Convert</p>
<div>
<label for="expression">
<p>Type the Expression below without space<br><small>format 1: 2+4/5*(5-3)^5^4<br>format 2:
A+B/C*(D-A)^F^H <br> (NO SPACE)</small></p>
<input type="text" class="form-control form-control-lg" name="expression" id="expression">
<br>
<label for="fullpostfix">
<input type="radio" name="fixer" checked id="fullpostfix" value="1"> Postfix Table
</label>
<label for="fullprefix">
<input type="radio" name="fixer" id="fullprefix" value="2"> Prefix Table
</label>
<hr>
<button class="btn btn-primary btn-lg" id="convert" role="button">Convert</button> <a
class="btn btn-sm btn-warning" id="evaluate" href="./prefixAndPostfixEvaluator.html">Evaluate
></a>
</label>
</div>
<div id="shortresult">
<p class="lead">Postfix : <span id="postfix" class="font-weight-bold"></span> | Prefix : <span
id="prefix" class="font-weight-bold"></span></p>
<div class="text-center">
<small>By Raj</small>
</div>
</div>
</div>
<div id="fullresult">
<div class="row">
<div class="col-md-8">
<table class="table table-striped table-bordered">
<thead class="thead-inverse text-left">
<tr>
<th>Sr. no.</th>
<th>Expression</th>
<th>Stack</th>
<th id="itemname">Postfix</th>
</tr>
</thead>
<tbody id="tabledata" class="text-left">
</tbody>
<br>
<span id="ifpre" class="lead"></span>
</table>
</div>
<div class="col-md-4 text-center">
<h2>
Algorithm used
</h2>
<small id="algo">
<p class="lead">Postfix</p>
<ul class="text-left" id="postfixAlgo">
<li>Step 1: Add ")" to the end of the infix expression</li>
<li>Step 2: Push "(" onto the stack </li>
<li>Step 3: Repeat until each character in the infix notation is scanned
<ul>
<li>3.1: IF a "(" is encountered, push it on the stack </li>
<li>3.2: IF an operand ( whether a digit or a character) is encountered, add it
postfix
expression. </li>
<li>3.3: IF a ")" is encountered, then <br>
a. Repeatedly pop from stack and add it to the postfix expression until a
"(" is encountered. <br>
b. Discard the "(". That is, remove the(from stack and do not add it to the
postfix expression
</li>
<li>3.4: IF an operator O is encountered, then <br>
a. Repeatedly pop from stack and add each operator ( popped from the stack)
to the postfix expression which has equal or higher precedence
than O
<br>b. Push the operator O to the stack.
</li>
</ul>
[END OF IF]
</li>
<li>Step 4: Repeatedly pop from the stack and add it to the postfix expression until the
stack is empty </li>
<li>Step 5: EXIT </li>
</ul>
<p class="lead">Prefix</p>
<ul class="text-left" id="prefixAlgo">
<li>Step 1: Reverse the infix string. Note that while reversing the string you must
interchange left and right parentheses. </li>
<li>Step 2: Obtain the postfix expression of the expression obtained from Step 1
using the above given postfix algorithm with slight change in <strong>Step
3.4</strong><br />
<hr />
IF an operator O is encountered, then <br>
a. Repeatedly pop from stack and add each operator ( popped from the stack)
to the postfix expression which has <s>equal or</s> higher precedence
than O
<hr />
</li>
<li>Step 3: Reverse the postfix expression to get the prefix expression</li>
</ul>
</small>
</div>
</div>
</div>
<div>
<h2>Understanding the Infix to Postfix & Prefix Converter</h2>
<p>Our Infix to Postfix & Prefix Converter is a powerful tool designed to transform mathematical expressions
between different notations. This converter is particularly useful for students, programmers, and anyone
working with expression parsing or compiler design.</p>
<h3>What are Infix, Postfix, and Prefix Notations?</h3>
<ul>
<li><strong>Infix Notation:</strong> This is the standard way we write mathematical expressions, with
operators placed between operands. Example: A + B * C</li>
<li><strong>Postfix Notation (Reverse Polish Notation):</strong> In this notation, operators follow
their operands. Example: A B C * +</li>
<li><strong>Prefix Notation (Polish Notation):</strong> Here, operators precede their operands. Example:
+ A * B C</li>
</ul>
<h3>How to Use the Converter</h3>
<ol>
<li>Enter your infix expression in the input box. Remember not to use spaces between characters.</li>
<li>Valid formats include:
<ul>
<li>Numeric expressions: 2+4/5*(5-3)^5^4</li>
<li>Algebraic expressions: A+B/C*(D-A)^F^H</li>
</ul>
</li>
<li>Click the "Convert" button to see the postfix and prefix equivalents.</li>
<li>Review the conversion tables to understand the step-by-step process.</li>
</ol>
<h3>Features of Our Converter</h3>
<ul>
<li><strong>Dual Conversion:</strong> Provides both postfix and prefix notations simultaneously.</li>
<li><strong>Step-by-Step Breakdown:</strong> Displays the conversion process in detail, helping users
understand the algorithm.</li>
<li><strong>Support for Complex Expressions:</strong> Handles nested parentheses and multiple operators
with different precedences.</li>
<li><strong>Evaluation Option:</strong> Includes a link to evaluate the converted expressions.</li>
</ul>
<h3>Why Use Different Notations?</h3>
<p>While infix notation is more readable for humans, postfix and prefix notations have several advantages:
</p>
<ul>
<li>They eliminate the need for parentheses and operator precedence rules.</li>
<li>They are easier for computers to parse and evaluate.</li>
<li>They are used in many programming languages and calculators.</li>
</ul>
<h3>Applications</h3>
<p>This converter is valuable for:</p>
<ul>
<li>Students learning about expression parsing and notation conversion.</li>
<li>Programmers implementing expression evaluators or compilers.</li>
<li>Anyone looking to understand or work with different mathematical notations.</li>
</ul>
<p>By using this tool and studying the conversion process, you can gain a deeper understanding of how
mathematical expressions are structured and processed in different notations. This knowledge is
fundamental in various areas of computer science and mathematics.</p>
</div>
</div>
<!-- Bootstrap JS, Popper.js, and jQuery -->
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.5.3/dist/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
function sel(x) { return document.getElementById(x) }
$("#fullresult").hide();
function convert() {
const input = document.getElementById("expression").value.trim();
document.getElementById("postfix").innerHTML = infixToPostfix(input)['postfixExpression'];
document.getElementById("prefix").innerHTML = infixToPrefix(input)['prefixExpression'];
}
//converter button and expression
$("button#convert").click(function () {
convert();
var items, tab = "";
$("#fullresult").show();
const input = document.getElementById("expression").value.trim();
switch ($('input[name=fixer]:checked').val()) {
case '1':
items = infixToPostfix(input, 1)['table'];
sel("itemname").innerHTML = "Postfix";
$("#ifpre").html("Below is the table showing the conversion steps.");
break;
case '2':
items = infixToPrefix(input, 1)['table'];
sel('itemname').innerHTML = "Prefix ( Reversed )";
$("#ifpre").html("1. Reversed the Infix expression along with swithing ( with ) and vice versa.<br />2. <big><b>Find
the Postfix Expression as below table.</b></big><br />3. Reverse the resultant expression like in step 1.");
break;
}
for (var i = 0; i < items.exp.length; i++) { tab +="<tr><td>" + i + "</td><td>" + items['exp'][i] + "</td><td>" +
items['stack'][i] + "</td><td>" + items['conexp'][i] + "</td></tr>" ; } sel("tabledata").innerHTML=tab; })
//evaluator button and expression </script>
<!-- Footer -->
<footer class="footer">
<div class="container text-center">
<a href="privacy.html" class="text-muted">Privacy Policy</a>
</div>
</footer>
</body>
</html>