File size: 12,521 Bytes
9cf9981
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<!DOCTYPE html>
<html lang="fr" class="h-full">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Calculatrice Rapide</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <script src="https://kit.fontawesome.com/a076d05399.js" crossorigin="anonymous"></script>
    <style>
        .btn-press {
            transform: scale(0.95);
            transition: transform 0.1s;
        }
        .display-text {
            font-size: clamp(1.5rem, 5vw, 2.5rem);
        }
        @media (max-width: 640px) {
            .calculator {
                width: 95% !important;
                height: 80vh !important;
            }
        }
    </style>
</head>
<body class="h-full flex items-center justify-center bg-gray-100 dark:bg-gray-900 transition-colors duration-300">
    <div class="calculator w-96 h-[32rem] bg-white dark:bg-gray-800 rounded-3xl shadow-2xl overflow-hidden flex flex-col transition-colors duration-300">
        <!-- Header -->
        <div class="p-4 flex justify-between items-center">
            <div class="text-sm text-gray-500 dark:text-gray-400">Calculatrice</div>
            <button id="theme-toggle" class="w-8 h-8 rounded-full flex items-center justify-center bg-gray-200 dark:bg-gray-700 text-gray-700 dark:text-gray-300">
                <i class="fas fa-moon dark:hidden"></i>
                <i class="fas fa-sun hidden dark:block"></i>
            </button>
        </div>
        
        <!-- Display -->
        <div class="flex-1 p-4 flex flex-col justify-end items-end">
            <div id="history" class="text-gray-500 dark:text-gray-400 text-right text-sm h-6"></div>
            <div id="display" class="display-text font-semibold text-gray-800 dark:text-white text-right break-all">0</div>
        </div>
        
        <!-- Keypad -->
        <div class="p-4 grid grid-cols-4 gap-3 flex-1">
            <!-- Row 1 -->
            <button onclick="clearAll()" class="btn bg-gray-200 dark:bg-gray-700 text-gray-800 dark:text-white rounded-xl h-16 flex items-center justify-center font-medium text-lg">AC</button>
            <button onclick="toggleSign()" class="btn bg-gray-200 dark:bg-gray-700 text-gray-800 dark:text-white rounded-xl h-16 flex items-center justify-center font-medium text-lg">+/-</button>
            <button onclick="percentage()" class="btn bg-gray-200 dark:bg-gray-700 text-gray-800 dark:text-white rounded-xl h-16 flex items-center justify-center font-medium text-lg">%</button>
            <button onclick="operation('/')" class="btn operator bg-blue-500 text-white rounded-xl h-16 flex items-center justify-center font-medium text-2xl">÷</button>
            
            <!-- Row 2 -->
            <button onclick="appendNumber('7')" class="btn number bg-gray-100 dark:bg-gray-600 text-gray-800 dark:text-white rounded-xl h-16 flex items-center justify-center font-medium text-lg">7</button>
            <button onclick="appendNumber('8')" class="btn number bg-gray-100 dark:bg-gray-600 text-gray-800 dark:text-white rounded-xl h-16 flex items-center justify-center font-medium text-lg">8</button>
            <button onclick="appendNumber('9')" class="btn number bg-gray-100 dark:bg-gray-600 text-gray-800 dark:text-white rounded-xl h-16 flex items-center justify-center font-medium text-lg">9</button>
            <button onclick="operation('*')" class="btn operator bg-blue-500 text-white rounded-xl h-16 flex items-center justify-center font-medium text-2xl">×</button>
            
            <!-- Row 3 -->
            <button onclick="appendNumber('4')" class="btn number bg-gray-100 dark:bg-gray-600 text-gray-800 dark:text-white rounded-xl h-16 flex items-center justify-center font-medium text-lg">4</button>
            <button onclick="appendNumber('5')" class="btn number bg-gray-100 dark:bg-gray-600 text-gray-800 dark:text-white rounded-xl h-16 flex items-center justify-center font-medium text-lg">5</button>
            <button onclick="appendNumber('6')" class="btn number bg-gray-100 dark:bg-gray-600 text-gray-800 dark:text-white rounded-xl h-16 flex items-center justify-center font-medium text-lg">6</button>
            <button onclick="operation('-')" class="btn operator bg-blue-500 text-white rounded-xl h-16 flex items-center justify-center font-medium text-2xl">-</button>
            
            <!-- Row 4 -->
            <button onclick="appendNumber('1')" class="btn number bg-gray-100 dark:bg-gray-600 text-gray-800 dark:text-white rounded-xl h-16 flex items-center justify-center font-medium text-lg">1</button>
            <button onclick="appendNumber('2')" class="btn number bg-gray-100 dark:bg-gray-600 text-gray-800 dark:text-white rounded-xl h-16 flex items-center justify-center font-medium text-lg">2</button>
            <button onclick="appendNumber('3')" class="btn number bg-gray-100 dark:bg-gray-600 text-gray-800 dark:text-white rounded-xl h-16 flex items-center justify-center font-medium text-lg">3</button>
            <button onclick="operation('+')" class="btn operator bg-blue-500 text-white rounded-xl h-16 flex items-center justify-center font-medium text-2xl">+</button>
            
            <!-- Row 5 -->
            <button onclick="appendNumber('0')" class="btn number bg-gray-100 dark:bg-gray-600 text-gray-800 dark:text-white rounded-xl h-16 flex items-center justify-center font-medium text-lg col-span-2">0</button>
            <button onclick="appendDecimal()" class="btn number bg-gray-100 dark:bg-gray-600 text-gray-800 dark:text-white rounded-xl h-16 flex items-center justify-center font-medium text-lg">.</button>
            <button onclick="calculate()" class="btn operator bg-blue-500 text-white rounded-xl h-16 flex items-center justify-center font-medium text-2xl">=</button>
        </div>
    </div>

    <script>
        // Variables d'état
        let currentInput = '0';
        let previousInput = '';
        let operation = null;
        let resetInput = false;
        
        // Éléments du DOM
        const display = document.getElementById('display');
        const history = document.getElementById('history');
        const buttons = document.querySelectorAll('.btn');
        
        // Fonctions de la calculatrice
        function updateDisplay() {
            display.textContent = currentInput;
            history.textContent = previousInput + (operation ? ' ' + operation : '');
        }
        
        function appendNumber(number) {
            if (currentInput === '0' || resetInput) {
                currentInput = number;
                resetInput = false;
            } else {
                currentInput += number;
            }
            updateDisplay();
        }
        
        function appendDecimal() {
            if (resetInput) {
                currentInput = '0.';
                resetInput = false;
            } else if (!currentInput.includes('.')) {
                currentInput += '.';
            }
            updateDisplay();
        }
        
        function toggleSign() {
            currentInput = (parseFloat(currentInput) * -1).toString();
            updateDisplay();
        }
        
        function percentage() {
            currentInput = (parseFloat(currentInput) / 100).toString();
            updateDisplay();
        }
        
        function clearAll() {
            currentInput = '0';
            previousInput = '';
            operation = null;
            updateDisplay();
        }
        
        function operation(op) {
            if (operation && !resetInput) {
                calculate();
            }
            operation = op;
            previousInput = currentInput;
            resetInput = true;
            updateDisplay();
        }
        
        function calculate() {
            if (!operation) return;
            
            let result;
            const prev = parseFloat(previousInput);
            const current = parseFloat(currentInput);
            
            switch (operation) {
                case '+':
                    result = prev + current;
                    break;
                case '-':
                    result = prev - current;
                    break;
                case '*':
                    result = prev * current;
                    break;
                case '/':
                    result = prev / current;
                    break;
                default:
                    return;
            }
            
            currentInput = result.toString();
            operation = null;
            previousInput = '';
            resetInput = true;
            updateDisplay();
        }
        
        // Animation des boutons
        buttons.forEach(button => {
            button.addEventListener('mousedown', () => {
                button.classList.add('btn-press');
            });
            
            button.addEventListener('mouseup', () => {
                button.classList.remove('btn-press');
            });
            
            button.addEventListener('mouseleave', () => {
                button.classList.remove('btn-press');
            });
        });
        
        // Gestion du thème sombre/clair
        const themeToggle = document.getElementById('theme-toggle');
        const prefersDarkScheme = window.matchMedia('(prefers-color-scheme: dark)');
        
        function setTheme(isDark) {
            document.documentElement.classList.toggle('dark', isDark);
            localStorage.setItem('theme', isDark ? 'dark' : 'light');
        }
        
        // Vérifier le thème stocké ou les préférences système
        const storedTheme = localStorage.getItem('theme');
        if (storedTheme) {
            setTheme(storedTheme === 'dark');
        } else {
            setTheme(prefersDarkScheme.matches);
        }
        
        // Basculer le thème manuellement
        themeToggle.addEventListener('click', () => {
            const isDark = document.documentElement.classList.contains('dark');
            setTheme(!isDark);
        });
        
        // Écouter les changements de préférences système
        prefersDarkScheme.addEventListener('change', e => {
            if (!localStorage.getItem('theme')) {
                setTheme(e.matches);
            }
        });
        
        // Gestion du clavier
        document.addEventListener('keydown', (e) => {
            if (/[0-9]/.test(e.key)) {
                appendNumber(e.key);
            } else if (e.key === '.') {
                appendDecimal();
            } else if (e.key === 'Enter' || e.key === '=') {
                calculate();
            } else if (e.key === 'Escape') {
                clearAll();
            } else if (e.key === '+' || e.key === '-' || e.key === '*' || e.key === '/') {
                operation(e.key);
            } else if (e.key === '%') {
                percentage();
            } else if (e.key === '_') {
                toggleSign();
            }
            
            // Trouver et animer le bouton correspondant
            const keyToButton = {
                '1': '1', '2': '2', '3': '3', '4': '4', '5': '5',
                '6': '6', '7': '7', '8': '8', '9': '9', '0': '0',
                '.': '.', '+': '+', '-': '-', '*': '*', '/': '/',
                'Enter': '=', '=': '=', 'Escape': 'AC', '%': '%',
                '_': '+/-'
            };
            
            if (keyToButton[e.key]) {
                const button = document.querySelector(`.btn:contains("${keyToButton[e.key]}")`);
                if (button) {
                    button.classList.add('btn-press');
                    setTimeout(() => button.classList.remove('btn-press'), 100);
                }
            }
        });
    </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=KINGFAUS/cal" style="color: #fff;text-decoration: underline;" target="_blank" >Remix</a></p></body>
</html>