生成小学二年级数学加减运算练习题的代码

最近想找点题目给娃做,然后发现Baidu搜索到的大部分都是收费的,于是自己编写代码进行生成。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Management;
using System.IO;

namespace _100Calc
{
    class Program
    {
        //将List转换为TXT文件
        static public void WriteListToTextFile(List<string> list, string txtFile)
        {
            //创建一个文件流,用以写入或者创建一个StreamWriter 
            FileStream fs = new FileStream(txtFile, FileMode.OpenOrCreate, FileAccess.Write);
            StreamWriter sw = new StreamWriter(fs);
            sw.Flush();
            // 使用StreamWriter来往文件中写入内容 
            sw.BaseStream.Seek(0, SeekOrigin.Begin);
            for (int i = 0; i < list.Count; i++) sw.WriteLine(list[i]);
            //关闭此文件t 
            sw.Flush();
            sw.Close();
            fs.Close();
        }


        //读取文本文件转换为List 
        static public List<string> ReadTextFileToList(string fileName)
        {
            FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
            List<string> list = new List<string>();
            StreamReader sr = new StreamReader(fs);
            //使用StreamReader类来读取文件 
            sr.BaseStream.Seek(0, SeekOrigin.Begin);
            // 从数据流中读取每一行,直到文件的最后一行
            string tmp = sr.ReadLine();
            while (tmp != null)
            {
                list.Add(tmp);
                tmp = sr.ReadLine();
            }
            //关闭此StreamReader对象 
            sr.Close();
            fs.Close();
            return list;
        }

        static void Main(string[] args)
        {
            List<string> Add100 = new List<string>();
            
            for (int i = 10; i < 100; i++)
                for (int j = 10; j < 100; j++) {
                    //Console.WriteLine("{0}+{1}=", i, j);
                    Add100.Add(i.ToString() + "+" + j.ToString() + "=");
                }
            //WriteListToTextFile(Add100,"100Add.txt");

            List<string> Sub100 = new List<string>();
            for (int i = 99; i >10; i--)
                for (int j = i-1; j >10; j--)
                {
                    //Console.WriteLine("{0}-{1}=", i, j);
                    Sub100.Add(i.ToString() + "-" + j.ToString() + "=");
                }

            //A - B + C 形式的,特别注意 A-B>0 
            List<string> ThrCalc1 = new List<string>();
            for (int i = 99; i > 10; i--)
                for (int j = i - 1; j > 10; j--)
                    for (int k = 10; k<100; k++)
                    {
                        //Console.WriteLine("{0}-{1}+{2}=", i, j,k);
                        ThrCalc1.Add(i.ToString() + "-" + j.ToString() + "+" + k.ToString() + "=");
                }

            //A - B - C 形式的,特别注意 A-B-C>0  
            List<string> ThrCalc2 = new List<string>();
            for (int i = 99; i > 10; i--)
                for (int j = i - 1; j > 10; j--)
                    for (int k = 10; k < 100; k++)
                        {
                            if (i-j-k>0)
                            {
                                //Console.WriteLine("{0}-{1}+{2}=", i, j,k);
                                ThrCalc2.Add(i.ToString() + "-" + j.ToString() + "-" + k.ToString() + "=");
                            }
                        
                    }

            Random rnd = new Random();
            List<string> timu = new List<string>();

            // 生成 180 套题目
            for (int i = 0; i < 160; i++) {
                
                for (int q1 = 0; q1 < 21; q1++) {
                    int v = rnd.Next(Add100.Count);
                    timu.Add(Add100[v]);
                    Add100.RemoveAt(v);
                }
                for (int q2 = 0; q2 < 22; q2++)
                {
                    int v = rnd.Next(Sub100.Count);
                    timu.Add(Sub100[v]);
                    Sub100.RemoveAt(v);
                }
                for (int q3 = 0; q3 < 7; q3++)
                {
                    int v = rnd.Next(ThrCalc1.Count);
                    timu.Add(ThrCalc1[v]);
                    ThrCalc1.RemoveAt(v);
                }
                for (int q4 = 0; q4 < 7; q4++)
                {
                    int v = rnd.Next(ThrCalc2.Count);
                    timu.Add(ThrCalc2[v]);
                    ThrCalc2.RemoveAt(v);
                }

                
                // 打乱排列顺序
                string s;
                for (int j = 0; j < timu.Count; j++) {
                    int v = rnd.Next(timu.Count);
                    s = timu[v];
                    timu[v] = timu[j];
                    timu[j] = s;
                }
                
                // 输出
                for (int j = 0; j < timu.Count/3; j++)
                {
                    Console.WriteLine("{0,-10}\t\t\t{1,-10}\t\t\t{2,-10}", timu[j*3], timu[j*3+1], timu[j*3+2]);
                }
                //Console.WriteLine("Data:                      Score:        ");
                timu.Clear();
            }

            Console.WriteLine("{0}  {1}  {2} {3}", Add100.Count,Sub100.Count, ThrCalc1.Count, ThrCalc2.Count);
            Console.ReadLine();
        }
    }
}

每一页21道2位数加法,22道两位数减法(没有负数),还有14到3个两位数加减运算。一共160页。直接拷贝到 Word文章后,生成一份 PDF如下,有需要的朋友可以下载打印。

增加一个3个数字的加减乘运算

一年级二十以内加减题目

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注