四则运算二

四则运算二

一、程序设计思路

在详细的查看了观察《冀教版数学二年级上册口算练习》文档之后,可以发现:1.算式中每个数不能超过100,运算结果不能超过100。2.运算结果必须得是正整数,除法不能出现余数。3.乘法算式的范围在9 X 9乘法表内,除法算式同样。4.可以出现一部分混合运算,但仅限于加减法而且不带括号优先级。

根据上述规则,可以采用如下方式产生算式:

首先可以明确,在一定范围内,随机数的产生结果不会有大规模的重复现象,查重很耗费时间。

操作符通过1-4的随机数产生,一个数对应一个操作符。

对于加法算式,第一个操作数opre1直接在1-99内生成,第二个操作数在100-opre1到1之间生成。

对于减法算式,第一个操作数opre1直接在1-99内生成,第二个操作数在opre1到1之间生成。

对于乘法,两个操作数均在1-9内产生。

对于除法,先产生两个1-9之间的随机数opre1、opre2,然后让opre1 = opre1*opre2,opre2不变,opre1作为算式的答案。

对于三个操作数的混合运算,原理同上。

 

 

二、源代码

 

  1 import java.util.ArrayList;
  2 import java.util.List;
  3 import java.util.Random;
  4 import java.util.Scanner;
  5 import java.util.Set;
  6 import java.util.Vector;
  7 
  8 public class GradeTwo {
  9     private static String opera = "+-*/";
 10     public static Vector<String> pro = new Vector<String>();
 11     public static Vector<String> key = new Vector<String>();
 12     //public static Vector<String> key1 = new Vector<String>();
 13     public static String[]result1;
 14     public static int[]end = new int[3];
 15     public static void solve(String[]list) {
 16         result1 = new String[list.length];
 17         for(int i = 0;i<list.length;++i)
 18             result1[i]= list[i];
 19     }
 20     public static void clear() {
 21         pro.clear();
 22         key.clear();
 23     }
 24     public static int isInteger(int c1, int c2, char opera) {
 25         int result = 0;
 26         int result1 = 0;
 27         switch (opera) {
 28         case '+':
 29             result = c1 + c2;
 30             break;
 31         case '-':
 32             result = c1 - c2;
 33             break;
 34         case '*':
 35             result = c1 * c2;
 36             break;
 37         case '/':
 38             result = c1 / c2;
 39             result1 = c1 % c2;
 40             break;
 41         }
 42         if (result >= 0 && result <= 100 && result1 == 0)
 43             return result;
 44         else
 45             return -1;
 46     }
 47 
 48     public static String getPoly() {
 49         String poly = "";
 50 //        Random random = new Random();
 51 //        
 52 //        int c1;
 53 //        int c2;
 54 //        int result;
 55 //        char operac;
 56 //        boolean flag = true;
 57 //        do {
 58 //            c1 = random.nextInt(100) + 1;
 59 //            c2 = random.nextInt(100) + 1;
 60 //            operac = opera.charAt(random.nextInt(4));
 61 //            if (operac == '*')
 62 //                poly = c1 + " × " + c2 + " = ";
 63 //            if (operac == '/')
 64 //                poly = c1 + " ÷ " + c2 + " = ";
 65 //            if (operac == '+')
 66 //                poly = c1 + " + " + c2 + " = ";
 67 //            if (operac == '-')
 68 //                poly = c1 + " - " + c2 + " = ";
 69 //            /*for (int i = 0;i<pro.size();++i) {
 70 //                if (poly.equals(pro.get(i))) {
 71 //                    flag = false;
 72 //                    break;
 73 //                }
 74 //            }*/
 75 //        } while (!flag || (result = isInteger(c1, c2, operac)) < 0);
 76 //        pro.add(poly);
 77 //        key.add(String.valueOf(result));
 78         Random random = new Random();
 79         //产生0.9的两位运算,0.1的三位运算
 80         char operate;
 81         char operate1;
 82         int operand1 = 0;
 83         int operand2 = 0;
 84         int operand3 = 0;
 85         int result;
 86         int percent = random.nextInt(100)+1;
 87         if(percent<=90) {
 88             operate = opera.charAt(random.nextInt(4));
 89             switch(operate) {
 90                case '+':
 91                    operand1 = random.nextInt(100);
 92                    operand2 = random.nextInt(100-operand1);
 93                    poly = operand1+" + "+operand2+" = ";
 94                    break;
 95                case '-':
 96                    operand1 = random.nextInt(100)+1;
 97                    operand2 = random.nextInt(operand1);
 98                    poly = operand1+" - "+operand2+ " = ";
 99                    break;
100                case '*':
101                    operand1 = random.nextInt(9)+1;
102                    operand2 = random.nextInt(9)+1;
103                    poly = operand1+" × "+operand2+ " = ";
104                    break;
105                case '/':
106                    operand1 = random.nextInt(9)+1;
107                    operand2 = random.nextInt(9)+1;
108                    operand1 = operand1*operand2;
109                    poly = operand1+" ÷ "+operand2+ " = ";
110                    break;
111             }
112             result = isInteger(operand1, operand2, operate);
113             key.add(String.valueOf(result));
114         }else {
115             operate = opera.charAt(random.nextInt(2));
116             operate1 = opera.charAt(random.nextInt(2));
117             if(operate=='+') {
118                 operand1 = random.nextInt(100);
119                 operand2 = random.nextInt(100-operand1);
120                 if(operate1=='+') {
121                     operand3 = random.nextInt(100-operand1-operand2);
122                     poly = operand1+" + "+operand2+" + "+operand3+" = ";
123                 }else {
124                     operand3 = random.nextInt(operand1+operand2);
125                     poly = operand1+" + "+operand2+" - "+operand3+" = ";
126                 }
127             }else {
128                 operand1 = random.nextInt(100)+1;
129                 operand2 = random.nextInt(operand1);
130                 if(operate1=='+') {
131                     operand3 = random.nextInt(100-operand1+operand2);
132                     poly = operand1+" - "+operand2+" + "+operand3+" = ";
133                 }else {
134                     operand3 = random.nextInt(operand1-operand2);
135                     poly = operand1+" - "+operand2+" - "+operand3+" = ";
136                 }
137             }
138             result = isInteger(operand1, operand2, operate);
139             result = isInteger(result, operand3, operate1);
140             key.add(String.valueOf(result));
141         }
142         pro.add(poly);
143         return poly;
144     }
145     public static void End(String[]list){
146         //int[]result = new int[3];
147         end[0] = 0;
148         end[1] = 0;
149         end[2] = 0;
150         int len = list.length;
151         for(int i = 0;i<len;++i) {
152             if(key.get(i).equals(list[i]))end[0]++;
153             else {
154                 if(list[i].equals(""))end[2]++;
155                 else end[1]++;
156             }
157         }
158         return;
159     }
160     public static void main(String[]args) {
161 //        int result = isInteger(55,13,'-');
162 //        result = isInteger(result,19,'-');
163 //        System.out.println(result);
164         Scanner s = new Scanner(System.in);
165         System.out.print("输入题目数量:");
166         int num = s.nextInt();
167         String str = null;
168         for(int i = 1;i<=num;++i) {
169             str = GradeTwo.getPoly();
170             System.out.println(str);
171         }
172     }
173 }

 

 

 

 

三、验证截图

------------------------------------------------------------------------------------------------------------------------------------------END

 

 

posted @ 2017-11-30 23:17  messi2017  阅读(192)  评论(0编辑  收藏  举报