10.23每日总结

完善课上的口算题卡代码,实现重复题目的检测、题目数字范围、加减乘除算式的参数化等扩展功能,提交代码和运行截图。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
import java.util.*;
 
 
 
public class test {
 
    private static String strTest = "";
 
    private static final Random RANDOM = new Random();
 
    private static final Scanner SCANNER = new Scanner(System.in);
 
    private static Integer number_random;
 
 
 
    public static void main(String[] args) {
 
 
 
        System.out.print("请输入参与数字的最大值:");
 
        number_random = SCANNER.nextInt();
 
 
 
        System.out.print("请输入要生成题目数量:");
 
        getExpression(SCANNER.nextInt());
 
    }
 
 
 
    /**
 
     * 计算公约数(在下面getExpression方法里面已经排除 0 )
 
     */
 
    public static int gcd(int a, int b) {
 
        if (a % b == 0) {
 
            return b;
 
        } else {
 
            return gcd(b, a % b);
 
        }
 
    }
 
 
 
    /**
 
     * 获取随机运算符
 
     */
 
    public static char getOperator() {
 
        char[] signal = {'+', '-', '*', '/'};
 
        return signal[RANDOM.nextInt(4)];
 
    }
 
 
 
 
 
    /**
 
     * 生成表达式
 
     */
 
    public static void getExpression(int getNums) {
 
        char operator1, operator2;
 
        int num1, num2, num3;
 
        while (getNums > 0) {
 
            boolean flag = false;
 
            getNums--;
 
            operator1 = getOperator();
 
            operator2 = getOperator();
 
            //保证两个不同的运算符
 
            while (operator1 == operator2) {
 
                operator1 = getOperator();
 
                operator2 = getOperator();
 
            }
 
            // + 1 是避免出现 0
 
            num1 = RANDOM.nextInt(number_random) + 1;
 
            num2 = RANDOM.nextInt(number_random) + 1;
 
            num3 = RANDOM.nextInt(number_random) + 1;
 
 
 
            //以下判断只是为了初步判断是否出现小数和负数
 
            if (operator1 == '/') {
 
                if (gcd(num1, num2) != num2) {
 
                    flag = true;
 
                }
 
            }
 
            if (operator2 == '/') {
 
                if (gcd(num2, num3) != num3) {
 
                    flag = true;
 
                }
 
            }
 
            if (operator1 == '-') {
 
                if (num1 - num2 <= 0) {
 
                    flag = true;
 
                }
 
            }
 
            if (operator2 == '-') {
 
                if (num2 - num3 <= 0) {
 
                    flag = true;
 
                }
 
            }
 
            if (operator1 == '*') {
 
                if (num1 * num2 > 99) {
 
                    flag = true;
 
                }
 
            }
 
            if (operator2 == '*') {
 
                if (num2 * num3 > 99) {
 
                    flag = true;
 
                }
 
            }
 
            if (operator1 == '+') {
 
                if (num1 + num2 > 99) {
 
                    flag = true;
 
                }
 
            }
 
            if (operator2 == '+') {
 
                if (num2 + num3 > 99) {
 
                    flag = true;
 
                }
 
            }
 
            if (flag) {
 
                getNums++;
 
                continue;
 
            }
 
            strTest += num1 + "" + operator1 + "" + num2 + "" + operator2 + num3;
 
            String str = calculate(strTest);
 
            strTest = "";
 
            double res = Double.parseDouble(str);
 
            int intRes = (int) res;
 
            if (intRes >= 0 && intRes <= 100) {
 
                String res2 = "" + num1 + "" + operator1 + "" + num2 + "" + operator2 + "" + num3 + "" + '=' + "" + intRes;
 
                String replace = res2.replace('/', '÷');
 
                System.out.println(replace);
 
            } else {
 
                getNums++;
 
            }
 
        }
 
    }
 
    public static String calculate(String expression) {
 
        List<String> num;
 
        Stack<Double> stack;
 
        num = transformEnd(expression);
 
        stack = new Stack<>();
 
        double sum;
 
        while (!num.isEmpty()) {
 
            String temp = String.valueOf(num.remove(0));
 
            if (isNumber(temp)) {
 
                double s = Double.parseDouble(temp);
 
                stack.push(s);
 
            } else {
 
                double a = stack.pop();
 
                double b = stack.pop();
 
                double c = calTwo(b, a, temp);
 
                stack.push(c);
 
            }
 
        }
 
        sum = stack.pop();
 
        return String.valueOf(sum);
 
    }
 
 
 
    /**
 
     * 计算两个值
 
     */
 
    private static double calTwo(double a, double b, String opr) {
 
        double sum = 0.0;
 
        switch (opr) {
 
            case "+":
 
                sum = a + b;
 
                break;
 
            case "-":
 
                sum = a - b;
 
                break;
 
            case "*":
 
                sum = a * b;
 
                break;
 
            case "/":
 
                sum = a / b;
 
                break;
 
        }
 
        return sum;
 
    }
 
 
 
    /**
 
     * 将中缀表达式转化为后缀表达式(栈是用来进出运算的符号)
 
     */
 
    public static List<String> transformEnd(String expre) {
 
        List<String> sb = new ArrayList<>();
 
        Stack<String> stack = new Stack<>();
 
        expre = expre.replaceAll("(\\D)", "o$1o");
 
        String[] esp = expre.trim().split("o");
 
        for (String value : esp) {
 
            String s = value.trim();
 
            if (isNumber(s)) {
 
                // 如果是数字则输出
 
                sb.add(s);
 
            } else if (!s.isEmpty()) {
 
                if (!stack.isEmpty() && !isMaxExp(s.charAt(0), stack.peek().charAt(0))) {
 
                    while (!stack.isEmpty() && !isMaxExp(s.charAt(0), stack.peek().charAt(0))) {
 
                        sb.add(stack.pop());
 
                    }
 
                }
 
                stack.push(s);
 
            }
 
        }
 
        while (!stack.isEmpty()) {
 
            sb.add(stack.pop());
 
        }
 
        return sb;
 
    }
 
 
 
    /**
 
     * 判断是否是数字
 
     */
 
    private static boolean isNumber(String str) {
 
        try {
 
            Double.parseDouble(str);
 
        } catch (RuntimeException e) {
 
            return false;
 
        }
 
        return true;
 
    }
 
 
 
    /**
 
     * 判断两个操作符优先级
 
     */
 
    private static boolean isMaxExp(char exp1, char exp2) {
 
        return transExp(exp1) > transExp(exp2);
 
    }
 
 
 
    /**
 
     * 优先级
 
     */
 
    private static int transExp(char exp) {
 
        int re = 0;
 
        switch (exp) {
 
            case '*':
 
            case '/':
 
                re = 2;
 
                break;
 
            case '+':
 
            case '-':
 
                re = 1;
 
                break;
 
        }
 
        return re;
 
    }
 
 
 
}

  

 

posted @   漏网鲨鱼  阅读(2)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
点击右上角即可分享
微信分享提示