-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculator.tsx
260 lines (242 loc) · 8.01 KB
/
calculator.tsx
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
254
255
256
257
258
259
260
"use client"
import { useState } from "react"
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"
import { Input } from "@/components/ui/input"
import { Button } from "@/components/ui/button"
import { Label } from "@/components/ui/label"
import { Card, CardContent, CardHeader, CardTitle, CardDescription } from "@/components/ui/card"
export default function Component() {
return (
<div className="container mx-auto p-4">
<h1 className="text-3xl font-bold mb-6 text-center">Multi-Purpose Calculator</h1>
<p className="text-center text-lg mb-4 text-muted-foreground">by Shreeyut Saraf</p>
<Tabs defaultValue="binary" className="w-full max-w-3xl mx-auto">
<TabsList className="grid w-full grid-cols-3 mb-4">
<TabsTrigger value="binary">Binary</TabsTrigger>
<TabsTrigger value="unit-conversion">Unit Conversion</TabsTrigger>
<TabsTrigger value="arithmetic">Arithmetic</TabsTrigger>
</TabsList>
<TabsContent value="binary">
<BinaryCalculator />
</TabsContent>
<TabsContent value="unit-conversion">
<UnitConversionCalculator />
</TabsContent>
<TabsContent value="arithmetic">
<ArithmeticCalculator />
</TabsContent>
</Tabs>
</div>
)
}
function BinaryCalculator() {
const [binary1, setBinary1] = useState("")
const [binary2, setBinary2] = useState("")
const [operation, setOperation] = useState("add")
const [result, setResult] = useState("")
const calculateBinary = () => {
const num1 = parseInt(binary1, 2)
const num2 = parseInt(binary2, 2)
let calculatedResult
switch (operation) {
case "add":
calculatedResult = (num1 + num2).toString(2)
break
case "subtract":
calculatedResult = (num1 - num2).toString(2)
break
case "multiply":
calculatedResult = (num1 * num2).toString(2)
break
case "divide":
calculatedResult = Math.floor(num1 / num2).toString(2)
break
default:
calculatedResult = "Error"
}
setResult(calculatedResult)
}
return (
<Card>
<CardHeader>
<CardTitle>Binary Calculator</CardTitle>
<CardDescription>Perform binary operations</CardDescription>
</CardHeader>
<CardContent className="space-y-4">
<div className="space-y-2">
<Label htmlFor="binary1">Binary Number 1</Label>
<Input
id="binary1"
value={binary1}
onChange={(e) => setBinary1(e.target.value.replace(/[^01]/g, ""))}
placeholder="Enter binary number"
/>
</div>
<div className="space-y-2">
<Label htmlFor="binary2">Binary Number 2</Label>
<Input
id="binary2"
value={binary2}
onChange={(e) => setBinary2(e.target.value.replace(/[^01]/g, ""))}
placeholder="Enter binary number"
/>
</div>
<div className="space-y-2">
<Label htmlFor="operation">Operation</Label>
<select
id="operation"
value={operation}
onChange={(e) => setOperation(e.target.value)}
className="w-full p-2 border rounded"
>
<option value="add">Add</option>
<option value="subtract">Subtract</option>
<option value="multiply">Multiply</option>
<option value="divide">Divide</option>
</select>
</div>
<Button onClick={calculateBinary}>Calculate</Button>
{result && (
<div className="mt-4">
<Label>Result</Label>
<div className="text-2xl font-bold">{result}</div>
</div>
)}
</CardContent>
</Card>
)
}
function UnitConversionCalculator() {
const [value, setValue] = useState("")
const [fromUnit, setFromUnit] = useState("kg")
const [toUnit, setToUnit] = useState("g")
const [result, setResult] = useState("")
const convertUnit = () => {
const numValue = parseFloat(value)
if (isNaN(numValue)) {
setResult("Invalid input")
return
}
let calculatedResult
switch (`${fromUnit}-${toUnit}`) {
case "kg-g":
calculatedResult = numValue * 1000
break
case "g-kg":
calculatedResult = numValue / 1000
break
case "l-ml":
calculatedResult = numValue * 1000
break
case "ml-l":
calculatedResult = numValue / 1000
break
default:
calculatedResult = numValue // Same unit, no conversion needed
}
setResult(`${calculatedResult} ${toUnit}`)
}
return (
<Card>
<CardHeader>
<CardTitle>Unit Conversion Calculator</CardTitle>
<CardDescription>Convert between different units</CardDescription>
</CardHeader>
<CardContent className="space-y-4">
<div className="space-y-2">
<Label htmlFor="value">Value</Label>
<Input
id="value"
type="number"
value={value}
onChange={(e) => setValue(e.target.value)}
placeholder="Enter value"
/>
</div>
<div className="grid grid-cols-2 gap-4">
<div className="space-y-2">
<Label htmlFor="fromUnit">From Unit</Label>
<select
id="fromUnit"
value={fromUnit}
onChange={(e) => setFromUnit(e.target.value)}
className="w-full p-2 border rounded"
>
<option value="kg">Kilogram (kg)</option>
<option value="g">Gram (g)</option>
<option value="l">Liter (L)</option>
<option value="ml">Milliliter (mL)</option>
</select>
</div>
<div className="space-y-2">
<Label htmlFor="toUnit">To Unit</Label>
<select
id="toUnit"
value={toUnit}
onChange={(e) => setToUnit(e.target.value)}
className="w-full p-2 border rounded"
>
<option value="kg">Kilogram (kg)</option>
<option value="g">Gram (g)</option>
<option value="l">Liter (L)</option>
<option value="ml">Milliliter (mL)</option>
</select>
</div>
</div>
<Button onClick={convertUnit}>Convert</Button>
{result && (
<div className="mt-4">
<Label>Result</Label>
<div className="text-2xl font-bold">{result}</div>
</div>
)}
</CardContent>
</Card>
)
}
function ArithmeticCalculator() {
const [expression, setExpression] = useState("")
const [result, setResult] = useState("")
const appendToExpression = (value: string) => {
setExpression((prev) => prev + value)
}
const calculateResult = () => {
try {
// Using eval here for simplicity. In a production environment, use a safer alternative.
const calculatedResult = eval(expression)
setResult(calculatedResult.toString())
} catch (error) {
setResult("Error")
}
}
const clearExpression = () => {
setExpression("")
setResult("")
}
return (
<Card>
<CardHeader>
<CardTitle>Arithmetic Calculator</CardTitle>
<CardDescription>Perform basic arithmetic operations</CardDescription>
</CardHeader>
<CardContent className="space-y-4">
<Input value={expression} readOnly className="text-right text-2xl" />
{result && <div className="text-right text-3xl font-bold">{result}</div>}
<div className="grid grid-cols-4 gap-2">
{["7", "8", "9", "/", "4", "5", "6", "*", "1", "2", "3", "-", "0", ".", "=", "+"].map((btn) => (
<Button
key={btn}
onClick={() => (btn === "=" ? calculateResult() : appendToExpression(btn))}
className="text-xl p-4"
>
{btn}
</Button>
))}
</div>
<Button onClick={clearExpression} className="w-full">
Clear
</Button>
</CardContent>
</Card>
)
}