Spaces:
Build error
Build error
Upload src/App.svelte with huggingface_hub
Browse files- src/App.svelte +132 -42
src/App.svelte
CHANGED
@@ -1,47 +1,137 @@
|
|
|
|
1 |
<script lang="ts">
|
2 |
-
|
3 |
-
|
4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
</script>
|
6 |
|
7 |
<main>
|
8 |
-
<div>
|
9 |
-
<
|
10 |
-
<
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
</
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
</div>
|
21 |
-
|
22 |
-
<p>
|
23 |
-
Check out <a href="https://github.com/sveltejs/kit#readme" target="_blank" rel="noreferrer">SvelteKit</a>, the official Svelte app framework powered by Vite!
|
24 |
-
</p>
|
25 |
-
|
26 |
-
<p class="read-the-docs">
|
27 |
-
Click on the Vite and Svelte logos to learn more
|
28 |
-
</p>
|
29 |
-
</main>
|
30 |
-
|
31 |
-
<style>
|
32 |
-
.logo {
|
33 |
-
height: 6em;
|
34 |
-
padding: 1.5em;
|
35 |
-
will-change: filter;
|
36 |
-
transition: filter 300ms;
|
37 |
-
}
|
38 |
-
.logo:hover {
|
39 |
-
filter: drop-shadow(0 0 2em #646cffaa);
|
40 |
-
}
|
41 |
-
.logo.svelte:hover {
|
42 |
-
filter: drop-shadow(0 0 2em #ff3e00aa);
|
43 |
-
}
|
44 |
-
.read-the-docs {
|
45 |
-
color: #888;
|
46 |
-
}
|
47 |
-
</style>
|
|
|
1 |
+
<!-- src/App.svelte -->
|
2 |
<script lang="ts">
|
3 |
+
let display = '0';
|
4 |
+
let previous = 0;
|
5 |
+
let operator: string | null = null;
|
6 |
+
let waitingForOperand = false;
|
7 |
+
let error = '';
|
8 |
+
|
9 |
+
const clear = () => {
|
10 |
+
display = '0';
|
11 |
+
previous = 0;
|
12 |
+
operator = null;
|
13 |
+
waitingForOperand = false;
|
14 |
+
error = '';
|
15 |
+
};
|
16 |
+
|
17 |
+
const inputDigit = (digit: number) => {
|
18 |
+
if (waitingForOperand) {
|
19 |
+
display = String(digit);
|
20 |
+
waitingForOperand = false;
|
21 |
+
} else {
|
22 |
+
display = display === '0' ? String(digit) : display + digit;
|
23 |
+
}
|
24 |
+
};
|
25 |
+
|
26 |
+
const inputDecimal = () => {
|
27 |
+
if (waitingForOperand) {
|
28 |
+
display = '0.';
|
29 |
+
waitingForOperand = false;
|
30 |
+
} else if (display.indexOf('.') === -1) {
|
31 |
+
display += '.';
|
32 |
+
}
|
33 |
+
};
|
34 |
+
|
35 |
+
const performOperation = (nextOperator: string) => {
|
36 |
+
const inputValue = parseFloat(display);
|
37 |
+
|
38 |
+
if (isNaN(inputValue)) return;
|
39 |
+
|
40 |
+
if (operator && !waitingForOperand) {
|
41 |
+
const result = calculate(previous, inputValue, operator);
|
42 |
+
if (typeof result === 'string') {
|
43 |
+
error = result;
|
44 |
+
display = 'Error';
|
45 |
+
return;
|
46 |
+
}
|
47 |
+
display = String(result);
|
48 |
+
previous = result;
|
49 |
+
} else {
|
50 |
+
previous = inputValue;
|
51 |
+
}
|
52 |
+
|
53 |
+
waitingForOperand = true;
|
54 |
+
operator = nextOperator;
|
55 |
+
};
|
56 |
+
|
57 |
+
const calculate = (first: number, second: number, op: string): number | string => {
|
58 |
+
switch (op) {
|
59 |
+
case '+':
|
60 |
+
return first + second;
|
61 |
+
case '-':
|
62 |
+
return first - second;
|
63 |
+
case '*':
|
64 |
+
return first * second;
|
65 |
+
case '/':
|
66 |
+
if (second === 0) return 'Cannot divide by zero';
|
67 |
+
return first / second;
|
68 |
+
default:
|
69 |
+
return second;
|
70 |
+
}
|
71 |
+
};
|
72 |
+
|
73 |
+
const handleEquals = () => {
|
74 |
+
if (!operator || waitingForOperand) return;
|
75 |
+
|
76 |
+
const inputValue = parseFloat(display);
|
77 |
+
const result = calculate(previous, inputValue, operator);
|
78 |
+
|
79 |
+
if (typeof result === 'string') {
|
80 |
+
error = result;
|
81 |
+
display = 'Error';
|
82 |
+
} else {
|
83 |
+
display = String(result);
|
84 |
+
previous = result;
|
85 |
+
}
|
86 |
+
|
87 |
+
waitingForOperand = true;
|
88 |
+
operator = null;
|
89 |
+
};
|
90 |
+
|
91 |
+
const handlePercent = () => {
|
92 |
+
const value = parseFloat(display);
|
93 |
+
display = String(value / 100);
|
94 |
+
};
|
95 |
+
|
96 |
+
const handleNegate = () => {
|
97 |
+
const value = parseFloat(display);
|
98 |
+
display = String(value * -1);
|
99 |
+
};
|
100 |
</script>
|
101 |
|
102 |
<main>
|
103 |
+
<div class="calculator">
|
104 |
+
<div class="display">
|
105 |
+
<div class="display-text">{display}</div>
|
106 |
+
{#if error}
|
107 |
+
<div class="error">{error}</div>
|
108 |
+
{/if}
|
109 |
+
</div>
|
110 |
+
|
111 |
+
<div class="buttons">
|
112 |
+
<button class="function" on:click={clear}>C</button>
|
113 |
+
<button class="function" on:click={handleNegate}>±</button>
|
114 |
+
<button class="function" on:click={handlePercent}>%</button>
|
115 |
+
<button class="operator" on:click={() => performOperation('/')}>/</button>
|
116 |
+
|
117 |
+
<button on:click={() => inputDigit(7)}>7</button>
|
118 |
+
<button on:click={() => inputDigit(8)}>8</button>
|
119 |
+
<button on:click={() => inputDigit(9)}>9</button>
|
120 |
+
<button class="operator" on:click={() => performOperation('*')}>×</button>
|
121 |
+
|
122 |
+
<button on:click={() => inputDigit(4)}>4</button>
|
123 |
+
<button on:click={() => inputDigit(5)}>5</button>
|
124 |
+
<button on:click={() => inputDigit(6)}>6</button>
|
125 |
+
<button class="operator" on:click={() => performOperation('-')}>−</button>
|
126 |
+
|
127 |
+
<button on:click={() => inputDigit(1)}>1</button>
|
128 |
+
<button on:click={() => inputDigit(2)}>2</button>
|
129 |
+
<button on:click={() => inputDigit(3)}>3</button>
|
130 |
+
<button class="operator" on:click={() => performOperation('+')}>+</button>
|
131 |
+
|
132 |
+
<button class="zero" on:click={() => inputDigit(0)}>0</button>
|
133 |
+
<button on:click={inputDecimal}>.</button>
|
134 |
+
<button class="equals" on:click={handleEquals}>=</button>
|
135 |
+
</div>
|
136 |
</div>
|
137 |
+
</main>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|