Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,88 +1,264 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
<head>
|
| 4 |
+
<meta charset="UTF-8">
|
| 5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 6 |
+
<title>Simple Calculator</title>
|
| 7 |
+
<!-- Tailwind CSS CDN for styling -->
|
| 8 |
+
<script src="https://cdn.tailwindcss.com"></script>
|
| 9 |
+
<style>
|
| 10 |
+
/* Custom styles for the calculator */
|
| 11 |
+
body {
|
| 12 |
+
font-family: 'Inter', sans-serif; /* Using Inter font as per instructions */
|
| 13 |
+
background-color: #f0f2f5; /* Light gray background */
|
| 14 |
+
display: flex;
|
| 15 |
+
justify-content: center;
|
| 16 |
+
align-items: center;
|
| 17 |
+
min-height: 100vh;
|
| 18 |
+
margin: 0;
|
| 19 |
+
padding: 20px; /* Add some padding for smaller screens */
|
| 20 |
+
box-sizing: border-box;
|
| 21 |
+
}
|
| 22 |
+
.calculator-grid {
|
| 23 |
+
display: grid;
|
| 24 |
+
grid-template-columns: repeat(4, 1fr); /* 4 columns for buttons */
|
| 25 |
+
gap: 10px; /* Space between buttons */
|
| 26 |
+
}
|
| 27 |
+
.calculator-button {
|
| 28 |
+
padding: 20px;
|
| 29 |
+
font-size: 1.5rem;
|
| 30 |
+
border: none;
|
| 31 |
+
cursor: pointer;
|
| 32 |
+
transition: background-color 0.2s ease-in-out, transform 0.1s ease-in-out;
|
| 33 |
+
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1); /* Subtle shadow */
|
| 34 |
+
user-select: none; /* Prevent text selection on buttons */
|
| 35 |
+
}
|
| 36 |
+
.calculator-button:hover {
|
| 37 |
+
transform: translateY(-2px); /* Slight lift on hover */
|
| 38 |
+
}
|
| 39 |
+
.calculator-button:active {
|
| 40 |
+
transform: translateY(0); /* Return to normal on click */
|
| 41 |
+
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); /* Smaller shadow on click */
|
| 42 |
+
}
|
| 43 |
+
.span-two {
|
| 44 |
+
grid-column: span 2; /* Make button span two columns */
|
| 45 |
+
}
|
| 46 |
+
.output {
|
| 47 |
+
grid-column: span 4; /* Output display spans all columns */
|
| 48 |
+
background-color: #222; /* Dark background for output */
|
| 49 |
+
color: #fff; /* White text */
|
| 50 |
+
padding: 20px;
|
| 51 |
+
text-align: right;
|
| 52 |
+
font-size: 2.5rem;
|
| 53 |
+
overflow: hidden; /* Hide overflow text */
|
| 54 |
+
white-space: nowrap; /* Prevent wrapping */
|
| 55 |
+
text-overflow: ellipsis; /* Add ellipsis for overflow */
|
| 56 |
+
min-height: 80px; /* Ensure minimum height */
|
| 57 |
+
display: flex;
|
| 58 |
+
align-items: flex-end; /* Align text to bottom */
|
| 59 |
+
justify-content: flex-end; /* Align text to right */
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
/* Responsive adjustments for smaller screens */
|
| 63 |
+
@media (max-width: 640px) {
|
| 64 |
+
.calculator-container {
|
| 65 |
+
width: 100%; /* Full width on small screens */
|
| 66 |
+
max-width: 350px; /* Limit max width even on full width */
|
| 67 |
+
}
|
| 68 |
+
.calculator-button {
|
| 69 |
+
padding: 15px;
|
| 70 |
+
font-size: 1.2rem;
|
| 71 |
+
}
|
| 72 |
+
.output {
|
| 73 |
+
font-size: 2rem;
|
| 74 |
+
min-height: 60px;
|
| 75 |
+
}
|
| 76 |
+
}
|
| 77 |
+
</style>
|
| 78 |
+
</head>
|
| 79 |
+
<body class="bg-gradient-to-br from-blue-100 to-purple-200">
|
| 80 |
+
<!-- Calculator Container -->
|
| 81 |
+
<div class="calculator-container bg-white p-6 rounded-2xl shadow-xl max-w-sm w-full border border-gray-200">
|
| 82 |
+
<!-- Calculator Display Output -->
|
| 83 |
+
<div id="output" class="output rounded-t-xl mb-4 shadow-inner">0</div>
|
| 84 |
+
|
| 85 |
+
<!-- Calculator Buttons Grid -->
|
| 86 |
+
<div class="calculator-grid">
|
| 87 |
+
<!-- Clear and Delete Buttons -->
|
| 88 |
+
<button class="calculator-button bg-red-500 text-white rounded-xl span-two hover:bg-red-600" onclick="clearDisplay()">AC</button>
|
| 89 |
+
<button class="calculator-button bg-yellow-500 text-white rounded-xl hover:bg-yellow-600" onclick="deleteLast()">DEL</button>
|
| 90 |
+
<button class="calculator-button bg-blue-500 text-white rounded-xl hover:bg-blue-600" onclick="appendOperator('/')">/</button>
|
| 91 |
+
|
| 92 |
+
<!-- Number Buttons (7-9) and Multiplication -->
|
| 93 |
+
<button class="calculator-button bg-gray-200 text-gray-800 rounded-xl hover:bg-gray-300" onclick="appendNumber('7')">7</button>
|
| 94 |
+
<button class="calculator-button bg-gray-200 text-gray-800 rounded-xl hover:bg-gray-300" onclick="appendNumber('8')">8</button>
|
| 95 |
+
<button class="calculator-button bg-gray-200 text-gray-800 rounded-xl hover:bg-gray-300" onclick="appendNumber('9')">9</button>
|
| 96 |
+
<button class="calculator-button bg-blue-500 text-white rounded-xl hover:bg-blue-600" onclick="appendOperator('*')">*</button>
|
| 97 |
+
|
| 98 |
+
<!-- Number Buttons (4-6) and Subtraction -->
|
| 99 |
+
<button class="calculator-button bg-gray-200 text-gray-800 rounded-xl hover:bg-gray-300" onclick="appendNumber('4')">4</button>
|
| 100 |
+
<button class="calculator-button bg-gray-200 text-gray-800 rounded-xl hover:bg-gray-300" onclick="appendNumber('5')">5</button>
|
| 101 |
+
<button class="calculator-button bg-gray-200 text-gray-800 rounded-xl hover:bg-gray-300" onclick="appendNumber('6')">6</button>
|
| 102 |
+
<button class="calculator-button bg-blue-500 text-white rounded-xl hover:bg-blue-600" onclick="appendOperator('-')">-</button>
|
| 103 |
+
|
| 104 |
+
<!-- Number Buttons (1-3) and Addition -->
|
| 105 |
+
<button class="calculator-button bg-gray-200 text-gray-800 rounded-xl hover:bg-gray-300" onclick="appendNumber('1')">1</button>
|
| 106 |
+
<button class="calculator-button bg-gray-200 text-gray-800 rounded-xl hover:bg-gray-300" onclick="appendNumber('2')">2</button>
|
| 107 |
+
<button class="calculator-button bg-gray-200 text-gray-800 rounded-xl hover:bg-gray-300" onclick="appendNumber('3')">3</button>
|
| 108 |
+
<button class="calculator-button bg-blue-500 text-white rounded-xl hover:bg-blue-600" onclick="appendOperator('+')">+</button>
|
| 109 |
+
|
| 110 |
+
<!-- Number Button (0), Decimal, and Equals -->
|
| 111 |
+
<button class="calculator-button bg-gray-200 text-gray-800 rounded-xl span-two hover:bg-gray-300" onclick="appendNumber('0')">0</button>
|
| 112 |
+
<button class="calculator-button bg-gray-200 text-gray-800 rounded-xl hover:bg-gray-300" onclick="appendNumber('.')">.</button>
|
| 113 |
+
<button class="calculator-button bg-green-500 text-white rounded-xl hover:bg-green-600" onclick="calculateResult()">=</button>
|
| 114 |
+
</div>
|
| 115 |
+
</div>
|
| 116 |
+
|
| 117 |
+
<script>
|
| 118 |
+
let currentInput = '0'; // Stores the current number or expression
|
| 119 |
+
let operator = null; // Stores the last operator clicked
|
| 120 |
+
let previousInput = ''; // Stores the previous number for calculations
|
| 121 |
+
let shouldResetDisplay = false; // Flag to clear display after an operation
|
| 122 |
+
|
| 123 |
+
const outputElement = document.getElementById('output');
|
| 124 |
+
|
| 125 |
+
/**
|
| 126 |
+
* Updates the calculator display.
|
| 127 |
+
* Ensures '0' is shown if input is empty.
|
| 128 |
+
*/
|
| 129 |
+
function updateDisplay() {
|
| 130 |
+
outputElement.innerText = currentInput === '' ? '0' : currentInput;
|
| 131 |
+
}
|
| 132 |
+
|
| 133 |
+
/**
|
| 134 |
+
* Appends a number to the current input.
|
| 135 |
+
* Handles leading zeros and decimal points.
|
| 136 |
+
* @param {string} number - The number string to append.
|
| 137 |
+
*/
|
| 138 |
+
function appendNumber(number) {
|
| 139 |
+
if (shouldResetDisplay) {
|
| 140 |
+
currentInput = number;
|
| 141 |
+
shouldResetDisplay = false;
|
| 142 |
+
} else {
|
| 143 |
+
// Prevent multiple leading zeros (e.g., 007 -> 7)
|
| 144 |
+
if (currentInput === '0' && number !== '.') {
|
| 145 |
+
currentInput = number;
|
| 146 |
+
} else if (number === '.' && currentInput.includes('.')) {
|
| 147 |
+
// Prevent multiple decimal points
|
| 148 |
+
return;
|
| 149 |
+
} else {
|
| 150 |
+
currentInput += number;
|
| 151 |
+
}
|
| 152 |
+
}
|
| 153 |
+
updateDisplay();
|
| 154 |
+
}
|
| 155 |
+
|
| 156 |
+
/**
|
| 157 |
+
* Appends an operator to the current input.
|
| 158 |
+
* If an operator already exists, it calculates the intermediate result.
|
| 159 |
+
* @param {string} nextOperator - The operator string to append.
|
| 160 |
+
*/
|
| 161 |
+
function appendOperator(nextOperator) {
|
| 162 |
+
if (currentInput === '0' && previousInput === '') {
|
| 163 |
+
// Allow starting with a negative sign
|
| 164 |
+
if (nextOperator === '-') {
|
| 165 |
+
currentInput = '-';
|
| 166 |
+
}
|
| 167 |
+
return;
|
| 168 |
+
}
|
| 169 |
+
|
| 170 |
+
if (operator !== null && !shouldResetDisplay) {
|
| 171 |
+
// If there's a pending operation and we're not starting a new number
|
| 172 |
+
calculateResult(); // Calculate intermediate result
|
| 173 |
+
}
|
| 174 |
+
|
| 175 |
+
operator = nextOperator;
|
| 176 |
+
previousInput = currentInput;
|
| 177 |
+
shouldResetDisplay = true; // Next number input will clear the display
|
| 178 |
+
updateDisplay(); // Display the previous input (before operator)
|
| 179 |
+
}
|
| 180 |
+
|
| 181 |
+
/**
|
| 182 |
+
* Clears the entire calculator display and resets all states.
|
| 183 |
+
*/
|
| 184 |
+
function clearDisplay() {
|
| 185 |
+
currentInput = '0';
|
| 186 |
+
operator = null;
|
| 187 |
+
previousInput = '';
|
| 188 |
+
shouldResetDisplay = false;
|
| 189 |
+
updateDisplay();
|
| 190 |
+
}
|
| 191 |
+
|
| 192 |
+
/**
|
| 193 |
+
* Deletes the last character from the current input.
|
| 194 |
+
* If input becomes empty, sets it to '0'.
|
| 195 |
+
*/
|
| 196 |
+
function deleteLast() {
|
| 197 |
+
if (currentInput === '0' || currentInput === '') return;
|
| 198 |
+
currentInput = currentInput.slice(0, -1);
|
| 199 |
+
if (currentInput === '') {
|
| 200 |
+
currentInput = '0';
|
| 201 |
+
}
|
| 202 |
+
updateDisplay();
|
| 203 |
+
}
|
| 204 |
+
|
| 205 |
+
/**
|
| 206 |
+
* Calculates the final result of the expression.
|
| 207 |
+
* Handles basic arithmetic operations.
|
| 208 |
+
*/
|
| 209 |
+
function calculateResult() {
|
| 210 |
+
if (operator === null || shouldResetDisplay) return; // No pending operation
|
| 211 |
+
|
| 212 |
+
const prev = parseFloat(previousInput);
|
| 213 |
+
const current = parseFloat(currentInput);
|
| 214 |
+
|
| 215 |
+
if (isNaN(prev) || isNaN(current)) {
|
| 216 |
+
currentInput = 'Error';
|
| 217 |
+
updateDisplay();
|
| 218 |
+
shouldResetDisplay = true;
|
| 219 |
+
operator = null;
|
| 220 |
+
previousInput = '';
|
| 221 |
+
return;
|
| 222 |
+
}
|
| 223 |
+
|
| 224 |
+
let result;
|
| 225 |
+
switch (operator) {
|
| 226 |
+
case '+':
|
| 227 |
+
result = prev + current;
|
| 228 |
+
break;
|
| 229 |
+
case '-':
|
| 230 |
+
result = prev - current;
|
| 231 |
+
break;
|
| 232 |
+
case '*':
|
| 233 |
+
result = prev * current;
|
| 234 |
+
break;
|
| 235 |
+
case '/':
|
| 236 |
+
if (current === 0) {
|
| 237 |
+
currentInput = 'Error: Div by 0';
|
| 238 |
+
updateDisplay();
|
| 239 |
+
shouldResetDisplay = true;
|
| 240 |
+
operator = null;
|
| 241 |
+
previousInput = '';
|
| 242 |
+
return;
|
| 243 |
+
}
|
| 244 |
+
result = prev / current;
|
| 245 |
+
break;
|
| 246 |
+
default:
|
| 247 |
+
return;
|
| 248 |
+
}
|
| 249 |
+
|
| 250 |
+
// Handle floating point precision issues (optional, but good for calculators)
|
| 251 |
+
result = parseFloat(result.toFixed(10)); // Limit to 10 decimal places
|
| 252 |
+
|
| 253 |
+
currentInput = result.toString();
|
| 254 |
+
operator = null;
|
| 255 |
+
previousInput = '';
|
| 256 |
+
shouldResetDisplay = true; // Next number input will clear the display
|
| 257 |
+
updateDisplay();
|
| 258 |
+
}
|
| 259 |
+
|
| 260 |
+
// Initialize display on load
|
| 261 |
+
window.onload = updateDisplay;
|
| 262 |
+
</script>
|
| 263 |
+
</body>
|
| 264 |
+
</html>
|