Spaces:
Running
Running
File size: 3,323 Bytes
91111e4 | 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 | // ============================================================
// Mathematical Evaluation Adapter — Tính toán biểu thức toán học
// Phụ thuộc: currentLang, _adapterPath
// ============================================================
/**
* Trích xuất và tính toán biểu thức toán học từ chuỗi đầu vào.
* Không sử dụng eval() — dùng regex + switch/case.
*/
function parseMathExpression(input, lang) {
if (typeof input !== 'string' || input.trim().length === 0) {
return { error: lang === 'en' ? 'Invalid expression.' : lang === 'ja' ? '無効な式です。' : 'Biểu thức không hợp lệ.' };
}
var expr = input.trim();
if (lang !== 'ja') expr = expr.toLowerCase();
if (lang === 'vi') {
expr = expr.replace(/cộng/g, '+').replace(/trừ/g, '-').replace(/nhân/g, '*').replace(/chia/g, '/');
} else if (lang === 'en') {
expr = expr.replace(/divided\s+by/g, '/').replace(/plus/g, '+').replace(/minus/g, '-').replace(/times/g, '*');
} else if (lang === 'ja') {
expr = expr.replace(/足す/g, '+').replace(/引く/g, '-').replace(/掛ける/g, '*').replace(/割る/g, '/');
}
expr = expr.replace(/×/g, '*').replace(/÷/g, '/');
var match = expr.match(/(-?\d+(?:\.\d+)?)\s*([+\-*/])\s*(-?\d+(?:\.\d+)?)/);
if (!match) {
return { error: lang === 'en' ? 'Cannot parse the mathematical expression.' : lang === 'ja' ? '数式を解析できませんでした。' : 'Không thể phân tích biểu thức toán học.' };
}
var a = parseFloat(match[1]);
var operator = match[2];
var b = parseFloat(match[3]);
var result;
switch (operator) {
case '+': result = a + b; break;
case '-': result = a - b; break;
case '*': result = a * b; break;
case '/':
if (b === 0) return { error: lang === 'en' ? 'Cannot divide by zero.' : lang === 'ja' ? 'ゼロで割ることはできません。' : 'Không thể chia cho 0.' };
result = a / b; break;
default:
return { error: lang === 'en' ? 'Unsupported operator.' : lang === 'ja' ? 'サポートされていない演算子です。' : 'Phép tính không được hỗ trợ.' };
}
return { result: result };
}
function mathematicalEvaluationAdapter(rs, args) {
_adapterPath.push('mathematical_evaluation');
var input = (args || []).join(' ').trim();
var lang = currentLang || 'vi';
if (input.length === 0) {
return lang === 'en' ? 'Please provide a mathematical expression.'
: lang === 'ja' ? '数式を入力してください。'
: 'Vui lòng nhập biểu thức toán học.';
}
var parsed = parseMathExpression(input, lang);
if (parsed.error) return parsed.error;
var resultStr = Number.isInteger(parsed.result) ? String(parsed.result) : parsed.result.toFixed(2).replace(/\.?0+$/, '');
if (lang === 'en') return 'Result: ' + resultStr;
if (lang === 'ja') return '結果: ' + resultStr;
return 'Kết quả: ' + resultStr;
}
// Node/test: export to globalThis
if (typeof module !== 'undefined' && module.exports) {
globalThis.parseMathExpression = parseMathExpression;
globalThis.mathematicalEvaluationAdapter = mathematicalEvaluationAdapter;
}
|