小学加减法数学题自动生成

小学加减法题自动生成器

A±B=?

  1. 写入题目写入word文档
    答案写入txt
  2. equation类中有设置AB数取值范围方法
  3. 取去除掉了结果为小数,如果需要小数,删掉一条判断条件即可
#include<iostream>
#include<fstream>
#include<stdlib.h>
#include<stdio.h>
using namespace std;
#pragma warning(disable:4996)
class equation
{
public:
	int numA;
	int numAmin = 1;
	int numAmax = 100;

	int numB;
	int numBmin = 1;
	int numBmax = 10;

	bool SubAdd=true;  // 真为减
	int result;
	equation* next=NULL;

	//************
	// 只计算结果
	//************
	equation(int a,int b,bool Symbol)
	{
		numA = a;
		numB = b;
		SubAdd = Symbol;
		if (Symbol)
		{
			result = numA - numB;
		}
		else
		{
			result = numA + numB;
		}
	}
public :equation(){}


	// 设置AB取值范围
	void SetNumaSize(int min,int max)
	{
		numAmin = min;
		numBmax = max;
	}
	void SetNumbSize(int min, int max)
	{
		numBmin = min;
		numBmax = max;
	}

	bool judge(equation* head, int a, int b, bool Symbol)
	{
		// 带头链表
		equation* temp = head;
		while (temp->next!=NULL)
		{
			// 完全重复
			if (temp->numA == a && temp->numB == b && temp->SubAdd == Symbol)
			{
				return 1;
			}
			temp = temp->next;
		}
		return 0;
	}
	void AddEquation(equation* head)
	{
		int numa, numb, symbol;
		numa = rand() % numAmax;
		numb = rand() % numBmax;
		symbol = rand() % 2;
				// 比最小值要小         ||     a<b,且是a-b		|| 完全重复
		while ((numa<numAmin||numb<numBmin)||(numa<numb&&symbol==1)||judge(head,numa,numb,symbol))
		{
			numa = rand() % numAmax;
			numb = rand() % numBmax;
			symbol = rand() % 2;
		}
		equation* newNode= new equation(numa, numb, symbol);
		newNode->next = head->next;
		head->next = newNode;
	}
};



void printEquation(equation* head)
{
	equation* temp = head->next;
	while (temp!=NULL)
	{
		cout << temp->numA;

		if (temp->SubAdd)
		{
			cout << " - ";
		}
		else
		{
			cout << " + ";
		}
		cout << temp->numB <<" = " << temp->result << endl;
		temp = temp->next;
	}
}


int main()
{
	// 设置AB最大最小数


	int max;
	cout << "请输入出题数" << endl;
	cin >> max;
	equation* math1 = new equation();
	for (int i = 0; i < max; i++)
	{
		math1->AddEquation(math1);
	}
	printEquation(math1);


	//**********************************
	//
	//		 写入word文档
	//
	//**********************************
	ofstream outfile("HomeWork.doc");
	outfile.close();
	equation*temp = math1;

	fstream fs;
	fs.open("HomeWork.doc");
	for (int i = 0; i < max; i++)
	{
		fs <<i+1<<". " << temp->next->numA;
		if (temp->next->SubAdd)
		{
			fs << " - ";
		}
		else
		{
			fs << " + ";
		}
		fs << temp->next->numB << " =" << endl;
		temp = temp->next;
	}
	fs.close();



	//*********************************
	//   将结果写入到txt,批改
	//*********************************
	temp = math1->next;
	outfile.open("result.txt");
	outfile.close();


	FILE* fp=NULL;
	if ((fp = fopen("result.txt", "w+")) != NULL)
	{
		for (int i = 1; i <= max; i++)
		{
			fprintf(fp, "%d. %-5d\t", i, temp->result);
			temp = temp->next;
			if (i%10==0)
			{
				fprintf(fp,"\n");
			}
		}
	}
	fclose(fp);
}

追加一个HTML版本的,我弟肯定爱死我了

