Skip to content

Commit

Permalink
Added Reverse Polish Notation Calculator (#899)
Browse files Browse the repository at this point in the history
  • Loading branch information
animex007 authored May 27, 2024
1 parent 0246912 commit 9539c6c
Show file tree
Hide file tree
Showing 5 changed files with 183 additions and 0 deletions.
15 changes: 15 additions & 0 deletions Calculators/RPN-Calculator/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# <p align="center">RPN Calculator</p>

## Description :-

RPN(Reverse Polish Notation) Calculator represents expressions by placing the operator symbol after the operands.

## Tech Stacks :-

- HTML
- CSS
- JavaScript

## Screenshots :-

![image](https://github.com/Rakesh9100/CalcDiverse/assets/73993775/18f3c287-2e10-4b2a-a714-5f5faf03dc7b)
18 changes: 18 additions & 0 deletions Calculators/RPN-Calculator/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<title>RPN Calculator</title>
</head>
<body>
<div class="container">
<h1>RPN Calculator</h1>
<input type="text" id="expression" placeholder="Enter RPN expression">
<button onclick="calculateRPN()">Calculate</button>
<div class="result" id="result"></div>
</div>
<script src="script.js"></script>
</body>
</html>
49 changes: 49 additions & 0 deletions Calculators/RPN-Calculator/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
function evaluateRPN(expression) {
const stack = [];
const operators = {
'+': (a, b) => a + b,
'-': (a, b) => a - b,
'*': (a, b) => a * b,
'/': (a, b) => {
if (b === 0) {
throw new Error("Division by zero");
}
return a / b;
}
};

expression.split(' ').forEach(token => {
if (operators[token]) {
const b = stack.pop();
const a = stack.pop();
if (a === undefined || b === undefined) {
throw new Error("Invalid expression: insufficient operands");
}
stack.push(operators[token](a, b));
} else {
const number = parseFloat(token);
if (isNaN(number)) {
throw new Error(`Invalid token: ${token}`);
}
stack.push(number);
}
});

if (stack.length !== 1) {
throw new Error("Invalid expression: too many operands");
}

return stack.pop();
}

function calculateRPN() {
const expression = document.getElementById('expression').value;
const resultDisplay = document.getElementById('result');

try {
const result = evaluateRPN(expression);
resultDisplay.textContent = `Result: ${result}`;
} catch (e) {
resultDisplay.textContent = `Error: ${e.message}`;
}
}
87 changes: 87 additions & 0 deletions Calculators/RPN-Calculator/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
body {
font-family: Arial, sans-serif;
background: #262626; /* Adjust background color for better contrast */
margin: 0;
padding: 50px;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}

.container {
max-width: 500px;
width: 100%;
background: #00ffa0; /* Neo Brutalism background */
padding: 1rem;
border-radius: 1rem;
border: 0.5vmin solid #05060f;
box-shadow: 0.4rem 0.4rem #05060f;
overflow: hidden;
color: black;
text-align: center;
}

input, button {
padding: 10px;
margin: 5px 0; /* Adjust margin to vertical only */
font-size: 16px;
width: calc(100% - 24px); /* Ensure input and button take full width minus padding */
box-sizing: border-box; /* Include padding in width calculation */
}

button {
background-color: #05060f; /* Button color adjustment */
color: #00ffa0;
border: none;
border-radius: 0.5rem;
cursor: pointer;
}

button:hover {
background-color: #000; /* Darker hover color */
}

.result {
margin-top: 20px;
font-size: 20px;
}

/* Additional styles for card content */
.pricing-block-content {
display: flex;
flex-direction: column;
gap: 0.5rem;
}

.pricing-plan {
color: #05060f;
font-size: 1.3rem;
line-height: 1.25;
font-weight: 700;
}

.price-value {
display: flex;
color: #05060f;
font-size: 1.8rem;
line-height: 1.25;
font-weight: 700;
}

.pricing-note {
opacity: 0.8;
}

.check-list {
display: flex;
flex-direction: column;
gap: 0.5rem;
margin-top: 0.5rem;
}

.check-list-item {
display: flex;
align-items: center;
gap: 4px;
}
14 changes: 14 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -1728,6 +1728,20 @@ <h3>Calculates the Quotient and Remainder.</h3>
</div>
</div>
</div>
<div class="box">
<div class="content">
<h2>RPN Calculator</h2>
<h3>Reverse Polish Notation calculator which represents expressions by placeing the operator after the operands.</h3>
<div class="card-footer">
<a href="./Calculators/RPN-Calculator/index.html" target="_blank">
<button>Try Now</button>
</a>
<a href="https://github.com/Rakesh9100/CalcDiverse/tree/main/Calculators/RPN-Calculator" title="Source Code" target="_blank">
<img src="./assets/images/github.png" alt="Source Code"></img>
</a>
</div>
</div>
</div>
<div class="box">
<div class="content">
<h2>Ratio Calculator</h2>
Expand Down

0 comments on commit 9539c6c

Please sign in to comment.