Erik commited on
Commit
407127f
·
verified ·
1 Parent(s): 818aaa8

Calculator

Browse files
Files changed (4) hide show
  1. README.md +7 -4
  2. index.html +66 -19
  3. script.js +185 -0
  4. style.css +27 -19
README.md CHANGED
@@ -1,10 +1,13 @@
1
  ---
2
- title: Calcmaster Pro
3
- emoji: 😻
4
- colorFrom: green
5
  colorTo: gray
 
6
  sdk: static
7
  pinned: false
 
 
8
  ---
9
 
10
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
1
  ---
2
+ title: CalcMaster Pro 🧮
3
+ colorFrom: purple
 
4
  colorTo: gray
5
+ emoji: 🐳
6
  sdk: static
7
  pinned: false
8
+ tags:
9
+ - deepsite-v3
10
  ---
11
 
12
+ # Welcome to your new DeepSite project!
13
+ This project was created with [DeepSite](https://huggingface.co/deepsite).
index.html CHANGED
@@ -1,19 +1,66 @@
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
+ <title>CalcMaster Pro</title>
7
+ <link rel="stylesheet" href="style.css">
8
+ <script src="https://cdn.tailwindcss.com"></script>
9
+ <script src="https://unpkg.com/feather-icons"></script>
10
+ </head>
11
+ <body class="bg-gradient-to-br from-gray-900 to-black min-h-screen flex items-center justify-center p-4">
12
+ <div class="w-full max-w-md">
13
+ <div class="bg-gray-800 rounded-3xl shadow-2xl overflow-hidden">
14
+ <!-- Display -->
15
+ <div class="p-6 bg-gray-900">
16
+ <div class="text-right">
17
+ <div id="previous-operand" class="text-gray-400 text-xl h-7 overflow-hidden"></div>
18
+ <div id="current-operand" class="text-white text-4xl font-bold h-12 overflow-hidden">0</div>
19
+ </div>
20
+ </div>
21
+
22
+ <!-- Keypad -->
23
+ <div class="p-5">
24
+ <div class="grid grid-cols-4 gap-4">
25
+ <button data-action="clear" class="bg-red-500 hover:bg-red-600 text-white font-bold py-4 rounded-xl transition duration-200 active:scale-95">C</button>
26
+ <button data-action="delete" class="bg-amber-500 hover:bg-amber-600 text-white font-bold py-4 rounded-xl transition duration-200 active:scale-95">
27
+ <i data-feather="delete"></i>
28
+ </button>
29
+ <button data-action="operation" data-operation="%" class="bg-amber-500 hover:bg-amber-600 text-white font-bold py-4 rounded-xl transition duration-200 active:scale-95">%</button>
30
+ <button data-action="operation" data-operation="÷" class="bg-amber-500 hover:bg-amber-600 text-white font-bold py-4 rounded-xl transition duration-200 active:scale-95">÷</button>
31
+
32
+ <button data-number="7" class="bg-gray-700 hover:bg-gray-600 text-white font-bold py-4 rounded-xl transition duration-200 active:scale-95">7</button>
33
+ <button data-number="8" class="bg-gray-700 hover:bg-gray-600 text-white font-bold py-4 rounded-xl transition duration-200 active:scale-95">8</button>
34
+ <button data-number="9" class="bg-gray-700 hover:bg-gray-600 text-white font-bold py-4 rounded-xl transition duration-200 active:scale-95">9</button>
35
+ <button data-action="operation" data-operation="×" class="bg-amber-500 hover:bg-amber-600 text-white font-bold py-4 rounded-xl transition duration-200 active:scale-95">×</button>
36
+
37
+ <button data-number="4" class="bg-gray-700 hover:bg-gray-600 text-white font-bold py-4 rounded-xl transition duration-200 active:scale-95">4</button>
38
+ <button data-number="5" class="bg-gray-700 hover:bg-gray-600 text-white font-bold py-4 rounded-xl transition duration-200 active:scale-95">5</button>
39
+ <button data-number="6" class="bg-gray-700 hover:bg-gray-600 text-white font-bold py-4 rounded-xl transition duration-200 active:scale-95">6</button>
40
+ <button data-action="operation" data-operation="-" class="bg-amber-500 hover:bg-amber-600 text-white font-bold py-4 rounded-xl transition duration-200 active:scale-95">-</button>
41
+
42
+ <button data-number="1" class="bg-gray-700 hover:bg-gray-600 text-white font-bold py-4 rounded-xl transition duration-200 active:scale-95">1</button>
43
+ <button data-number="2" class="bg-gray-700 hover:bg-gray-600 text-white font-bold py-4 rounded-xl transition duration-200 active:scale-95">2</button>
44
+ <button data-number="3" class="bg-gray-700 hover:bg-gray-600 text-white font-bold py-4 rounded-xl transition duration-200 active:scale-95">3</button>
45
+ <button data-action="operation" data-operation="+" class="bg-amber-500 hover:bg-amber-600 text-white font-bold py-4 rounded-xl transition duration-200 active:scale-95">+</button>
46
+
47
+ <button data-number="0" class="bg-gray-700 hover:bg-gray-600 text-white font-bold py-4 rounded-xl col-span-2 transition duration-200 active:scale-95">0</button>
48
+ <button data-number="." class="bg-gray-700 hover:bg-gray-600 text-white font-bold py-4 rounded-xl transition duration-200 active:scale-95">.</button>
49
+ <button data-action="calculate" class="bg-green-500 hover:bg-green-600 text-white font-bold py-4 rounded-xl transition duration-200 active:scale-95">=</button>
50
+ </div>
51
+ </div>
52
+ </div>
53
+
54
+ <div class="mt-8 text-center">
55
+ <h1 class="text-3xl font-bold text-white mb-2">CalcMaster Pro 🧮</h1>
56
+ <p class="text-gray-400">The ultimate calculator experience</p>
57
+ </div>
58
+ </div>
59
+
60
+ <script src="script.js"></script>
61
+ <script>
62
+ feather.replace();
63
+ </script>
64
+ <script src="https://huggingface.co/deepsite/deepsite-badge.js"></script>
65
+ </body>
66
+ </html>
script.js ADDED
@@ -0,0 +1,185 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ class Calculator {
2
+ constructor(previousOperandElement, currentOperandElement) {
3
+ this.previousOperandElement = previousOperandElement;
4
+ this.currentOperandElement = currentOperandElement;
5
+ this.clear();
6
+ }
7
+
8
+ clear() {
9
+ this.currentOperand = '0';
10
+ this.previousOperand = '';
11
+ this.operation = undefined;
12
+ this.resetNextInput = false;
13
+ }
14
+
15
+ delete() {
16
+ if (this.resetNextInput) return;
17
+ this.currentOperand = this.currentOperand.toString().slice(0, -1);
18
+ if (this.currentOperand === '') {
19
+ this.currentOperand = '0';
20
+ }
21
+ }
22
+
23
+ appendNumber(number) {
24
+ if (this.resetNextInput) {
25
+ this.currentOperand = '0';
26
+ this.resetNextInput = false;
27
+ }
28
+ if (number === '.' && this.currentOperand.includes('.')) return;
29
+ if (this.currentOperand === '0' && number !== '.') {
30
+ this.currentOperand = number.toString();
31
+ } else {
32
+ this.currentOperand = this.currentOperand.toString() + number.toString();
33
+ }
34
+ }
35
+
36
+ chooseOperation(operation) {
37
+ if (this.currentOperand === '') return;
38
+ if (this.previousOperand !== '') {
39
+ this.calculate();
40
+ }
41
+ this.operation = operation;
42
+ this.previousOperand = this.currentOperand;
43
+ this.resetNextInput = true;
44
+ }
45
+
46
+ calculate() {
47
+ let computation;
48
+ const prev = parseFloat(this.previousOperand);
49
+ const current = parseFloat(this.currentOperand);
50
+ if (isNaN(prev) || isNaN(current)) return;
51
+
52
+ switch (this.operation) {
53
+ case '+':
54
+ computation = prev + current;
55
+ break;
56
+ case '-':
57
+ computation = prev - current;
58
+ break;
59
+ case '×':
60
+ computation = prev * current;
61
+ break;
62
+ case '÷':
63
+ if (current === 0) {
64
+ computation = 'Error';
65
+ } else {
66
+ computation = prev / current;
67
+ }
68
+ break;
69
+ case '%':
70
+ computation = prev % current;
71
+ break;
72
+ default:
73
+ return;
74
+ }
75
+
76
+ this.currentOperand = computation.toString();
77
+ this.operation = undefined;
78
+ this.previousOperand = '';
79
+ this.resetNextInput = true;
80
+ }
81
+
82
+ getDisplayNumber(number) {
83
+ if (number === 'Error') return number;
84
+
85
+ const stringNumber = number.toString();
86
+ const integerDigits = parseFloat(stringNumber.split('.')[0]);
87
+ const decimalDigits = stringNumber.split('.')[1];
88
+
89
+ let integerDisplay;
90
+ if (isNaN(integerDigits)) {
91
+ integerDisplay = '';
92
+ } else {
93
+ integerDisplay = integerDigits.toLocaleString('en', {
94
+ maximumFractionDigits: 0
95
+ });
96
+ }
97
+
98
+ if (decimalDigits != null) {
99
+ return `${integerDisplay}.${decimalDigits}`;
100
+ } else {
101
+ return integerDisplay;
102
+ }
103
+ }
104
+
105
+ updateDisplay() {
106
+ this.currentOperandElement.innerText = this.getDisplayNumber(this.currentOperand);
107
+ if (this.operation != null) {
108
+ this.previousOperandElement.innerText =
109
+ `${this.getDisplayNumber(this.previousOperand)} ${this.operation}`;
110
+ } else {
111
+ this.previousOperandElement.innerText = '';
112
+ }
113
+ }
114
+ }
115
+
116
+ // Initialize calculator
117
+ const previousOperandElement = document.getElementById('previous-operand');
118
+ const currentOperandElement = document.getElementById('current-operand');
119
+
120
+ const calculator = new Calculator(previousOperandElement, currentOperandElement);
121
+
122
+ // Event listeners
123
+ document.querySelectorAll('[data-number]').forEach(button => {
124
+ button.addEventListener('click', () => {
125
+ calculator.appendNumber(button.getAttribute('data-number'));
126
+ calculator.updateDisplay();
127
+ });
128
+ });
129
+
130
+ document.querySelectorAll('[data-operation]').forEach(button => {
131
+ button.addEventListener('click', () => {
132
+ calculator.chooseOperation(button.getAttribute('data-operation'));
133
+ calculator.updateDisplay();
134
+ });
135
+ });
136
+
137
+ document.querySelector('[data-action="calculate"]').addEventListener('click', () => {
138
+ calculator.calculate();
139
+ calculator.updateDisplay();
140
+ });
141
+
142
+ document.querySelector('[data-action="clear"]').addEventListener('click', () => {
143
+ calculator.clear();
144
+ calculator.updateDisplay();
145
+ });
146
+
147
+ document.querySelector('[data-action="delete"]').addEventListener('click', () => {
148
+ calculator.delete();
149
+ calculator.updateDisplay();
150
+ });
151
+
152
+ // Keyboard support
153
+ document.addEventListener('keydown', event => {
154
+ if (event.key >= 0 && event.key <= 9) {
155
+ calculator.appendNumber(event.key);
156
+ calculator.updateDisplay();
157
+ }
158
+ if (event.key === '.') {
159
+ calculator.appendNumber('.');
160
+ calculator.updateDisplay();
161
+ }
162
+ if (event.key === '+' || event.key === '-' || event.key === '*' || event.key === '/') {
163
+ let op = event.key;
164
+ if (op === '*') op = '×';
165
+ if (op === '/') op = '÷';
166
+ calculator.chooseOperation(op);
167
+ calculator.updateDisplay();
168
+ }
169
+ if (event.key === '%') {
170
+ calculator.chooseOperation('%');
171
+ calculator.updateDisplay();
172
+ }
173
+ if (event.key === 'Enter' || event.key === '=') {
174
+ calculator.calculate();
175
+ calculator.updateDisplay();
176
+ }
177
+ if (event.key === 'Escape') {
178
+ calculator.clear();
179
+ calculator.updateDisplay();
180
+ }
181
+ if (event.key === 'Backspace') {
182
+ calculator.delete();
183
+ calculator.updateDisplay();
184
+ }
185
+ });
style.css CHANGED
@@ -1,28 +1,36 @@
1
- body {
2
- padding: 2rem;
3
- font-family: -apple-system, BlinkMacSystemFont, "Arial", sans-serif;
 
 
 
4
  }
5
 
6
- h1 {
7
- font-size: 16px;
8
- margin-top: 0;
 
 
 
 
9
  }
10
 
11
- p {
12
- color: rgb(107, 114, 128);
13
- font-size: 15px;
14
- margin-bottom: 10px;
15
- margin-top: 5px;
16
  }
17
 
18
- .card {
19
- max-width: 620px;
20
- margin: 0 auto;
21
- padding: 16px;
22
- border: 1px solid lightgray;
23
- border-radius: 16px;
24
  }
25
 
26
- .card p:last-child {
27
- margin-bottom: 0;
 
28
  }
 
 
 
 
 
 
1
+ @import url('https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;500;700&display=swap');
2
+
3
+ * {
4
+ margin: 0;
5
+ padding: 0;
6
+ box-sizing: border-box;
7
  }
8
 
9
+ body {
10
+ font-family: 'Roboto', sans-serif;
11
+ background: linear-gradient(135deg, #1a202c, #2d3748);
12
+ min-height: 100vh;
13
+ display: flex;
14
+ align-items: center;
15
+ justify-content: center;
16
  }
17
 
18
+ button {
19
+ outline: none;
20
+ border: none;
21
+ cursor: pointer;
 
22
  }
23
 
24
+ button:focus {
25
+ outline: none;
 
 
 
 
26
  }
27
 
28
+ #previous-operand {
29
+ word-wrap: break-word;
30
+ word-break: break-all;
31
  }
32
+
33
+ #current-operand {
34
+ word-wrap: break-word;
35
+ word-break: break-all;
36
+ }