<html>
    <head>
        <title>
           数学计算生成
        </title>
    </head>
    <body>
        <script>
            var result=0;
            var count=-1;
            var erro=0;
            var correct=-1;
            var numA=0;
            var numB=0;
            var c='+';
            function addErro()
            {
                var tbody=document.getElementById("tbmain")   
                tr=tbody.insertRow();
                var f;
                if(c==0)
                {
                    f='-';
                }
                else
                {
                    f='+';
                }
                tr.innerHTML= numA+" "+" "+f+" "+""+numB+"= ";
            }
            function getsum()
            {
                count++;
                if(document.getElementById("result").value==result)
                {
                    correct++;
                }
                else
                {
                    erro++;
                    addErro();
                    alert("算错了噢!");
                    
                }
                // 清空输入框
                document.getElementById("result").value="";

                numA=Math.floor(Math.random()*100)+1;
                numB=Math.floor(Math.random()*10+1);
                c=Math.floor(Math.random()*2);
                while((numA<numB&&c==0)||(numA+numB>100))
                {
                    numA=Math.floor(Math.random()*100)+1;
                    numB=Math.floor(Math.random()*10)+1;
                    c=Math.floor(Math.random()*2);
                }
                document.getElementById("NUMA").innerHTML=numA;
                document.getElementById("NUMB").innerHTML=numB;
                if(c==0)
                {
                    document.getElementById("c").innerHTML="-";
                    result=numA-numB;
                }
                else
                {
                    document.getElementById("c").innerHTML="+";
                    result=numA+numB;
                }
                document.getElementById("count").innerHTML="总题数:"+count;
                document.getElementById("erro").innerHTML="算错数:"+erro;
                document.getElementById("correct").innerHTML="算对数:"+correct;
            }
        </script>
    <center>
        <br><br><br><br><br><br>
        <label style ="font-size:26px;" id="NUMA">0</label>
        <label style ="font-size:26px;" id="c">+</label>
        <label style ="font-size:26px;" id="NUMB">0</label>
       <font style ="font-size:26px;"> =</font> 
        <input type="text" style ="font-size:26px;" name="结果" id="result">
        <br><br>
        <input type="button" style ="font-size:26px;" value="提交结果" onclick="getsum()"/></center> 
        <br><br><br>
     <center> <label  id="count">总题数: 0</label></center>
     <center> <label  id="erro">算错数:0</label></center>
     <center> <label  id="correct">算对数:0</label></center>
	<br><br><br><br>
     <center>
		<table width="500" border="1">
            <thead>
                <tr><th>错题列表</th></tr>
            </thead>
		  <tbody id="tbmain"></tbody>
    </table>
</center>
    </body>
</html>
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据引用\[1\]和引用\[2\]的代码,可以生成二十以内的加减法作业。以下是生成的代码示例: ```python import random def generate_add_sub_questions(): questions = \[\] for i in range(50): num1 = random.randint(0, 20) num2 = random.randint(0, 20) if random.choice(\[True, False\]): question = "{}+{}=".format(num1, num2) answer = num1 + num2 else: if num1 >= num2: question = "{}-{}=".format(num1, num2) answer = num1 - num2 else: continue questions.append((question, answer)) return questions questions = generate_add_sub_questions() print("学号:*** 姓名:***") for index, question in enumerate(questions): if (index + 1) % 5 == 0: print(question\[0\]) else: print(question\[0\], end='\t') ``` 这段代码会生成50道二十以内的加减法作业,并按照每行5道的格式输出。每道的格式为`num1+num2=`或`num1-num2=`,其中`num1`和`num2`是随机生成的数字。 #### 引用[.reference_title] - *1* [Python妙用|给小外甥生成10以内加减运算数学作业](https://blog.csdn.net/a55656aq/article/details/122984822)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [python作业——随机生成不重复的20以内加法算式](https://blog.csdn.net/Pan_77777/article/details/125164497)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值