hudzax commited on
Commit
e37bf64
·
verified ·
1 Parent(s): f574605

Upload folder using huggingface_hub

Browse files
Files changed (3) hide show
  1. assets/css/styles.css +220 -0
  2. assets/js/calculator.js +154 -0
  3. index.html +55 -19
assets/css/styles.css ADDED
@@ -0,0 +1,220 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ * {
2
+ margin: 0;
3
+ padding: 0;
4
+ box-sizing: border-box;
5
+ }
6
+
7
+ :root {
8
+ --primary-color: #2c3e50;
9
+ --secondary-color: #3498db;
10
+ --accent-color: #e74c3c;
11
+ --bg-color: #ecf0f1;
12
+ --text-color: #2c3e50;
13
+ --display-bg: #34495e;
14
+ --button-bg: #ffffff;
15
+ --button-hover: #f8f9fa;
16
+ --operator-bg: #3498db;
17
+ --operator-hover: #2980b9;
18
+ --equals-bg: #27ae60;
19
+ --equals-hover: #229954;
20
+ --clear-bg: #e74c3c;
21
+ --clear-hover: #c0392b;
22
+ }
23
+
24
+ body {
25
+ font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
26
+ background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
27
+ min-height: 100vh;
28
+ display: flex;
29
+ flex-direction: column;
30
+ color: var(--text-color);
31
+ }
32
+
33
+ header {
34
+ padding: 1rem;
35
+ text-align: center;
36
+ background: rgba(255, 255, 255, 0.1);
37
+ backdrop-filter: blur(10px);
38
+ }
39
+
40
+ .built-with {
41
+ color: white;
42
+ text-decoration: none;
43
+ font-size: 0.9rem;
44
+ font-weight: 500;
45
+ transition: opacity 0.3s ease;
46
+ }
47
+
48
+ .built-with:hover {
49
+ opacity: 0.8;
50
+ text-decoration: underline;
51
+ }
52
+
53
+ main {
54
+ flex: 1;
55
+ display: flex;
56
+ justify-content: center;
57
+ align-items: center;
58
+ padding: 2rem 1rem;
59
+ }
60
+
61
+ .calculator {
62
+ background: white;
63
+ border-radius: 20px;
64
+ box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
65
+ padding: 1.5rem;
66
+ max-width: 400px;
67
+ width: 100%;
68
+ }
69
+
70
+ .display {
71
+ background: var(--display-bg);
72
+ border-radius: 12px;
73
+ padding: 1.5rem;
74
+ margin-bottom: 1.5rem;
75
+ min-height: 100px;
76
+ display: flex;
77
+ flex-direction: column;
78
+ justify-content: flex-end;
79
+ align-items: flex-end;
80
+ word-wrap: break-word;
81
+ word-break: break-all;
82
+ }
83
+
84
+ .display-previous {
85
+ color: rgba(255, 255, 255, 0.6);
86
+ font-size: 1.2rem;
87
+ min-height: 1.5rem;
88
+ margin-bottom: 0.5rem;
89
+ }
90
+
91
+ .display-current {
92
+ color: white;
93
+ font-size: 2.5rem;
94
+ font-weight: 300;
95
+ min-height: 2.5rem;
96
+ }
97
+
98
+ .buttons {
99
+ display: grid;
100
+ grid-template-columns: repeat(4, 1fr);
101
+ gap: 0.75rem;
102
+ }
103
+
104
+ .btn {
105
+ border: none;
106
+ border-radius: 10px;
107
+ font-size: 1.5rem;
108
+ padding: 1.25rem;
109
+ cursor: pointer;
110
+ transition: all 0.2s ease;
111
+ font-weight: 500;
112
+ background: var(--button-bg);
113
+ color: var(--text-color);
114
+ box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
115
+ }
116
+
117
+ .btn:hover {
118
+ transform: translateY(-2px);
119
+ box-shadow: 0 4px 10px rgba(0, 0, 0, 0.2);
120
+ }
121
+
122
+ .btn:active {
123
+ transform: translateY(0);
124
+ box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
125
+ }
126
+
127
+ .btn-number:hover {
128
+ background: var(--button-hover);
129
+ }
130
+
131
+ .btn-operator {
132
+ background: var(--operator-bg);
133
+ color: white;
134
+ }
135
+
136
+ .btn-operator:hover {
137
+ background: var(--operator-hover);
138
+ }
139
+
140
+ .btn-clear {
141
+ background: var(--clear-bg);
142
+ color: white;
143
+ grid-column: span 2;
144
+ }
145
+
146
+ .btn-clear:hover {
147
+ background: var(--clear-hover);
148
+ }
149
+
150
+ .btn-delete {
151
+ background: var(--accent-color);
152
+ color: white;
153
+ }
154
+
155
+ .btn-delete:hover {
156
+ background: #c0392b;
157
+ }
158
+
159
+ .btn-equals {
160
+ background: var(--equals-bg);
161
+ color: white;
162
+ grid-row: span 2;
163
+ }
164
+
165
+ .btn-equals:hover {
166
+ background: var(--equals-hover);
167
+ }
168
+
169
+ .btn-zero {
170
+ grid-column: span 2;
171
+ }
172
+
173
+ footer {
174
+ background: rgba(255, 255, 255, 0.1);
175
+ backdrop-filter: blur(10px);
176
+ color: white;
177
+ text-align: center;
178
+ padding: 1rem;
179
+ font-size: 0.9rem;
180
+ }
181
+
182
+ @media (max-width: 480px) {
183
+ .calculator {
184
+ padding: 1rem;
185
+ border-radius: 15px;
186
+ }
187
+
188
+ .display {
189
+ padding: 1rem;
190
+ min-height: 80px;
191
+ }
192
+
193
+ .display-current {
194
+ font-size: 2rem;
195
+ }
196
+
197
+ .display-previous {
198
+ font-size: 1rem;
199
+ }
200
+
201
+ .btn {
202
+ font-size: 1.25rem;
203
+ padding: 1rem;
204
+ }
205
+
206
+ .buttons {
207
+ gap: 0.5rem;
208
+ }
209
+ }
210
+
211
+ @media (max-width: 320px) {
212
+ .btn {
213
+ font-size: 1rem;
214
+ padding: 0.75rem;
215
+ }
216
+
217
+ .display-current {
218
+ font-size: 1.5rem;
219
+ }
220
+ }
assets/js/calculator.js ADDED
@@ -0,0 +1,154 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ class Calculator {
2
+ constructor() {
3
+ this.currentValue = '0';
4
+ this.previousValue = '';
5
+ this.operator = null;
6
+ this.shouldResetDisplay = false;
7
+ this.currentDisplay = document.getElementById('currentDisplay');
8
+ this.previousDisplay = document.getElementById('previousDisplay');
9
+
10
+ this.initEventListeners();
11
+ }
12
+
13
+ initEventListeners() {
14
+ const numberButtons = document.querySelectorAll('[data-number]');
15
+ const operatorButtons = document.querySelectorAll('[data-operator]');
16
+ const clearButton = document.querySelector('[data-action="clear"]');
17
+ const deleteButton = document.querySelector('[data-action="delete"]');
18
+ const equalsButton = document.querySelector('[data-action="equals"]');
19
+
20
+ numberButtons.forEach(button => {
21
+ button.addEventListener('click', () => this.appendNumber(button.dataset.number));
22
+ });
23
+
24
+ operatorButtons.forEach(button => {
25
+ button.addEventListener('click', () => this.chooseOperator(button.dataset.operator));
26
+ });
27
+
28
+ clearButton.addEventListener('click', () => this.clear());
29
+ deleteButton.addEventListener('click', () => this.delete());
30
+ equalsButton.addEventListener('click', () => this.calculate());
31
+
32
+ // Keyboard support
33
+ document.addEventListener('keydown', (e) => this.handleKeyboard(e));
34
+ }
35
+
36
+ handleKeyboard(e) {
37
+ if (e.key >= '0' && e.key <= '9') this.appendNumber(e.key);
38
+ if (e.key === '.') this.appendNumber(e.key);
39
+ if (e.key === '+' || e.key === '-' || e.key === '*' || e.key === '/') {
40
+ this.chooseOperator(e.key);
41
+ }
42
+ if (e.key === 'Enter' || e.key === '=') this.calculate();
43
+ if (e.key === 'Backspace') this.delete();
44
+ if (e.key === 'Escape') this.clear();
45
+ }
46
+
47
+ appendNumber(number) {
48
+ if (this.shouldResetDisplay) {
49
+ this.currentValue = '0';
50
+ this.shouldResetDisplay = false;
51
+ }
52
+
53
+ if (number === '.' && this.currentValue.includes('.')) return;
54
+
55
+ if (this.currentValue === '0') {
56
+ this.currentValue = number === '.' ? '0.' : number;
57
+ } else {
58
+ this.currentValue += number;
59
+ }
60
+
61
+ this.updateDisplay();
62
+ }
63
+
64
+ chooseOperator(operator) {
65
+ if (this.operator !== null && !this.shouldResetDisplay) {
66
+ this.calculate();
67
+ }
68
+
69
+ this.operator = operator;
70
+ this.previousValue = this.currentValue;
71
+ this.shouldResetDisplay = true;
72
+ this.updateDisplay();
73
+ }
74
+
75
+ calculate() {
76
+ if (this.operator === null || this.shouldResetDisplay) return;
77
+
78
+ const prev = parseFloat(this.previousValue);
79
+ const current = parseFloat(this.currentValue);
80
+
81
+ if (isNaN(prev) || isNaN(current)) return;
82
+
83
+ let result;
84
+ switch (this.operator) {
85
+ case '+':
86
+ result = prev + current;
87
+ break;
88
+ case '-':
89
+ result = prev - current;
90
+ break;
91
+ case '*':
92
+ result = prev * current;
93
+ break;
94
+ case '/':
95
+ if (current === 0) {
96
+ alert('Cannot divide by zero!');
97
+ this.clear();
98
+ return;
99
+ }
100
+ result = prev / current;
101
+ break;
102
+ default:
103
+ return;
104
+ }
105
+
106
+ // Round to avoid floating point errors
107
+ result = Math.round(result * 100000000) / 100000000;
108
+
109
+ this.currentValue = result.toString();
110
+ this.operator = null;
111
+ this.previousValue = '';
112
+ this.shouldResetDisplay = true;
113
+ this.updateDisplay();
114
+ }
115
+
116
+ clear() {
117
+ this.currentValue = '0';
118
+ this.previousValue = '';
119
+ this.operator = null;
120
+ this.shouldResetDisplay = false;
121
+ this.updateDisplay();
122
+ }
123
+
124
+ delete() {
125
+ if (this.shouldResetDisplay) return;
126
+
127
+ this.currentValue = this.currentValue.slice(0, -1);
128
+ if (this.currentValue === '' || this.currentValue === '-') {
129
+ this.currentValue = '0';
130
+ }
131
+ this.updateDisplay();
132
+ }
133
+
134
+ updateDisplay() {
135
+ this.currentDisplay.textContent = this.currentValue;
136
+
137
+ if (this.operator !== null) {
138
+ const operatorSymbols = {
139
+ '+': '+',
140
+ '-': '−',
141
+ '*': '×',
142
+ '/': '÷'
143
+ };
144
+ this.previousDisplay.textContent = `${this.previousValue} ${operatorSymbols[this.operator]}`;
145
+ } else {
146
+ this.previousDisplay.textContent = '';
147
+ }
148
+ }
149
+ }
150
+
151
+ // Initialize calculator when DOM is loaded
152
+ document.addEventListener('DOMContentLoaded', () => {
153
+ new Calculator();
154
+ });
index.html CHANGED
@@ -1,19 +1,55 @@
1
- <!doctype html>
2
- <html>
3
- <head>
4
- <meta charset="utf-8" />
5
- <meta name="viewport" content="width=device-width" />
6
- <title>My static Space</title>
7
- <link rel="stylesheet" href="style.css" />
8
- </head>
9
- <body>
10
- <div class="card">
11
- <h1>Welcome to your static Space!</h1>
12
- <p>You can modify this app directly by editing <i>index.html</i> in the Files and versions tab.</p>
13
- <p>
14
- Also don't forget to check the
15
- <a href="https://huggingface.co/docs/hub/spaces" target="_blank">Spaces documentation</a>.
16
- </p>
17
- </div>
18
- </body>
19
- </html>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <meta name="description" content="A simple, modern calculator with basic arithmetic operations">
7
+ <title>Calculator - Basic Arithmetic</title>
8
+ <link rel="stylesheet" href="assets/css/styles.css">
9
+ </head>
10
+ <body>
11
+ <header>
12
+ <a href="https://huggingface.co/spaces/akhaliq/anycoder" target="_blank" rel="noopener noreferrer" class="built-with">Built with anycoder</a>
13
+ </header>
14
+
15
+ <main>
16
+ <div class="calculator">
17
+ <div class="display">
18
+ <div class="display-previous" id="previousDisplay"></div>
19
+ <div class="display-current" id="currentDisplay">0</div>
20
+ </div>
21
+
22
+ <div class="buttons">
23
+ <button class="btn btn-clear" data-action="clear">C</button>
24
+ <button class="btn btn-delete" data-action="delete">DEL</button>
25
+ <button class="btn btn-operator" data-operator="/">÷</button>
26
+ <button class="btn btn-operator" data-operator="*">×</button>
27
+
28
+ <button class="btn btn-number" data-number="7">7</button>
29
+ <button class="btn btn-number" data-number="8">8</button>
30
+ <button class="btn btn-number" data-number="9">9</button>
31
+ <button class="btn btn-operator" data-operator="-">−</button>
32
+
33
+ <button class="btn btn-number" data-number="4">4</button>
34
+ <button class="btn btn-number" data-number="5">5</button>
35
+ <button class="btn btn-number" data-number="6">6</button>
36
+ <button class="btn btn-operator" data-operator="+">+</button>
37
+
38
+ <button class="btn btn-number" data-number="1">1</button>
39
+ <button class="btn btn-number" data-number="2">2</button>
40
+ <button class="btn btn-number" data-number="3">3</button>
41
+ <button class="btn btn-equals" data-action="equals" id="equalsBtn">=</button>
42
+
43
+ <button class="btn btn-number btn-zero" data-number="0">0</button>
44
+ <button class="btn btn-number" data-number=".">.</button>
45
+ </div>
46
+ </div>
47
+ </main>
48
+
49
+ <footer>
50
+ <p>&copy; 2024 Calculator App. Simple & Efficient.</p>
51
+ </footer>
52
+
53
+ <script src="assets/js/calculator.js"></script>
54
+ </body>
55
+ </html>