构建之法--四则运算

一、基本要求
自动生成小学四则运算
填入答案,会判断正确与否
支持整数、真分数
题型选择 加、减、乘、除
最大、最小范围

二、需求分析
编写小学四则运算测试系统,要求完成两位数的加,减,乘,除四则运算。
能根据用户的输入来选择运算种类,并自动生成题目及打印

 

三、设计
1.算法:随机选取2位数,进行运算组合
2.打印题目时,进行题目打印
3.避免重复

四、代码实现

建立Mathpro类及ThreadSafeRandom类,抓取一定范围内的随机数组成运算,并进行排列,输出及打印

  1  //试题集
  2 List<Mathpro> mathlist = new List<Mathpro>();
  3 // flag1 ,flag2,flag3,flag4 分别表示是否选择加法、减法、乘法、除法
  4  int typecount = flag1 + flag2 + flag3 + flag4;
  5  do
  6               {
  7 
  8                   for (int j = 0; j < typecount; j++)
  9                   {
 10 
 11                       factor1 = ThreadSafeRandom.Next(iDwon, iUp);
 12                       factor2 = ThreadSafeRandom.Next(iDwon, iUp);
 13                       symbol = symbollist[j];
 14                       if (symbol == "/")
 15                       {
 16                           factor1 = factor1 * factor2;
 17                       }
 18                       else if (symbol == "-")
 19                       {
 20                           if (factor1.CompareTo(factor2) < 0)
 21                           {
 22                               int itemp = factor1;
 23                               factor1 = factor2;
 24                               factor2 = itemp;
 25                           }
 26                       }
 27                       if (mathlist != null && mathlist.Count > 0)
 28                       {
 29                           if (mathlist.Find((Mathpro s) => s.factor1 == factor1.ToString() && s.factor2 == factor2.ToString() && s.symbol == symbol.ToString()) != null)
 30                           {
 31                               continue;
 32                           }
 33 
 34                       }
 35                       Mathpro math = new Mathpro(factor1.ToString(), factor2.ToString(), symbol, "");
 36                       mathlist.Add(math);
 37                       istep++;
 38                   }
 39 
 40               } while (istep < Sumcount);
 41 
 42 
 43               listBox1.Items.Clear();
 44               mathlist.Sort();
 45 
 46               foreach (Mathpro p in mathlist)
 47               {
 48                   listBox1.Items.Add(p.factor1 + " " + p.symbol + " " + p.factor2 + " =  ");
 49 
 50               }
 51 
 52 
 53 public class Mathpro : IComparable<Mathpro>
 54     {
 55         private string _factor1;
 56         private string _factor2;
 57         private string _symbol;
 58         private string _answer;
 59 
 60         public int CompareTo(Mathpro mathpro)
 61         {
 62             if (mathpro.symbol.Equals(_symbol))
 63                 return 0;
 64             else
 65             {
 66 
 67                 if (_symbol.CompareTo(mathpro.symbol) > 0)
 68                 {
 69                     return -1;
 70                 }
 71                 else
 72                     return 1;
 73             }
 74 
 75         }
 76 
 77         public Mathpro(String factor1, String factor2, String symbol, String answer) 
 78         {
 79             this._factor1 = factor1;
 80             this._factor2 = factor2;
 81             this._symbol = symbol;
 82             this._answer = answer;
 83         }
 84         //Factor1
 85         public String factor1 
 86         {
 87             get 
 88             {
 89                 return  this._factor1;
 90             } 
 91         }
 92 
 93         //Factor2
 94         public String factor2
 95         {
 96             get
 97             {
 98                 return this._factor2;
 99             }
100         }
101 
102         //Factor1
103         public String answer
104         {
105             get
106             {
107                 return this._answer;
108             }
109         }
110         //Factor1
111         public String symbol
112         {
113             get
114             {
115                 return this._symbol;
116             }
117         }
118       
119        
120 
121     }
122 
123   
124 public class ThreadSafeRandom
125     {
126         private static Random random = new Random();
127 
128         public static int Next(int iDown,int iUp)
129         {
130             lock (random)
131             {
132                 return random.Next(iDown, iUp);
133             }
134         }
135     }

 

posted @ 2017-06-01 09:32  tinary  阅读(165)  评论(0)    收藏  举报