tommytracx commited on
Commit
e12c2f8
·
verified ·
1 Parent(s): 87b51ec

Upload components/Calculator.jsx with huggingface_hub

Browse files
Files changed (1) hide show
  1. components/Calculator.jsx +178 -0
components/Calculator.jsx ADDED
@@ -0,0 +1,178 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { useState } from 'react';
2
+
3
+ export default function Calculator() {
4
+ const [displayValue, setDisplayValue] = useState('0');
5
+ const [firstOperand, setFirstOperand] = useState(null);
6
+ const [operator, setOperator] = useState(null);
7
+ const [waitingForSecondOperand, setWaitingForSecondOperand] = useState(false);
8
+
9
+ const inputDigit = (digit) => {
10
+ if (waitingForSecondOperand) {
11
+ setDisplayValue(String(digit));
12
+ setWaitingForSecondOperand(false);
13
+ } else {
14
+ setDisplayValue(displayValue === '0' ? String(digit) : displayValue + digit);
15
+ }
16
+ };
17
+
18
+ const inputDecimal = () => {
19
+ if (waitingForSecondOperand) {
20
+ setDisplayValue('0.');
21
+ setWaitingForSecondOperand(false);
22
+ return;
23
+ }
24
+
25
+ if (!displayValue.includes('.')) {
26
+ setDisplayValue(displayValue + '.');
27
+ }
28
+ };
29
+
30
+ const clearDisplay = () => {
31
+ setDisplayValue('0');
32
+ setFirstOperand(null);
33
+ setOperator(null);
34
+ setWaitingForSecondOperand(false);
35
+ };
36
+
37
+ const performOperation = (nextOperator) => {
38
+ const inputValue = parseFloat(displayValue);
39
+
40
+ if (firstOperand === null) {
41
+ setFirstOperand(inputValue);
42
+ } else if (operator) {
43
+ const result = calculate(firstOperand, inputValue, operator);
44
+ setDisplayValue(String(result));
45
+ setFirstOperand(result);
46
+ }
47
+
48
+ setWaitingForSecondOperand(true);
49
+ setOperator(nextOperator);
50
+ };
51
+
52
+ const calculate = (firstOperand, secondOperand, operator) => {
53
+ switch (operator) {
54
+ case '+':
55
+ return firstOperand + secondOperand;
56
+ case '-':
57
+ return firstOperand - secondOperand;
58
+ case '*':
59
+ return firstOperand * secondOperand;
60
+ case '/':
61
+ return firstOperand / secondOperand;
62
+ default:
63
+ return secondOperand;
64
+ }
65
+ };
66
+
67
+ const handleEquals = () => {
68
+ if (!operator || firstOperand === null) return;
69
+
70
+ const inputValue = parseFloat(displayValue);
71
+ const result = calculate(firstOperand, inputValue, operator);
72
+
73
+ setDisplayValue(String(result));
74
+ setFirstOperand(null);
75
+ setOperator(null);
76
+ setWaitingForSecondOperand(false);
77
+ };
78
+
79
+ return (
80
+ <div className="w-full max-w-md mx-auto bg-calc-bg rounded-xl shadow-lg overflow-hidden">
81
+ <div className="p-4">
82
+ <div className="text-right mb-4">
83
+ <a
84
+ href="https://huggingface.co/spaces/akhaliq/anycoder"
85
+ target="_blank"
86
+ rel="noopener noreferrer"
87
+ className="text-sm text-gray-500 hover:text-gray-700 transition-colors"
88
+ >
89
+ Built with anycoder
90
+ </a>
91
+ </div>
92
+
93
+ <div className="bg-calc-display rounded-lg p-6 mb-4 text-right">
94
+ <div className="text-4xl font-light text-gray-800 overflow-hidden">
95
+ {displayValue}
96
+ </div>
97
+ </div>
98
+
99
+ <div className="grid grid-cols-4 gap-2">
100
+ {['7', '8', '9', '/'].map((btn) => (
101
+ <button
102
+ key={btn}
103
+ className={`p-4 rounded-lg text-xl font-medium transition-colors ${
104
+ isNaN(btn)
105
+ ? 'bg-calc-btn-operator hover:bg-calc-btn-operator-hover active:bg-calc-btn-operator-active text-white'
106
+ : 'bg-calc-btn hover:bg-calc-btn-hover active:bg-calc-btn-active text-gray-800'
107
+ }`}
108
+ onClick={() => isNaN(btn) ? performOperation(btn) : inputDigit(btn)}
109
+ >
110
+ {btn}
111
+ </button>
112
+ ))}
113
+
114
+ {['4', '5', '6', '*'].map((btn) => (
115
+ <button
116
+ key={btn}
117
+ className={`p-4 rounded-lg text-xl font-medium transition-colors ${
118
+ isNaN(btn)
119
+ ? 'bg-calc-btn-operator hover:bg-calc-btn-operator-hover active:bg-calc-btn-operator-active text-white'
120
+ : 'bg-calc-btn hover:bg-calc-btn-hover active:bg-calc-btn-active text-gray-800'
121
+ }`}
122
+ onClick={() => isNaN(btn) ? performOperation(btn) : inputDigit(btn)}
123
+ >
124
+ {btn}
125
+ </button>
126
+ ))}
127
+
128
+ {['1', '2', '3', '-'].map((btn) => (
129
+ <button
130
+ key={btn}
131
+ className={`p-4 rounded-lg text-xl font-medium transition-colors ${
132
+ isNaN(btn)
133
+ ? 'bg-calc-btn-operator hover:bg-calc-btn-operator-hover active:bg-calc-btn-operator-active text-white'
134
+ : 'bg-calc-btn hover:bg-calc-btn-hover active:bg-calc-btn-active text-gray-800'
135
+ }`}
136
+ onClick={() => isNaN(btn) ? performOperation(btn) : inputDigit(btn)}
137
+ >
138
+ {btn}
139
+ </button>
140
+ ))}
141
+
142
+ {['0', '.', '=', '+'].map((btn) => (
143
+ <button
144
+ key={btn}
145
+ className={`p-4 rounded-lg text-xl font-medium transition-colors ${
146
+ btn === '='
147
+ ? 'bg-calc-btn-operator hover:bg-calc-btn-operator-hover active:bg-calc-btn-operator-active text-white'
148
+ : isNaN(btn)
149
+ ? 'bg-calc-btn hover:bg-calc-btn-hover active:bg-calc-btn-active text-gray-800'
150
+ : 'bg-calc-btn hover:bg-calc-btn-hover active:bg-calc-btn-active text-gray-800 col-span-2'
151
+ }`}
152
+ onClick={() => {
153
+ if (btn === '=') {
154
+ handleEquals();
155
+ } else if (btn === '.') {
156
+ inputDecimal();
157
+ } else if (isNaN(btn)) {
158
+ performOperation(btn);
159
+ } else {
160
+ inputDigit(btn);
161
+ }
162
+
163
+ >
164
+ {btn}
165
+ </button>
166
+ ))}
167
+
168
+ <button
169
+ className="col-span-4 p-4 rounded-lg text-xl font-medium bg-gray-400 hover:bg-gray-500 active:bg-gray-600 text-white transition-colors"
170
+ onClick={clearDisplay}
171
+ >
172
+ AC
173
+ </button>
174
+ </div>
175
+ </div>
176
+ </div>
177
+ );
178
+ }