吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1767|回复: 39
收起左侧

[其他原创] 小学数学题目生成器

  [复制链接]
haixiang1214 发表于 2024-11-7 12:09
原帖:https://www.52pojie.cn/forum.php?mod=viewthread&tid=1842637&highlight=%D0%A1%D1%A7%CA%FD%D1%A7
改进为
1、优化排版
2、设置每页列数
3、实现打印功能
[HTML] 纯文本查看 复制代码
<html>
<head>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
        }
        h1 {
            text-align: center;
        }
        .container {
            display: flex;
            flex-direction: column;
            align-items: center;
        }
        .options {
            display: flex;
            line-height: 25px;
        }
        .options label {
            display: block;
        }
        .questions {
            display: grid;
            gap: 20px;
            width: 80%;
            padding: 10px;
        }
        .question-item {
            font-size: 24px;
            font-weight: bold;
            text-align: left;
        }
        .print {
            margin-top: 20px;
        }
        .options div {
            margin-right: 20px;
            background-color: #8080801a;
        }
    </style>
</head>
<body>
    <h1>小学数学出题界面</h1>
    <div class="container">
        <div class="options">
            <div>
                <p>请选择运算符:</p>
                <label><input type="checkbox" id="add" checked> 加法</label>
                <label><input type="checkbox" id="sub"> 减法</label>
                <label><input type="checkbox" id="mul"> 乘法</label>
                <label><input type="checkbox" id="div"> 除法</label>
            </div>
            <div>
                <p>请选择出题数量:</p>
                <input type="number" id="num" min="1" max="10" value="5">
                <button id="gen">生成题目</button>
            </div>
            <div>
                <p>请选择数字范围:</p>
                <label>最小值:<input type="number" id="min" min="1" max="9" value="1"></label>
                <label>最大值:<input type="number" id="max" min="2" max="10" value="10"></label>
            </div>
            <div>
                <p>设置题目列数:</p>
                <input type="number" id="columns" min="1" max="10" value="3">
            </div>
        </div>
        <hr>
        <button class="print">打印题目</button>
        <div class="questions"></div>
    </div>
    <script>
        var add = document.getElementById("add");
        var sub = document.getElementById("sub");
        var mul = document.getElementById("mul");
        var div = document.getElementById("div");
        var num = document.getElementById("num");
        var min = document.getElementById("min");
        var max = document.getElementById("max");
        var columns = document.getElementById("columns");
        var gen = document.getElementById("gen");
        var questions = document.getElementsByClassName("questions")[0];

        var operators = ["+", "-", "×", "÷"];
        var functions = [
            function(a, b) { return a + b; },
            function(a, b) { return a - b; },
            function(a, b) { return a * b; },
            function(a, b) { return a / b; }
        ];

        function randomInt(min, max) {
            return Math.floor(Math.random() * (max - min + 1)) + min;
        }

        function generateQuestions() {
            questions.innerHTML = "";
            questions.style.gridTemplateColumns = `repeat(${columns.value}, 1fr)`;

            var selectedOperators = [];
            if (add.checked) selectedOperators.push(0);
            if (sub.checked) selectedOperators.push(1);
            if (mul.checked) selectedOperators.push(2);
            if (div.checked) selectedOperators.push(3);

            var questionNum = parseInt(num.value);
            var minValue = parseInt(min.value);
            var maxValue = parseInt(max.value);

            if (selectedOperators.length == 0) {
                alert("请至少选择一个运算符!");
                return;
            }

            if (minValue >= maxValue) {
                alert("最小值应该小于最大值!");
                return;
            }

            for (var i = 0; i < questionNum; i++) {
                var opIndex = randomInt(0, selectedOperators.length - 1);
                var op = operators[selectedOperators[opIndex]];
                var func = functions[selectedOperators[opIndex]];

                var a, b, result;
                do {
                    a = randomInt(minValue, maxValue);
                    b = randomInt(minValue, maxValue);
                    result = func(a, b);
                } while (!Number.isInteger(result) || result <= 0);

                var questionItem = document.createElement("div");
                questionItem.className = "question-item";
                questionItem.textContent = a + " " + op + " " + b + " = ";
                questions.appendChild(questionItem);
            }
        }

        gen.addEventListener("click", generateQuestions);
    </script>
</body>
</html>

免费评分

参与人数 5吾爱币 +8 热心值 +4 收起 理由
doudou1789 + 1 厉害
Missyou2048 + 1 热心回复!
QYC1983 + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!
sannian918 + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!
苏紫方璇 + 7 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

dingqh 发表于 2024-11-7 16:00
感谢分享
可以看看这个
https://www.52pojie.cn/thread-1845482-1-1.html
(出处: 吾爱破解论坛)

免费评分

参与人数 1吾爱币 +1 热心值 +1 收起 理由
hehehero + 1 + 1 牛逼克拉斯

查看全部评分

cioceo 发表于 2024-12-17 20:19
本帖最后由 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>
微信截图_20241217202220.png
 楼主| haixiang1214 发表于 2024-11-7 12:11
使用方法:文本编辑器编辑-修改后缀为HTML-双击打开-出题并打印-把娃抓过来做题

免费评分

参与人数 1吾爱币 +1 热心值 +1 收起 理由
greatpeng + 1 + 1 我很赞同!

查看全部评分

cqfyaaa 发表于 2024-11-7 16:15
小学生:我谢谢你哦
李亲顾 发表于 2024-11-7 16:22
小学生的噩梦来了,哈哈、
吖远zzy 发表于 2024-11-7 16:39
感谢分享,小学生&家长会谢谢你
vzzwcom 发表于 2024-11-7 16:48
小学生&家长会谢谢你
skzhaixing 发表于 2024-11-7 17:39
我也做了一个  [img]!%5Bimage%5D()[/img][img]!%5Bimage%5D()[/img][img]!%5Bimage%5D()[/img]
ltgb 发表于 2024-11-7 18:19
不到150行的代码咋写出来的 厉害
逐雅斋 发表于 2024-11-7 18:22
这个不错,感谢分享!
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2025-1-7 19:55

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表