Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added stack representation in Infix Prefix Postfix Calculator #1922

Merged
merged 2 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Calculators/Infix-Prefix-Postfix-Calculator/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ <h2>Infix Prefix Postfix Calculator</h2>
<div class="result" id="result">
<!-- Result will be displayed here -->
</div>
<div class="steps" id="steps"></div>

</div>
</div>
Expand Down
163 changes: 151 additions & 12 deletions Calculators/Infix-Prefix-Postfix-Calculator/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ function getPrecedence(operator) {
return 0;
}

let steps = document.getElementById('steps');

function infixToPostfix(infixExpression) {
let result = '';
let stack = [];

// Define a function to get precedence of operators
function getPrecedence(operator) {
if (operator === '+' || operator === '-') {
return 1;
Expand All @@ -25,6 +26,23 @@ function infixToPostfix(infixExpression) {
return 0;
}

// Table structure
steps.innerHTML = `
<table border="1" style="width: 100%; text-align: center; border-collapse: collapse;">
<thead>
<tr>
<th>Character</th>
<th>Stack</th>
<th>Result</th>
</tr>
</thead>
<tbody id="table-body">
</tbody>
</table>
`;

let tableBody = document.getElementById('table-body');

for (let i = 0; i < infixExpression.length; i++) {
let char = infixExpression[i];

Expand All @@ -35,40 +53,85 @@ function infixToPostfix(infixExpression) {
} else if (char === ')') {
while (stack.length > 0 && stack[stack.length - 1] !== '(') {
result += stack.pop();
appendRow(char, stack, result);
}
stack.pop(); // Pop '('
stack.pop();
} else if (['+', '-', '*', '/'].includes(char)) {
while (
stack.length > 0 &&
getPrecedence(stack[stack.length - 1]) >= getPrecedence(char)
) {
result += stack.pop();
appendRow(char, stack, result);
}
stack.push(char);
}

// Append a row to the table for each step
appendRow(char, stack, result);
}

// Pop remaining operators in the stack and append rows
while (stack.length > 0) {
result += stack.pop();
appendRow('-', stack, result);
}
document.getElementById('result').innerText = `Result: ${result}`;

return result;

// Helper function to append rows
function appendRow(character, stack, result) {
tableBody.innerHTML += `
<tr>
<td>${character}</td>
<td>${stack.join(', ')}</td>
<td>${result}</td>
</tr>
`;
}
}

function postfixToInfix(postfixExpression) {
let stack = [];
let result = '';

steps.innerHTML += `
<table border="1" style="width: 100%; text-align: center; border-collapse: collapse;">
<thead>
<tr>
<th>Character</th>
<th>Stack</th>
<th>Result</th>
</tr>
</thead>
<tbody id="table-body">
</tbody>
</table>
`;
for (let i = 0; i < postfixExpression.length; i++) {
let char = postfixExpression[i];

if (/[a-zA-Z0-9]/.test(char)) {
stack.push(char);
result = char;
} else if (['+', '-', '*', '/'].includes(char)) {
let operand2 = stack.pop();
let operand1 = stack.pop();
stack.push(`(${operand1}${char}${operand2})`);
let expression = (`(${operand1}${char}${operand2})`);
stack.push(expression);
result = expression;
}
let tableBody = document.getElementById('table-body');

tableBody.innerHTML += `
<tr>
<td>${char}</td>
<td>${stack.join(', ')}</td>
<td>${result}</td>
</tr>
`;
}
document.getElementById('result').innerText = `Result: ${result}`;

return stack.pop();
}
Expand All @@ -77,35 +140,88 @@ function prefixToPostfix(prefixExpression) {
let stack = [];
let result = '';

steps.innerHTML += `
<table border="1" style="width: 100%; text-align: center; border-collapse: collapse;">
<thead>
<tr>
<th>Character</th>
<th>Stack</th>
<th>Result</th>
</tr>
</thead>
<tbody id="table-body">
</tbody>
</table>
`;
for (let i = prefixExpression.length - 1; i >= 0; i--) {
let char = prefixExpression[i];

if (/[a-zA-Z0-9]/.test(char)) {
stack.push(char);
result = char;
} else if (['+', '-', '*', '/'].includes(char)) {
let operand1 = stack.pop();
let operand2 = stack.pop();
stack.push(operand1 + operand2 + char);
let exp = (operand1 + operand2 + char);
result = exp;
}
let tableBody = document.getElementById('table-body');

tableBody.innerHTML += `
<tr>
<td>${char}</td>
<td>${stack.join(', ')}</td>
<td>${result}</td>
</tr>
`;
}
document.getElementById('result').innerText = `Result: ${result}`;

return stack.pop();
}


function postfixToPrefix(postfixExpression) {
let stack = [];
let result = '';

steps.innerHTML += `
<table border="1" style="width: 100%; text-align: center; border-collapse: collapse;">
<thead>
<tr>
<th>Character</th>
<th>Stack</th>
<th>Result</th>
</tr>
</thead>
<tbody id="table-body">
</tbody>
</table>
`;

for (let i = 0; i < postfixExpression.length; i++) {
let char = postfixExpression[i];

if (/[a-zA-Z0-9]/.test(char)) {
stack.push(char);
result = char;
} else if (['+', '-', '*', '/'].includes(char)) {
let operand2 = stack.pop();
let operand1 = stack.pop();
stack.push(char + operand1 + operand2);
let exp = (char + operand1 + operand2);
result = exp;
}
let tableBody = document.getElementById('table-body');

tableBody.innerHTML += `
<tr>
<td>${char}</td>
<td>${stack.join(', ')}</td>
<td>${result}</td>
</tr>
`;
}
document.getElementById('result').innerText = `Result: ${result}`;

return stack.pop();
}
Expand All @@ -119,23 +235,48 @@ function infixToPrefix(infixExpression) {

function prefixToInfix(prefixExpression) {
let stack = [];
let result = '';

steps.innerHTML += `
<table border="1" style="width: 100%; text-align: center; border-collapse: collapse;">
<thead>
<tr>
<th>Character</th>
<th>Stack</th>
<th>Result</th>
</tr>
</thead>
<tbody id="table-body">
</tbody>
</table>
`;

for (let i = prefixExpression.length - 1; i >= 0; i--) {
let char = prefixExpression[i];

if (/[a-zA-Z0-9]/.test(char)) {
stack.push(char);
result = char;
} else if (['+', '-', '*', '/'].includes(char)) {
let operand1 = stack.pop();
let operand2 = stack.pop();
stack.push(`(${operand1}${char}${operand2})`);
let exp = (`(${operand1}${char}${operand2})`);
result = exp;
}
}
let tableBody = document.getElementById('table-body');

tableBody.innerHTML += `
<tr>
<td>${char}</td>
<td>${stack.join(', ')}</td>
<td>${result}</td>
</tr>
`;
}
document.getElementById('result').innerText = `Result: ${result}`;
return stack.pop();
}


function convertExpression() {
let expression = document.getElementById('expression').value;
let conversionType = document.getElementById('conversionType').value;
Expand Down Expand Up @@ -170,11 +311,9 @@ function convertExpression() {
alert('Invalid conversion type selected.');
return;
}

document.getElementById('result').innerText = `Result: ${result}`;
}

function clearInput() {
document.getElementById('expression').value = '';
document.getElementById('result').innerText = '';
}
}
3 changes: 2 additions & 1 deletion Calculators/Infix-Prefix-Postfix-Calculator/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ body {
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
min-height: 100vh;
}

.sup {
Expand All @@ -22,6 +22,7 @@ body {
justify-content: space-evenly;
background-color: #fff;
padding: 20px;
margin: 25px 0;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
}
Expand Down
Loading