无脑测评
2026-11-01 19:41:05
发布于:河南
主角:呵啊~好无聊,干点什么呢?
…………10min…………………思考中
噢噢噢!开工~
// ask.js
const fs = require('fs');
const readline = require('readline');
const path = require('path');
const GREEN = '\x1b[32m';
const RED = '\x1b[31m';
const RESET = '\x1b[0m';
const filePath = path.join(__dirname, 'question.txt');
if (!fs.existsSync(filePath)) {
console.error(RED + `未找到文件: ${filePath}` + RESET);
process.exit(1);
}
const lines = fs
.readFileSync(filePath, 'utf-8')
.split(/\r?\n/)
.map((l) => l.trim())
.filter((l) => l.length > 0);
if (lines.length === 0) {
console.log('question.txt 中没有题目。');
process.exit(0);
}
function isSafeExpression(expr) {
return /^[\d\s+\-*/().]+$/.test(expr);
}
function evaluateExpression(expr) {
if (!isSafeExpression(expr)) return null;
try {
const result = Function(`"use strict"; return (${expr});`)();
return typeof result === 'number' && isFinite(result) ? result : null;
} catch {
return null;
}
}
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
let index = 0;
let correctCount = 0;
function askNext() {
if (index >= lines.length) {
rl.close();
const msg = `\n全部完成:共 ${lines.length} 题,答对 ${correctCount} 题。`;
console.log((correctCount === lines.length ? GREEN : RED) + msg + RESET);
return;
}
const question = lines[index];
const expected = evaluateExpression(question);
rl.question(`第 ${index + 1} 题:${question} = `, (answer) => {
const userVal = parseFloat(answer.trim());
const ok =
expected !== null &&
!isNaN(userVal) &&
Math.abs(userVal - expected) < 1e-9;
if (ok) {
console.log(GREEN + '√ 答对了' + RESET);
correctCount++;
} else {
console.log(RED + '× 答错了' + RESET);
if (expected !== null) {
console.log(` 正确答案是: ${expected}`);
} else {
console.log(` (题目格式无法解析,请检查 question.txt 第 ${index + 1} 行)`);
}
}
index++;
askNext();
});
}
askNext();
使用步骤
搞定ask.js
来到桌面,右键,新建,📂文件夹,命名为question
双击进入,新建,文本文档,进入命名模式,全选名称(带.txt,先开显示扩展名),设置为ask.js,上面的代码放入,再次新建文本文档,命名为question.txt,question.txt是题目,格式如下
3 + 5
8 - 2
2 * 4
9 / 3
7 + 1
6 - 4
5 * 2
8 / 4
1 + 9
10 - 3
3 * 3
6 / 2
4 + 6
9 - 5
2 * 5
10 / 5
7 + 2
8 - 6
4 * 2
9 / 9
5 + 5
10 - 7
3 * 2
8 / 2
6 + 3
7 - 1
5 * 1
4 / 2
2 + 7
9 - 3
6 * 1
6 / 3
8 + 1
10 - 4
2 * 3
10 / 2
1 + 4
5 - 2
7 * 1
8 / 8
9 + 1
6 - 5
4 * 1
9 / 1
3 + 4
10 - 6
5 * 0
4 / 4
6 + 2
7 - 3
3 * 1
2 / 1
8 + 2
9 - 7
1 * 6
10 / 10
5 + 3
8 - 5
2 * 2
6 / 1
7 + 3
10 - 2
4 * 0
9 / 3
1 + 8
6 - 3
5 * 1
8 / 4
2 + 5
9 - 4
3 * 3
10 / 5
6 + 4
7 - 6
2 * 4
4 / 1
9 + 0
10 - 8
1 * 9
6 / 6
4 + 4
5 - 1
3 * 2
8 / 2
7 + 0
9 - 8
2 * 1
10 / 1
1 + 6
8 - 7
6 * 0
9 / 9
3 + 6
10 - 9
5 * 2
4 / 2
2 + 8
7 - 4
1 * 5
6 / 2
点击最上方类似C://Users/用户名/Desktop/question的那个,全选,输cmd
启动程序
输入:node ask.js
稍候~
启动!
这里空空如也














有帮助,赞一个