Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add files via upload
  • Loading branch information
ngmurphy committed Apr 25, 2024
0 parents commit 627a9b1
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 0 deletions.
34 changes: 34 additions & 0 deletions Lab11/index.html
@@ -0,0 +1,34 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>Nathan's Calculator</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="calculator">
<div class="display"><input type="text" id="display" disabled></div>
<div class="buttons">
<button onclick="clearDisplay()">C</button>
<button onclick="pressButton('/')">/</button>
<button onclick="pressButton('%')">%</button>
<button onclick="pressButton('√')"></button>
<button onclick="pressButton('7')">7</button>
<button onclick="pressButton('8')">8</button>
<button onclick="pressButton('9')">9</button>
<button onclick="pressButton('*')">*</button>
<button onclick="pressButton('4')">4</button>
<button onclick="pressButton('5')">5</button>
<button onclick="pressButton('6')">6</button>
<button onclick="pressButton('-')">-</button>
<button onclick="pressButton('1')">1</button>
<button onclick="pressButton('2')">2</button>
<button onclick="pressButton('3')">3</button>
<button onclick="pressButton('+')">+</button>
<button onclick="pressButton('0')">0</button>
<button onclick="pressButton('.')">.</button>
<button onclick="calculateResult()">=</button>
</div>
</div>
<script src="script.js"></script>
</body>
</html>
68 changes: 68 additions & 0 deletions Lab11/script.js
@@ -0,0 +1,68 @@
var currentInput = '';

function pressButton(buttonValue) {
var displayElement = document.getElementById('display');

if (['+', '-', '*', '/'].indexOf(buttonValue) !== -1)
{
if (currentInput !== '' && ['+', '-', '*', '/'].indexOf(currentInput.slice(-1)) !== -1)
{
currentInput = currentInput.slice(0, -1) + buttonValue;
}
else
{
currentInput += buttonValue;
}
} else if (buttonValue === '.' && (currentInput === '' || isNaN(currentInput.slice(-1))))
{
currentInput += '0.';
}
else if (buttonValue === '.' && currentInput.includes('.') && !currentInput.slice(currentInput.lastIndexOf('.')).includes('+') && !currentInput.slice(currentInput.lastIndexOf('.')).includes('-') && !currentInput.slice(currentInput.lastIndexOf('.')).includes('*') && !currentInput.slice(currentInput.lastIndexOf('.')).includes('/'))
{
return;
}
else
{
currentInput += buttonValue;
}
displayElement.value = currentInput;
}

function clearDisplay()
{
currentInput = '';
document.getElementById('display').value = '';
}

function calculateResult() {
var displayElement = document.getElementById('display');
if (['+', '-', '*', '/'].indexOf(currentInput.slice(-1)) !== -1)
{
currentInput = currentInput.slice(0, -1);
}

try
{
currentInput = currentInput.replace(//g, 'Math.sqrt').replace(/%/g, '/100');
var result = eval(currentInput);
if (result > 9999999999)
{
throw new Error('Overflow');
}
else if(result < -9999999999)
{
throw new Error('Underflow');
}
if (result.toString().indexOf('.') !== -1 && result.toString().split('.')[0].length > 10)
{
throw new Error('Overflow');
}
displayElement.value = result;
currentInput = result.toString();
}
catch (error)
{
displayElement.value = error.message;
currentInput = '';
}
}
38 changes: 38 additions & 0 deletions Lab11/style.css
@@ -0,0 +1,38 @@
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #f0f0f0;
}

.calculator {
width: 300px;
border: 1px solid #888;
background-color: #fff;
}

.display {
background-color: #e0e0e0;
padding: 10px;
}

.display input {
width: 100%;
border: none;
background: none;
text-align: right;
font-size: 2em;
}

.buttons button {
width: 20%;
padding: 20px;
border: 1px solid #888;
font-size: 1.5em;
background-color: #f9f9f9;
}

.buttons button:hover {
background-color: #ddd;
}

0 comments on commit 627a9b1

Please sign in to comment.