我知道這跟題目一點關係都沒有但是
// const computerName = "NATH";
// const author = "Lily Kensa";
function findCharsPos(content, char, start, step) {
for (let i = start + step; (i < content.length && i >= 0); i += step) {
if (content[i] == char) return i;
}
return -1;
}
function operate(l, r, action) {
let ans;
switch (action) {
case '+':
ans = l + r;
break;
case '-':
ans = l - r;
break;
case '*':
ans = Math.round(l * r * 100) / 100;
break;
case '/':
ans = Math.round(l / r * 100) / 100;
break;
case '%':
ans = l % r;
break;
case '^':
ans = Math.round(Math.pow(l, r) * 100) / 100;
break;
default:
throw "_error_invalid_operator";
}
if (ans < 0) return "(" + ans + ")"
return ans;
}
function isNegateNumbersBrackets(content, index) {
var lb, rb;
switch (content[index]) {
case '(':
lb = index;
rb = findCharsPos(content, ')', index, 1);
break;
case ')':
lb = findCharsPos(content, '(', index, -1);
rb = index;
break;
default:
throw "_not_even_a_bracket";
}
if (content[lb + 1] != '-') return false;
if (isNaN(content.slice(lb + 2, rb))) return false;
return true;
}
function findOperators(content, chars) {
for (let i = 0; i < content.length; ++i) {
for (j in chars) {
if (content[i] == chars[j]) {
if (content[i] == '-'
&& content[i - 1] == '('
&& isNegateNumbersBrackets(content, i - 1))
break;
return i;
}
}
}
return -1;
}
function findLeftNumberReplacePos(content, operatorsPos) {
for (let i = operatorsPos - 1; i >= 0; --i) {
switch (content[i]) {
case '+':
case '-':
if (content[i - 1] == '(')
if (isNegateNumbersBrackets(content, i - 1))
break;
case '*':
case '/':
case '%':
case '^':
return i + 1;
}
}
return 0;
}
function findRightNumberReplacePos(content, operatorsPos) {
for (let i = operatorsPos + 1; i < content.length; ++i) {
switch (content[i]) {
case '+':
case '-':
if (content[i - 1] == '(')
if (isNegateNumbersBrackets(content, i - 1))
break;
case '*':
case '/':
case '%':
case '^':
return i;
}
}
return content.length;
}
function findLeftNumber(content, operatorsPos, replacePos) {
let rb = findCharsPos(content, ')', operatorsPos, -1);
if (rb != -1 && rb >= replacePos) {
let lb = findCharsPos(content, '(', rb, -1);
return parseFloat(content.slice(lb + 1, rb));
}
return parseFloat(content.slice(replacePos, operatorsPos));
}
function findRightNumber(content, operatorsPos, replacePos) {
let lb = findCharsPos(content, '(', operatorsPos, 1);
if (lb != -1 && lb < replacePos) {
let rb = findCharsPos(content, ')', lb, 1);
return parseFloat(content.slice(lb + 1, rb));
}
return parseFloat(content.slice(operatorsPos + 1, replacePos));
}
function findPairedBrackets(content, p) {
let depth = 0;
switch (content[p]) {
case '(':
for (let i = p; i < content.length; ++i) {
if (content[i] == '(') depth += 1
if (content[i] == ')') {
depth -= 1
if (depth == 0) return i;
}
}
return -1;
case ')':
for (let i = p; i >= 0; --i) {
if (content[i] == ')') depth += 1
if (content[i] == '(') {
depth -= 1
if (depth == 0) return i;
}
}
return -1;
default:
throw "_not_even_a_bracket";
}
}
const allOperators = ['*','/','%','^','+','-'];
function calculate(content) {
if (content.startsWith('-')) content = "0" + content;
while (findOperators(content, ['*','/','%','^']) != -1) {
let operatorsPos = findOperators(content, ['*','/','%','^']);
let replaceStart = findLeftNumberReplacePos(content, operatorsPos);
let replaceEnd = findRightNumberReplacePos(content, operatorsPos);
let afterOperated = operate(
findLeftNumber(content, operatorsPos, replaceStart),
findRightNumber(content, operatorsPos, replaceEnd),
content[operatorsPos]
);
content = content.replace(
content.slice(replaceStart, replaceEnd),
afterOperated
);
}
while (findOperators(content, ['+','-']) != -1) {
let operatorsPos = findOperators(content, ['+','-']);
let replaceStart = findLeftNumberReplacePos(content, operatorsPos);
let replaceEnd = findRightNumberReplacePos(content, operatorsPos);
let afterOperated = operate(
findLeftNumber(content, operatorsPos, replaceStart),
findRightNumber(content, operatorsPos, replaceEnd),
content[operatorsPos]
);
content = content.replace(
content.slice(replaceStart, replaceEnd),
afterOperated
);
}
return content;
}
function findMaxDepth(content) {
let depth = 0;
let maxDepth = 0;
for (let i = 0; i < content.length; ++i) {
if (content[i] == '(' && !isNegateNumbersBrackets(content, i)) {
depth += 1;
}
if (content[i] == ')' && !isNegateNumbersBrackets(content, i)) {
depth -= 1;
}
if (depth > maxDepth) maxDepth = depth;
}
return maxDepth;
}
function compute(content) {
const originalContent = content;
content = content.replaceAll(" ","");
let maxDepth = findMaxDepth(content);
while (maxDepth) {
depth = 0;
for (let i = 0; i < content.length; ++i) {
if (content[i] == '(' && !isNegateNumbersBrackets(content, i)) {
depth += 1;
}
if (content[i] == ')' && !isNegateNumbersBrackets(content, i)) {
depth -= 1;
}
if (depth == maxDepth) {
let toBeReplaced = content.slice(i, findPairedBrackets(content, i) + 1);
let innerContent = content.slice(i + 1, findPairedBrackets(content, i));
content = content.replace(toBeReplaced, calculate(innerContent))
break;
}
}
maxDepth = findMaxDepth(content);
}
content = calculate(content)
return content;
}
const { readLine } = require("stdio");
const fs = require("node:fs");
async function main() {
times = parseInt(await readLine());
for (let i = 0; i < times; ++i) {
let input = await readLine();
let output = compute(input);
console.log(output)
}
await readLine({ close: true });
return;
}
main();
|
啊如果你想玩但不會玩就安裝 Node.js 然後
npm i fs |
npm i stdio |
node "<file name>.js" |