四则运算题出题程序代码及分析

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了四则运算题出题程序代码及分析相关的知识,希望对你有一定的参考价值。

题目:

1、题目避免重复

2、可定制(数量/打印方式)

3、可以控制下列参数

     是否有乘除法

     是否有括号(最多可以支持十个参数参与计算)

     数值范围

     加减有无分数

     除法有无余数

分析:

1、题目避免分析

     保证首个参数不同

2、可定制

  根据用户选择进行输出方式

3、是否有括号

     保证右括号始终在最后一个数的后面

     左括号可以在前面任意位置插入

4、有无负数

     减号前一个数小于后一个则前后交换

5、有无余数

     若有余数,则除数重新生成,直到无余数为止

源代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace siz
{
    class Program
    {
        static Random rnd = new Random();
        static void Main(string[] args)
        {
            String filepath;
            String path;
            int num_min,num_max;//数据范围的上限和下限
            int num_topic;//题目个数
            int num_number;//表达式所含数字个数
            int num_milde;
            int num_output;
            String word_ch;//判断乘除
            String word_ys="N";//判断余数
            String word_k="N";//判断括号
            String word_fs="N";//判断有无负数
           

            Console.WriteLine("请输入题目个数:");
            num_topic = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("请输入表达式所含数字个数:");
            num_number = Convert.ToInt32(Console.ReadLine());

             Console.WriteLine("请输入范围下限:");
            num_min = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("请输入范围上限:");
            num_max=Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("是否有乘除(Y/N):");
            word_ch=Console.ReadLine();
            
            if(num_number==2)
            {
                if(word_ch=="Y")
                {
                    Console.WriteLine("是否有余数(Y/N):");
                    word_ys=Console.ReadLine();
                }
                if(num_min>=0)
                {
                    Console.WriteLine("是否有负数(Y/N):");
                    word_fs=Console.ReadLine();
                }
                
            }
            else
            {
                Console.WriteLine("是否有括号(Y/N):");
                word_k=Console.ReadLine();
            }
           
            int[,] Data = new int[num_topic,num_number];//保存数据

            int[,] Operator=new int[num_topic,num_number];//保存运算符

            int[] Data_first = new int[num_topic];//保存每个表达式的首个字符

            int[,] kuohao=new int[num_topic,num_number];
            if (word_ch == "Y")
            {
                for (int ii = 0; ii < num_topic; ii++)
                {
                    for (int jj = 0; jj < num_number - 1; jj++)
                    {
                        Operator[ii, jj] = rnd.Next(1, 5);
                    }
                    Operator[ii, num_number - 1] = 5;
                }
                
            }
            else
            {
                for (int i = 0; i < num_topic; i++)
                {
                    for (int j = 0; j < num_number - 1; j++)
                    {
                            Operator[i, j] = rnd.Next(1, 3);
                    }
                    Operator[i, num_number - 1] = 5;
                }

            }
            

            Data_first[0] = rnd.Next(num_min, num_max);//以第一个操作数的不同来使表达式不会重复

            for (int s = 1; s < num_topic;s++ )
            {
                Data_first[s] = rnd.Next(num_min, num_max);
                for (int h = 0; h < s-1; h++)
                {
                    if (Data_first[s] == Data_first[h])
                    {
                        s--;
                    }
                }
            }
            for (int x = 0; x < num_topic; x++)
            {
                Data[x, 0] = Data_first[x];
                for (int y = 1; y < num_number; y++)
                {
                    if (Operator[x,y-1] == 4)
                    {
                        for (; ; )
                        {
                            Data[x,y] = rnd.Next(num_min, num_max);
                            if (Data[x,y] != 0)
                            {
                                break;
                            }
                        }
                    }
                    else
                    {
                        Data[x, y] = rnd.Next(num_min, num_max);
                    }
                }
            }
            if (word_fs == "N")
            {
                for (int i1 = 0; i1 < num_topic; i1++)
                {
                    if (Operator[i1, 0] == 2&&Data[i1,0]<Data[i1,1])
                    {
                        num_milde = Data[i1, 1];
                        Data[i1, 1] = Data[i1, 0];
                        Data[i1, 0] = num_milde;
                        
                    }
                }
            }
            
            if (word_ch == "Y")
            {
                
                if (word_ys == "N")
                {
                    
                    for (int i2 = 0; i2 < num_topic; i2++)
                    {
                       
                        if (Operator[i2, 0] == 4)
                        {
                            
                            num_milde = Data[i2, 0] % Data[i2, 1];
                            while (num_milde != 0)
                            {
                                Data[i2, 1] = rnd.Next(1, Data[i2, 0]);
                                num_milde = Data[i2, 0] % Data[i2, 1];
                            }
                        }
                    }
                }
                
            }
             
            if (word_k == "Y")
            {
                for (int j1 = 0; j1 < num_topic; j1++)
                {
                    for (int j2 = 0; j2 < num_number-2; j2++)
                    {
                        kuohao[j1,j2] = rnd.Next(0, 2);
                    }
                    kuohao[j1,num_number-2]=0;
                    kuohao[j1,num_number-1]=0;
                }
            }
            Console.WriteLine("请输入输出方式:\n1、控制台输出。2、输出到文本");
            num_output = Convert.ToInt32(Console.ReadLine());
            if (num_output == 1)
            {
                for (int x1 = 0; x1 < num_topic; x1++)
                {
                    for (int x2 = 0; x2 < num_number; x2++)
                    {
                        if (kuohao[x1,x2] == 1)
                        {
                            Console.Write("(");
                            Console.Write(Data[x1, x2]);
                            if (Operator[x1, x2] != 5)
                            {
                                if (Operator[x1, x2] == 1)
                                {
                                    Console.Write("+");
                                }
                                if (Operator[x1, x2] == 2)
                                {
                                    Console.Write("-");
                                }
                                if (Operator[x1, x2] == 3)
                                {
                                    Console.Write("*");
                                }
                                if (Operator[x1, x2] == 4)
                                {
                                    Console.Write("/");
                                }

                            }

                        }
                        else
                        {
                            Console.Write(Data[x1, x2]);
                            if (Operator[x1, x2] != 5)
                            {
                                if (Operator[x1, x2] == 1)
                                {
                                    Console.Write("+");
                                }
                                if (Operator[x1, x2] == 2)
                                {
                                    Console.Write("-");
                                }
                                if (Operator[x1, x2] == 3)
                                {
                                    Console.Write("*");
                                }
                                if (Operator[x1, x2] == 4)
                                {
                                    Console.Write("/");
                                }
                                
                            }
                            else
                            {
                                for (int x3 = 0; x3 < num_number; x3++)
                                {
                                    if (kuohao[x1, x3] == 1)
                                    {
                                        Console.Write(")");
                                    }
                                }
                                Console.Write("=");
                                
                            }
                        }
                    }
                    Console.WriteLine();
                }
            }
            else
            {
                Console.WriteLine("请输入文件路径(G:\\1.txt):");
                filepath=Console.ReadLine();
                path = @filepath;
                StreamWriter strm1 = File.CreateText(path);
                for (int x1 = 0; x1 < num_topic; x1++)
                {
                    for (int x2 = 0; x2 < num_number; x2++)
                    {
                        if (kuohao[x1, x2] == 1)
                        {
                            strm1.Write("(");
                            strm1.Write(Data[x1, x2]);
                            if (Operator[x1, x2] != 5)
                            {
                                if (Operator[x1, x2] == 1)
                                {
                                    strm1.Write("+");
                                }
                                if (Operator[x1, x2] == 2)
                                {
                                    strm1.Write("-");
                                }
                                if (Operator[x1, x2] == 3)
                                {
                                    strm1.Write("*");
                                }
                                if (Operator[x1, x2] == 4)
                                {
                                    strm1.Write("/");
                                }

                            }

                        }
                        else
                        {
                            strm1.Write(Data[x1, x2]);
                            if (Operator[x1, x2] != 5)
                            {
                                if (Operator[x1, x2] == 1)
                                {
                                    strm1.Write("+");
                                }
                                if (Operator[x1, x2] == 2)
                                {
                                    strm1.Write("-");
                                }
                                if (Operator[x1, x2] == 3)
                                {
                                    strm1.Write("*");
                                }
                                if (Operator[x1, x2] == 4)
                                {
                                    strm1.Write("/");
                                }

                            }
                            else
                            {
                                for (int x3 = 0; x3 < num_number; x3++)
                                {
                                    if (kuohao[x1, x3] == 1)
                                    {
                                        strm1.Write(")");
                                    }
                                }
                                strm1.Write("=");

                            }
                        }
                    }
                    strm1.WriteLine();
                }
                strm1.Close();

            }


        }
    }
}


结果:

技术分享

技术分享

以上是关于四则运算题出题程序代码及分析的主要内容,如果未能解决你的问题,请参考以下文章

四则运算出题程序的分析与设计

20194582自动生成四则运算题第一版报告

网页版的四则运算出题

20194598自动生成四则运算题第一版报告

四则运算

用C语言写一个 小学生口算出题系统