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

小学加减法题自动生成器

A±B=?

  1. 写入题目写入word文档
    答案写入txt
  2. equation类中有设置AB数取值范围方法
  3. 取去除掉了结果为小数,如果需要小数,删掉一条判断条件即可
#include
#include
#include
#include
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
		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">0label>
        <label style ="font-size:26px;" id="c">+label>
        <label style ="font-size:26px;" id="NUMB">0label>
       <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">总题数: 0label>center>
     <center> <label  id="erro">算错数:0label>center>
     <center> <label  id="correct">算对数:0label>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>

你可能感兴趣的:(有趣的cpp,c++,开发语言)