「Mery 電卓」の版間の差分
編集の要約なし |
編集の要約なし |
||
| (同じ利用者による、間の2版が非表示) | |||
| 79行目: | 79行目: | ||
precision: 64 // 有効桁数 (デフォルトは64桁) | precision: 64 // 有効桁数 (デフォルトは64桁) | ||
}) | }) | ||
const lowerAndUpperExp = { lowerExp: -7, upperExp: 15 }; | |||
doMultiEdit(main); | doMultiEdit(main); | ||
| 99行目: | 100行目: | ||
try { | try { | ||
var exp_replaced = replaceExpression(exp); | var exp_replaced = replaceExpression(exp); | ||
var result_str = math.format(math.evaluate(exp_replaced), | var result_str = math.format(math.evaluate(exp_replaced), lowerAndUpperExp); | ||
} catch (e) { | } catch (e) { | ||
if (exp_replaced === undefined) { | if (exp_replaced === undefined) { | ||
alert("エラー発生" + "\n------\n" + e | alert("エラー発生" + "\n------\n" + e) | ||
} else { | } else { | ||
alert(" | alert("整形後の数式で計算エラーが発生" + "\n------\n" + e + "\n元の数式: " + exp + "\n整形後の数式: " + exp_replaced) | ||
} | } | ||
return; | return; | ||
| 141行目: | 142行目: | ||
const inputExp = exp.replace(/((?!;)\n)/mg, "; ").replace(/;\s*;/g, ";").replace(/ /g, " ").replace(/;\s*$/, "").trim(); | const inputExp = exp.replace(/((?!;)\n)/mg, "; ").replace(/;\s*;/g, ";").replace(/ /g, " ").replace(/;\s*$/, "").trim(); | ||
if (exp.slice(-1) === "\n") { | if (exp.slice(-1) === "\n") { | ||
sel.Text = inputExp + ' = ' + result_str + '\n'; | sel.Text = inputExp + '\n= ' + result_str + '\n'; | ||
} else { | } else { | ||
sel.Text = '\n' + inputExp + ' = ' + result_str + '\n'; | sel.Text = '\n' + inputExp + '\n= ' + result_str + '\n'; | ||
} | } | ||
sel.CharLeft(); | sel.CharLeft(); | ||
| 158行目: | 159行目: | ||
*/ | */ | ||
function replaceExpression(exp) { | function replaceExpression(exp) { | ||
const exp_replaced = | // コメント扱いの記載を削除 | ||
let exp_replaced = exp.replace(/`[^`]*`/g, " ") | |||
// 他の言語で対応する演算子などはあらかじめエラーにする | |||
const invalidOperators = ["**", "//"]; | |||
invalidOperators.forEach(operator => { | |||
if (exp.includes(operator)) { | |||
throw new Error(`「${operator}」はサポートされていません`); | |||
} | |||
}); | |||
exp_replaced = exp_replaced.trim() | |||
// 先頭の = を削除 | // 先頭の = を削除 | ||
.replace(/^= /, "") | .replace(/^= /, "") | ||
| 175行目: | 187行目: | ||
// 指数形式を退避 | // 指数形式を退避 | ||
.replace(/([0-9]+(?:\.[0-9]+)?[eE][+\-]?[0-9]+)/g, " %%%$1%%% ") | .replace(/([0-9]+(?:\.[0-9]+)?[eE][+\-]?[0-9]+)/g, " %%%$1%%% ") | ||
// | // 変数形式を退避してからドル記号削除 | ||
.replace(/(\$[a-zA-Z][a-zA-Z0-9]*)/g, " %%%$1%%% ") | .replace(/(\$[a-zA-Z][a-zA-Z0-9]*)/g, " %%%$1%%% ") | ||
.replace(/\$/g, "") | |||
.replace(/\ | // 空の括弧を削除 | ||
// | .replace(/\(\s*\)|\[\s*\]|\{\s*\}/g, " ") | ||
.replace(/ | |||
// 関数形式になっていない文字列を削除 | // 関数形式になっていない文字列を削除 | ||
.replace(/( [a-zA-Z][a-zA-Z0-9]*)(%%% |\()?/g, (match, p1, p2) => { | .replace(/([ ,+\-*\/%^&~|<>?])([a-zA-Z][a-zA-Z0-9]*)(%%% |\()?/g, (match, p1, p2, p3) => { | ||
return | return p3 ? p1 + p2 + p3 : p1 + " "; | ||
}) | }) | ||
// `/hour` などの文字列があるとスラッシュだけ残るためスラッシュを削除 | |||
.replace(/\/\s*([,+\-*\/%^&~|<>?/)\];\n]|$)/g, "$1") | |||
// 退避文字を復元 | // 退避文字を復元 | ||
.replace(/ %%%\$([a-zA-Z][a-zA-Z0-9]*)%%% /g, " $1 ") | .replace(/ %%%\$([a-zA-Z][a-zA-Z0-9]*)%%% /g, " $1 ") | ||
| 196行目: | 209行目: | ||
[https://github.com/josdejong/mathjs math.js] | [https://github.com/josdejong/mathjs math.js] | ||
== 更新履歴 == | |||
* 2025-10-19 公開 | |||
* 2025-10-25 対象テキストを数式に整形する処理を調整 | |||
スポンサーリンク