好友
阅读权限10
听众
最后登录1970-1-1
|
本帖最后由 cioceo 于 2024-12-17 20:25 编辑
我在【终版】小学数学出题器的基础上修改了下自己用,分享给大家,括号功能没弄好,将就用
[HTML] 纯文本查看 复制代码 <!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>小学生数学题生成器</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
margin: 0;
display: block;
flex-direction: column;
align-items: center;
justify-content: center;
}
#options {
display: block;
margin: 8px auto;
background-color: #fff;
border-radius: 5px;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
padding: 12px;
box-sizing: border-box;
width: 480px;
line-height: 30px;
}
label {
margin-right: 10px;
margin-bottom: 10px;
font-size: 16px;
}
button {
padding: 5px;
background-color: #4caf50;
color: #fff;
border: none;
border-radius: 5px;
cursor: pointer;
}
#questions {
display: flex;
flex-wrap: wrap;
margin-top: 20px;
}
.question {
flex: 1 0 18%;
box-sizing: border-box;
padding: 8px;
background-color: #fff;
border-radius: 5px;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
margin: 8px;
white-space: nowrap;
font-size: 20px;
}
.answer {
display: none;
font-size: 16px;
}
#printHeader {
display: none;
margin-bottom: 20px;
}
@media print {
#printHeader {
display: block;
text-align: center;
margin-bottom: 30px;
}
body {
margin: 30px;
}
#questions {
display: flex;
flex-wrap: wrap;
}
.question {
page-break-inside: avoid;
white-space: nowrap;
flex: 1 0 18%;
}
@page {
size: A4;
margin: 25mm 10mm 25mm 10mm;
}
}
div#printHeader {
text-align: center;
margin-bottom: 15px;
}
</style>
</head>
<body>
<div class="hd1" style="text-align:center;font-size:30px;background-color: #4CAF50;min-height: 50px;padding-top: 5px;">
<font>小学生数学题生成器</font></div>
<div id="options">运算符号:
<label>
<input type="checkbox" id="additionCheckbox" checked> 加法
</label>
<label>
<input type="checkbox" id="subtractionCheckbox" checked> 减法
</label>
<label>
<input type="checkbox" id="multiplicationCheckbox"> 乘法
</label>
<label>
<input type="checkbox" id="divisionCheckbox"> 除法
</label>
<label>
<input type="checkbox" id="bracketCheckbox"> 括号
</label>
<br>
<label>
运算项数:<input type="number" id="numOfDigits" value="2" min="1" style="width: 45px;">
</label>
<label>
小数位数:<input type="number" id="decimalPlaces" value="0" min="0" style="width: 40px;">
</label>
<label>
出题数量:<input type="number" id="numOfQuestions" value="50" min="1" style="width: 50px;">
</label>
<br>
<label>
数字范围:
<label><input type="number" id="minRange" value="0" min="0" style="width: 40px;"></label>-
<label><input type="number" id="maxRange" value="7" min="1" style="width: 40px;"></label>
</label>
<button>生成题目</button>
<button>一键打印</button>
<button>显/隐答案</button>
</div>
<div id="questions"></div>
<script>
function generateQuestions() {
const addition = document.getElementById("additionCheckbox").checked;
const subtraction = document.getElementById("subtractionCheckbox").checked;
const multiplication = document.getElementById("multiplicationCheckbox").checked;
const division = document.getElementById("divisionCheckbox").checked;
const useBrackets = document.getElementById("bracketCheckbox").checked;
const numOfDigits = document.getElementById("numOfDigits").value;
const decimalPlaces = document.getElementById("decimalPlaces").value;
const minRange = parseInt(document.getElementById("minRange").value);
const maxRange = parseInt(document.getElementById("maxRange").value);
const numOfQuestions = document.getElementById("numOfQuestions").value;
const questionsContainer = document.getElementById("questions");
questionsContainer.innerHTML = "";
for (let i = 0; i < numOfQuestions; i++) {
let validQuestion = false;
let questionText, answerText;
while (!validQuestion) {
const operators = getRandomOperators(addition, subtraction, multiplication, division, numOfDigits);
const numbers = generateNumbers(numOfDigits, decimalPlaces, minRange, maxRange, operators);
questionText = generateQuestionText(numbers, operators, decimalPlaces, useBrackets);
answerText = calculateAnswer(numbers, operators, decimalPlaces).toFixed(decimalPlaces);
if (!containsNegativeNumber(questionText) && answerText >= 0) {
validQuestion = true;
}
}
const questionDiv = document.createElement("div");
questionDiv.classList.add("question");
questionDiv.innerHTML = `<span>${questionText}</span><span class="answer">${parseFloat(answerText)}</span>`;
questionsContainer.appendChild(questionDiv);
}
}
function getRandomOperators(addition, subtraction, multiplication, division, numOfDigits) {
const availableOperators = [];
if (addition) availableOperators.push('+');
if (subtraction) availableOperators.push('-');
if (multiplication && numOfDigits >= 2) availableOperators.push('*');
if (division && numOfDigits >= 2) availableOperators.push('/');
const selectedOperators = [];
for (let i = 0; i < numOfDigits - 1; i++) {
const randomOperator = availableOperators[Math.floor(Math.random() * availableOperators.length)];
selectedOperators.push(randomOperator);
}
return selectedOperators;
}
function generateQuestionText(numbers, operators, decimalPlaces, useBrackets) {
let questionText = numbers[0].toString();
for (let i = 0; i < operators.length; i++) {
const operator = operators[i];
const num = parseFloat(numbers[i + 1]).toFixed(decimalPlaces);
questionText += ` ${operator.replace('*', 'x').replace('/', '÷')} ${num}`;
}
questionText += ' =';
if (useBrackets && operators.length > 1) {
const bracketPosition = Math.floor(Math.random() * operators.length);
const firstPart = questionText.split(' ').slice(0, bracketPosition * 2 + 1).join(' ');
const secondPart = questionText.split(' ').slice(bracketPosition * 2 + 1).join(' ');
const firstPartNumbers = firstPart.match(/-?\d+(\.\d+)?/g);
const firstPartOperators = firstPart.match(/[+\-*/]/g);
if (firstPartNumbers && firstPartNumbers.length === 2 && firstPartOperators && firstPartOperators.length === 1) {
questionText = `(${firstPart}) ${secondPart}`;
}
}
return questionText;
}
function generateNumbers(numOfDigits, decimalPlaces, minRange, maxRange, operators) {
const randomNumber = () => {
return (Math.random() * (maxRange - minRange) + minRange).toFixed(decimalPlaces);
};
const numbers = [];
for (let i = 0; i < numOfDigits; i++) {
let num;
if (i > 0 && (operators[i - 1] === '/' || operators[i - 1] === '÷')) {
do {
num = randomNumber();
} while (num === '0');
} else {
num = randomNumber();
}
numbers.push(num);
}
return numbers;
}
function calculateAnswer(numbers, operators, decimalPlaces) {
const calculateMulDiv = (nums, ops) => {
for (let i = 0; i < ops.length; i++) {
if (ops[i] === '*' || ops[i] === '/') {
const result = ops[i] === '*' ? nums[i] * nums[i + 1] : nums[i] / nums[i + 1];
nums.splice(i, 2, result);
ops.splice(i, 1);
i--;
}
}
};
const nums = numbers.map(num => parseFloat(num));
const ops = operators.map(op => op);
calculateMulDiv(nums, ops);
let result = nums[0];
for (let i = 0; i < ops.length; i++) {
const num = nums[i + 1];
const operator = ops[i];
switch (operator) {
case '+':
result += num;
break;
case '-':
result -= num;
break;
default:
break;
}
}
return parseFloat(result.toFixed(decimalPlaces));
}
function containsNegativeNumber(questionText) {
const parts = questionText.split(' ');
for (let i = 0; i < parts.length; i++) {
if (parseFloat(parts[i]) < 0) {
return true;
}
}
return false;
}
function printQuestions() {
const printWindow = window.open('', '_blank');
const printContent = document.getElementById("questions").innerHTML;
printWindow.document.write(`
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>打印题目</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 12px;
}
#questions {
display: flex;
flex-wrap: wrap;
}
.question {
flex: 1 0 14%;
box-sizing: border-box;
margin: 5px;
white-space: nowrap;
font-size: 21px;
margin: 11px;
}
.answer {
display: none;
font-size: 16px;
}
</style>
</head>
<body>
<div id="printHeader" style="text-align: center;margin-bottom: 18px;">
<div>姓名:__________ 日期:______年_____月_____日 答对:________题</div>
</div>
<div id="questions">${printContent}</div>
</body>
</html>
`);
printWindow.document.close();
printWindow.print();
}
function toggleAnswers() {
const answers = document.querySelectorAll('.answer');
answers.forEach(answer => {
answer.style.display = (answer.style.display === 'none' || answer.style.display === '') ? 'inline' : 'none';
});
}
window.onload = function() {
generateQuestions();
document.activeElement.blur();
};
</script>
</body>
</html>
|
-
|