Skip to content

Instantly share code, notes, and snippets.

@Michaelgathara
Last active January 30, 2024 04:05
Show Gist options
  • Save Michaelgathara/cac0cce5859c681afc9a703e63d0761f to your computer and use it in GitHub Desktop.
Save Michaelgathara/cac0cce5859c681afc9a703e63d0761f to your computer and use it in GitHub Desktop.
Infix to Prefix notation convertor
<!DOCTYPE html>
<!-- THIS IS CODE DONE IN 20 MINUTES, NOT THE BEST LOOKING CODE OUT THERE -->
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Infix to Prefix Notation Converter</title>
<style>
@import url("https://fonts.googleapis.com/css2?family=Roboto&display=swap");
body,
html {
margin: 0 auto;
font-family: "Roboto", sans-serif;
font-weight: 400;
font-style: normal;
text-align: center;
}
</style>
</head>
<body>
<h1>Infix to Prefix</h1>
<p>
Parans will dissapear, but it does take them into account, ie: (1 + 2) * 3
vs 1 + 2 * 3
</p>
<label for="infixInput">Enter Infix Expression:</label>
<br /><br />
<input type="text" id="infixInput" placeholder="e.g., (1 + 2) * 3" />
<br /><br />
<button onclick="convertToPrefix()">Convert</button>
<p>Prefix Notation:</p>
<br /><br />
<p id="prefixOutput"></p>
<br /><br /><br /><br />
<p>
The code for this is here:
<a
href="https://gist.github.com/Michaelgathara/cac0cce5859c681afc9a703e63d0761f"
>https://gist.github.com/Michaelgathara/cac0cce5859c681afc9a703e63d0761f</a
>
</p>
<script>
function getPrecedence(op) {
if (op === "+" || op === "-") {
return 1;
}
if (op === "*" || op === "/") {
return 2;
}
return 0;
}
function infixToPrefix(infix) {
let opsStack = [];
let operandsStack = [];
for (let i = 0; i < infix.length; i++) {
let token = infix[i];
if (!isNaN(parseInt(token))) {
operandsStack.push(token);
}
else if (token === "(") {
opsStack.push(token);
}
else if (token === ")") {
while (
opsStack.length > 0 &&
opsStack[opsStack.length - 1] !== "("
) {
const operator = opsStack.pop();
const rightOperand = operandsStack.pop();
const leftOperand = operandsStack.pop();
const newExpr = operator + leftOperand + rightOperand;
operandsStack.push(newExpr);
}
opsStack.pop();
}
else if (getPrecedence(token) > 0) {
while (
opsStack.length > 0 &&
getPrecedence(token) <=
getPrecedence(opsStack[opsStack.length - 1])
) {
const operator = opsStack.pop();
const rightOperand = operandsStack.pop();
const leftOperand = operandsStack.pop();
const newExpr = operator + leftOperand + rightOperand;
operandsStack.push(newExpr);
}
opsStack.push(token);
}
}
while (opsStack.length > 0) {
const operator = opsStack.pop();
const rightOperand = operandsStack.pop();
const leftOperand = operandsStack.pop();
const newExpr = operator + leftOperand + rightOperand;
operandsStack.push(newExpr);
}
return operandsStack[operandsStack.length - 1];
}
function convertToPrefix() {
let infix = document
.getElementById("infixInput")
.value.replace(/\s+/g, "");
let prefix = infixToPrefix(infix);
document.getElementById("prefixOutput").textContent = prefix;
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment