import React, { useState } from 'react'; import { Calculator, Plus, Minus, X, Divide, Equal } from 'lucide-react'; const MathSolver = () => { const [expression, setExpression] = useState(''); const [result, setResult] = useState(''); const [history, setHistory] = useState([]); const handleSolve = () => { try { if (!expression.trim()) { alert('অনুগ্রহ করে একটি গাণিতিক সমস্যা লিখুন'); return; } // Basic math expression evaluation (safe evaluation) const sanitizedExpression = expression.replace(/[^0-9+\-*/().\s]/g, ''); const calculatedResult = Function('"use strict"; return (' + sanitizedExpression + ')')(); if (isNaN(calculatedResult) || !isFinite(calculatedResult)) { throw new Error('Invalid calculation'); } const roundedResult = Math.round(calculatedResult * 100000000) / 100000000; setResult(roundedResult.toString()); // Add to history setHistory(prev => [...prev, { expression: expression, result: roundedResult }].slice(-5)); } catch (error) { setResult('ত্রুটি! অবৈধ গাণিতিক সমস্যা'); } }; const handleClear = () => { setExpression(''); setResult(''); }; const handleButtonClick = (value) => { if (value === '=') { handleSolve(); } else if (value === 'C') { handleClear(); } else if (value === '⌫') { setExpression(prev => prev.slice(0, -1)); } else { setExpression(prev => prev + value); } }; const buttons = [ ['C', '⌫', '(', ')'], ['7', '8', '9', '/'], ['4', '5', '6', '*'], ['1', '2', '3', '-'], ['0', '.', '+', '='] ]; return (
{/* Header */}
ম্যাথ সল্ভার
{/* Display */}
setExpression(e.target.value)} placeholder="গাণিতিক সমস্যা লিখুন (যেমন: 5+3*2)" className="w-full text-lg text-gray-800 bg-transparent border-none outline-none" />
{result && (
ফলাফল: {result}
)}
{/* Calculator Buttons */}
{buttons.flat().map((btn, index) => (
handleButtonClick(btn)} className={`p-4 rounded-lg font-bold text-lg transition-all duration-200 ${ btn === '=' ? 'bg-gradient-to-r from-green-500 to-emerald-500 text-white hover:from-green-600 hover:to-emerald-600 transform hover:scale-105' : btn === 'C' || btn === '⌫' ? 'bg-gradient-to-r from-red-500 to-pink-500 text-white hover:from-red-600 hover:to-pink-600' : ['+', '-', '*', '/', '(', ')'].includes(btn) ? 'bg-gradient-to-r from-blue-500 to-indigo-500 text-white hover:from-blue-600 hover:to-indigo-600' : 'bg-gradient-to-r from-gray-100 to-gray-200 text-gray-800 hover:from-gray-200 hover:to-gray-300' } shadow-md hover:shadow-lg active:scale-95`} > {btn}
))}
{/* Manual Solve Button */}
🧮 সমাধান করুন
🗑️ পরিষ্কার করুন
{/* History */} {history.length > 0 && (
📋 সাম্প্রতিক গণনা
{history.slice().reverse().map((item, index) => (
{item.expression} =
{item.result}
))}
)} {/* Instructions */}
📝 ব্যবহারবিধি:
• সাধারণ গণনা: 5+3, 10-4, 6*7, 15/3
• জটিল গণনা: (5+3)*2, 10+5*3
• দশমিক সংখ্যা: 3.14+2.86
• বন্ধনী ব্যবহার করুন গণনার ক্রম ঠিক রাখতে
); }; export default MathSolver;