File size: 14,706 Bytes
6968d2c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Modern Calculator</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        .calculator {
            box-shadow: 0 20px 50px rgba(0, 0, 0, 0.2);
            transition: all 0.3s ease;
        }
        .calculator:hover {
            box-shadow: 0 20px 50px rgba(0, 0, 0, 0.3);
        }
        .btn {
            transition: all 0.2s ease;
            transform: scale(1);
        }
        .btn:active {
            transform: scale(0.95);
        }
        .display {
            min-height: 100px;
            word-wrap: break-word;
            word-break: break-all;
        }
        .theme-toggle {
            transition: all 0.3s ease;
        }
    </style>
</head>
<body class="bg-gray-100 min-h-screen flex items-center justify-center p-4">
    <div class="calculator bg-white rounded-3xl overflow-hidden w-full max-w-md">
        <!-- Header with theme toggle -->
        <div class="flex justify-between items-center p-4 bg-gradient-to-r from-blue-500 to-purple-600">
            <h1 class="text-white font-bold text-xl">CalcPro</h1>
            <button id="themeToggle" class="theme-toggle bg-white bg-opacity-20 rounded-full p-2 text-white">
                <i class="fas fa-moon"></i>
            </button>
        </div>
        
        <!-- Display -->
        <div class="display p-4 bg-gray-50 text-right">
            <div id="previousOperand" class="text-gray-500 text-lg h-6"></div>
            <div id="currentOperand" class="text-3xl font-semibold"></div>
        </div>
        
        <!-- Keypad -->
        <div class="grid grid-cols-4 gap-2 p-4 bg-white">
            <!-- Row 1 -->
            <button class="btn bg-gray-200 hover:bg-gray-300 rounded-lg p-4 text-xl font-medium" data-action="clear">AC</button>
            <button class="btn bg-gray-200 hover:bg-gray-300 rounded-lg p-4 text-xl font-medium" data-action="delete">
                <i class="fas fa-backspace"></i>
            </button>
            <button class="btn bg-gray-200 hover:bg-gray-300 rounded-lg p-4 text-xl font-medium" data-action="percentage">%</button>
            <button class="btn bg-blue-500 hover:bg-blue-600 text-white rounded-lg p-4 text-xl font-medium" data-action="divide">÷</button>
            
            <!-- Row 2 -->
            <button class="btn bg-gray-100 hover:bg-gray-200 rounded-lg p-4 text-xl font-medium" data-number="7">7</button>
            <button class="btn bg-gray-100 hover:bg-gray-200 rounded-lg p-4 text-xl font-medium" data-number="8">8</button>
            <button class="btn bg-gray-100 hover:bg-gray-200 rounded-lg p-4 text-xl font-medium" data-number="9">9</button>
            <button class="btn bg-blue-500 hover:bg-blue-600 text-white rounded-lg p-4 text-xl font-medium" data-action="multiply">×</button>
            
            <!-- Row 3 -->
            <button class="btn bg-gray-100 hover:bg-gray-200 rounded-lg p-4 text-xl font-medium" data-number="4">4</button>
            <button class="btn bg-gray-100 hover:bg-gray-200 rounded-lg p-4 text-xl font-medium" data-number="5">5</button>
            <button class="btn bg-gray-100 hover:bg-gray-200 rounded-lg p-4 text-xl font-medium" data-number="6">6</button>
            <button class="btn bg-blue-500 hover:bg-blue-600 text-white rounded-lg p-4 text-xl font-medium" data-action="subtract">-</button>
            
            <!-- Row 4 -->
            <button class="btn bg-gray-100 hover:bg-gray-200 rounded-lg p-4 text-xl font-medium" data-number="1">1</button>
            <button class="btn bg-gray-100 hover:bg-gray-200 rounded-lg p-4 text-xl font-medium" data-number="2">2</button>
            <button class="btn bg-gray-100 hover:bg-gray-200 rounded-lg p-4 text-xl font-medium" data-number="3">3</button>
            <button class="btn bg-blue-500 hover:bg-blue-600 text-white rounded-lg p-4 text-xl font-medium" data-action="add">+</button>
            
            <!-- Row 5 -->
            <button class="btn bg-gray-100 hover:bg-gray-200 rounded-lg p-4 text-xl font-medium col-span-2" data-number="0">0</button>
            <button class="btn bg-gray-100 hover:bg-gray-200 rounded-lg p-4 text-xl font-medium" data-number=".">.</button>
            <button class="btn bg-purple-500 hover:bg-purple-600 text-white rounded-lg p-4 text-xl font-medium" data-action="equals">=</button>
        </div>
    </div>

    <script>
        document.addEventListener('DOMContentLoaded', () => {
            // Calculator logic
            class Calculator {
                constructor(previousOperandElement, currentOperandElement) {
                    this.previousOperandElement = previousOperandElement;
                    this.currentOperandElement = currentOperandElement;
                    this.clear();
                }

                clear() {
                    this.currentOperand = '0';
                    this.previousOperand = '';
                    this.operation = undefined;
                }

                delete() {
                    this.currentOperand = this.currentOperand.toString().slice(0, -1);
                    if (this.currentOperand === '') {
                        this.currentOperand = '0';
                    }
                }

                appendNumber(number) {
                    if (number === '.' && this.currentOperand.includes('.')) return;
                    if (this.currentOperand === '0' && number !== '.') {
                        this.currentOperand = number.toString();
                    } else {
                        this.currentOperand = this.currentOperand.toString() + number.toString();
                    }
                }

                chooseOperation(operation) {
                    if (this.currentOperand === '') return;
                    if (this.previousOperand !== '') {
                        this.compute();
                    }
                    this.operation = operation;
                    this.previousOperand = this.currentOperand;
                    this.currentOperand = '';
                }

                compute() {
                    let computation;
                    const prev = parseFloat(this.previousOperand);
                    const current = parseFloat(this.currentOperand);
                    if (isNaN(prev) || isNaN(current)) return;

                    switch (this.operation) {
                        case '+':
                            computation = prev + current;
                            break;
                        case '-':
                            computation = prev - current;
                            break;
                        case '×':
                            computation = prev * current;
                            break;
                        case '÷':
                            computation = prev / current;
                            break;
                        case '%':
                            computation = prev % current;
                            break;
                        default:
                            return;
                    }

                    this.currentOperand = computation.toString();
                    this.operation = undefined;
                    this.previousOperand = '';
                }

                getPercentage() {
                    this.currentOperand = (parseFloat(this.currentOperand) / 100).toString();
                }

                updateDisplay() {
                    this.currentOperandElement.innerText = this.currentOperand;
                    if (this.operation != null) {
                        this.previousOperandElement.innerText = 
                            `${this.previousOperand} ${this.operation}`;
                    } else {
                        this.previousOperandElement.innerText = '';
                    }
                }
            }

            // UI Elements
            const numberButtons = document.querySelectorAll('[data-number]');
            const operationButtons = document.querySelectorAll('[data-action]');
            const equalsButton = document.querySelector('[data-action="equals"]');
            const deleteButton = document.querySelector('[data-action="delete"]');
            const clearButton = document.querySelector('[data-action="clear"]');
            const percentageButton = document.querySelector('[data-action="percentage"]');
            const previousOperandElement = document.getElementById('previousOperand');
            const currentOperandElement = document.getElementById('currentOperand');
            const themeToggle = document.getElementById('themeToggle');

            const calculator = new Calculator(previousOperandElement, currentOperandElement);

            // Event Listeners
            numberButtons.forEach(button => {
                button.addEventListener('click', () => {
                    calculator.appendNumber(button.innerText);
                    calculator.updateDisplay();
                });
            });

            operationButtons.forEach(button => {
                button.addEventListener('click', () => {
                    const action = button.getAttribute('data-action');
                    
                    if (action === 'clear') {
                        calculator.clear();
                    } else if (action === 'delete') {
                        calculator.delete();
                    } else if (action === 'percentage') {
                        calculator.getPercentage();
                    } else if (action === 'equals') {
                        calculator.compute();
                    } else {
                        calculator.chooseOperation(button.innerText);
                    }
                    
                    calculator.updateDisplay();
                });
            });

            // Keyboard support
            document.addEventListener('keydown', (e) => {
                if (e.key >= 0 && e.key <= 9) {
                    calculator.appendNumber(e.key);
                    calculator.updateDisplay();
                } else if (e.key === '.') {
                    calculator.appendNumber(e.key);
                    calculator.updateDisplay();
                } else if (e.key === '+' || e.key === '-' || e.key === '*' || e.key === '/') {
                    calculator.chooseOperation(
                        e.key === '*' ? '×' : 
                        e.key === '/' ? '÷' : e.key
                    );
                    calculator.updateDisplay();
                } else if (e.key === '%') {
                    calculator.getPercentage();
                    calculator.updateDisplay();
                } else if (e.key === 'Enter' || e.key === '=') {
                    calculator.compute();
                    calculator.updateDisplay();
                } else if (e.key === 'Backspace') {
                    calculator.delete();
                    calculator.updateDisplay();
                } else if (e.key === 'Escape') {
                    calculator.clear();
                    calculator.updateDisplay();
                }
            });

            // Theme toggle
            let darkMode = false;
            themeToggle.addEventListener('click', () => {
                darkMode = !darkMode;
                if (darkMode) {
                    document.body.classList.add('bg-gray-900');
                    document.body.classList.remove('bg-gray-100');
                    document.querySelector('.calculator').classList.add('bg-gray-800');
                    document.querySelector('.calculator').classList.remove('bg-white');
                    document.querySelector('.display').classList.add('bg-gray-700', 'text-white');
                    document.querySelector('.display').classList.remove('bg-gray-50');
                    document.querySelectorAll('.btn').forEach(btn => {
                        if (!btn.classList.contains('bg-blue-500') && !btn.classList.contains('bg-purple-500')) {
                            btn.classList.add('bg-gray-600', 'text-white');
                            btn.classList.remove('bg-gray-100', 'bg-gray-200', 'bg-gray-300');
                        }
                    });
                    themeToggle.innerHTML = '<i class="fas fa-sun"></i>';
                } else {
                    document.body.classList.add('bg-gray-100');
                    document.body.classList.remove('bg-gray-900');
                    document.querySelector('.calculator').classList.add('bg-white');
                    document.querySelector('.calculator').classList.remove('bg-gray-800');
                    document.querySelector('.display').classList.add('bg-gray-50');
                    document.querySelector('.display').classList.remove('bg-gray-700', 'text-white');
                    document.querySelectorAll('.btn').forEach(btn => {
                        if (!btn.classList.contains('bg-blue-500') && !btn.classList.contains('bg-purple-500')) {
                            btn.classList.remove('bg-gray-600', 'text-white');
                            if (btn.getAttribute('data-action') === 'clear' || 
                                btn.getAttribute('data-action') === 'delete' || 
                                btn.getAttribute('data-action') === 'percentage') {
                                btn.classList.add('bg-gray-200');
                            } else {
                                btn.classList.add('bg-gray-100');
                            }
                        }
                    });
                    themeToggle.innerHTML = '<i class="fas fa-moon"></i>';
                }
            });
        });
    </script>
<p style="border-radius: 8px; text-align: center; font-size: 12px; color: #fff; margin-top: 16px;position: fixed; left: 8px; bottom: 8px; z-index: 10; background: rgba(0, 0, 0, 0.8); padding: 4px 8px;">Made with <img src="https://enzostvs-deepsite.hf.space/logo.svg" alt="DeepSite Logo" style="width: 16px; height: 16px; vertical-align: middle;display:inline-block;margin-right:3px;filter:brightness(0) invert(1);"><a href="https://enzostvs-deepsite.hf.space" style="color: #fff;text-decoration: underline;" target="_blank" >DeepSite</a> - 🧬 <a href="https://enzostvs-deepsite.hf.space?remix=sparkhousecreative/calculator" style="color: #fff;text-decoration: underline;" target="_blank" >Remix</a></p></body>
</html>