mirror of
https://github.com/milk-net/milk-net.github.io.git
synced 2025-04-20 14:13:42 -05:00
158 lines
6.1 KiB
HTML
158 lines
6.1 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>MilkNet | Calculator</title>
|
|
<style>
|
|
@import url('https://fonts.googleapis.com/css2?family=DM+Sans:wght@400;500;700&display=swap');
|
|
body {
|
|
font-family: 'DM Sans', sans-serif;
|
|
display: flex;
|
|
justify-content: center;
|
|
align-items: center;
|
|
height: 100vh;
|
|
background-color: #000;
|
|
}
|
|
.calculator {
|
|
background: #111;
|
|
color: #fff;
|
|
padding: 20px;
|
|
border-radius: 15px;
|
|
box-shadow: 0px 4px 15px rgba(255, 255, 255, 0.2);
|
|
text-align: center;
|
|
}
|
|
input {
|
|
width: 100%;
|
|
height: 60px;
|
|
text-align: center;
|
|
font-size: 2em;
|
|
margin-bottom: 15px;
|
|
background: #222;
|
|
color: #fff;
|
|
border: none;
|
|
padding: 10px;
|
|
border-radius: 8px;
|
|
}
|
|
.buttons {
|
|
display: grid;
|
|
grid-template-columns: repeat(5, 1fr);
|
|
gap: 10px;
|
|
}
|
|
button {
|
|
padding: 20px;
|
|
font-size: 1.3em;
|
|
font-weight: 500;
|
|
border: none;
|
|
background: #333;
|
|
color: #fff;
|
|
cursor: pointer;
|
|
border-radius: 8px;
|
|
transition: background 0.3s ease;
|
|
}
|
|
button:hover {
|
|
background: #555;
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div class="calculator">
|
|
<input type="text" id="display" disabled>
|
|
<div class="buttons">
|
|
<button onclick="clearDisplay()">C</button>
|
|
<button onclick="appendToDisplay('(')">(</button>
|
|
<button onclick="appendToDisplay(')')">)</button>
|
|
<button onclick="appendToDisplay(' / ')">/</button>
|
|
<button onclick="appendToDisplay(' * ')">*</button>
|
|
<button onclick="appendToDisplay('7')">7</button>
|
|
<button onclick="appendToDisplay('8')">8</button>
|
|
<button onclick="appendToDisplay('9')">9</button>
|
|
<button onclick="appendToDisplay(' - ')">-</button>
|
|
<button onclick="appendToDisplay(' + ')">+</button>
|
|
<button onclick="appendToDisplay('4')">4</button>
|
|
<button onclick="appendToDisplay('5')">5</button>
|
|
<button onclick="appendToDisplay('6')">6</button>
|
|
<button onclick="appendToDisplay('%')">%</button>
|
|
<button onclick="appendToDisplay('1')">1</button>
|
|
<button onclick="appendToDisplay('2')">2</button>
|
|
<button onclick="appendToDisplay('3')">3</button>
|
|
<button onclick="appendToDisplay('0')">0</button>
|
|
<button onclick="appendToDisplay('.')">.</button>
|
|
<button onclick="appendToDisplay('sin(')">sin</button>
|
|
<button onclick="appendToDisplay('cos(')">cos</button>
|
|
<button onclick="appendToDisplay('tan(')">tan</button>
|
|
<button onclick="appendToDisplay('log(')">log</button>
|
|
<button onclick="appendToDisplay('√(')">√</button>
|
|
<button onclick="appendToDisplay('^')">x^y</button>
|
|
<button onclick="appendToDisplay('π')">π</button>
|
|
<button onclick="appendToDisplay('e')">e</button>
|
|
<button onclick="appendToDisplay('!')">x!</button>
|
|
<button onclick="calculateResult()">=</button>
|
|
</div>
|
|
</div>
|
|
<script>
|
|
function appendToDisplay(value) {
|
|
// Add spaces around operators
|
|
if (['+', '-', '*', '/', '%', '^'].includes(value)) {
|
|
// Check if there's already a space before or after the operator
|
|
let display = document.getElementById('display').value;
|
|
if (display && display.slice(-1) !== ' ' && !['+', '-', '*', '/', '%', '^'].includes(display.slice(-1))) {
|
|
document.getElementById('display').value += ' ' + value + ' ';
|
|
} else if (display === '') {
|
|
document.getElementById('display').value += ' ' + value + ' ';
|
|
} else {
|
|
document.getElementById('display').value += value;
|
|
}
|
|
} else {
|
|
document.getElementById('display').value += value;
|
|
}
|
|
}
|
|
function clearDisplay() {
|
|
document.getElementById('display').value = '';
|
|
}
|
|
function calculateResult() {
|
|
try {
|
|
let expression = document.getElementById('display').value;
|
|
expression = expression.replace(/factorial\((\d+)\)/g, (match, num) => factorial(parseInt(num)));
|
|
document.getElementById('display').value = eval(expression);
|
|
} catch (e) {
|
|
alert('Invalid Expression');
|
|
}
|
|
}
|
|
function factorial(n) {
|
|
if (n === 0 || n === 1) return 1;
|
|
let result = 1;
|
|
for (let i = 2; i <= n; i++) {
|
|
result *= i;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
// Add event listener for keyboard input
|
|
document.addEventListener('keydown', function(event) {
|
|
const key = event.key;
|
|
|
|
// Handle digit keys and operators
|
|
if ((key >= '0' && key <= '9') || key === '.' || key === '+' || key === '-' || key === '*' || key === '/' || key === '%' || key === '^') {
|
|
appendToDisplay(key);
|
|
}
|
|
// Handle special characters and functions
|
|
else if (key === '(') {
|
|
appendToDisplay('(');
|
|
} else if (key === ')') {
|
|
appendToDisplay(')');
|
|
} else if (key === 'Enter') {
|
|
calculateResult();
|
|
} else if (key === 'Backspace') {
|
|
backspace();
|
|
}
|
|
});
|
|
|
|
// Function for handling Backspace
|
|
function backspace() {
|
|
let display = document.getElementById('display').value;
|
|
document.getElementById('display').value = display.slice(0, -1);
|
|
}
|
|
</script>
|
|
</body>
|
|
</html>
|