四则运算03

小组成员:

黄丽萍,杜若憧

工作照片:

设计思想:

没有括号时:

对整数’+’,’-’,’*’运算直接由用户输入结果,与计算结果进行比较。‘/’先化简再用字符串类型以最简分数的形式输出。

对分数运算:分别求出分子和分母,用simplify()函数将其化简,并用字符串类型将其以最简分数的形式输出。

有括号时:

使用新的数组rs[]储存括号计算结果,用op[]储存括号外的运算符。利用for循环,先找到‘*’,‘/’运算符将符号两边的数进行计算,将后一个数修改为0,此运算符修改为‘+’。再而计算‘+’,‘-’运算。

源代码:

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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
import java.text.DecimalFormat;<br>import java.util.Scanner;
public class operate {
    static int simplify(int x,int y){/*求两数最大公约数,用于分数化简*/
        int t,xx,yy,k=1;
        if(x>y)
        {
            t=x;x=y;y=t;
        }
        if(x<0)
        {
            x=-1*x;
        }
        if(y<0)
        {
            y=-1*y;
        }
        for(int i=x;i>=2;i--)
        {
              xx=x%i;yy=y%i;
              if(xx==0&&yy==0)
              {
                  k=i;
                  break;
              
            }
        return k;
    }
     
    public static void main(String[] args) {
        // TODO 自动生成的方法存根
        int a1,a2,b1,b2,k;
        int count=0;//统计作对题目个数
        /*控制题目个数*/
        Scanner in=new Scanner(System.in);
        System.out.print("请输入题目个数(>0):");
        int number=in.nextInt();
         /*数值范围*/
        System.out.print("请输入数值所取得最大值(>0):");
        int max=in.nextInt();
        /*判断括号*/
        int backets;
        System.out.print("是否有括号:(0:无  1:有):");
        backets=in.nextInt();
        if(backets==0)//没有括号
        {
            /*控制是否有负数*/
            System.out.print("是否有负数:(0:无  1:有):");
            int negative=in.nextInt();
            /*控制是否有乘除*/
            System.out.print("是否有乘除法(0:无   1:有):");
            int panduan;
            for(;;)
            {
                panduan=in.nextInt();
                if(panduan==0||panduan==1) break;
                else
                    panduan=in.nextInt();
            }    
           /*有无余数*/
            int remainder=0;
            if(panduan==1){
            System.out.print("是否有余数:(0:无  1:有):");
            remainder=in.nextInt();}
           /*算法的实现*/
            for(int i=0;i<number;i++)
            {
                int index;
                if(panduan==0)
                {
                    index=(int)(Math.random()*1);
                }
                else
                {
                    index=(int)(Math.random()*4); //随机产生,控制运算符
                }
               a1=(int)(Math.random()*max);
               a2=(int)(Math.random()*(max-1)+1);
               b1=(int)(Math.random()*max);
               b2=(int)(Math.random()*(max-1)+1);
               k=(int)(Math.random()*2);        //控制是否是整数或分数,随机产生
               if(k==0)//k=1输出整数运算
                    {
                      if(index==0) //加法运算
                        {
                          if(negative==0)//没有负数
                              {
                                 System.out.print(a1+"+"+b1+"=");
                                 int rs=a1+b1;
                                 int result=in.nextInt();
                                 if(result==rs)
                                 {
                                     count+=1;
                                     System.out.println("回答正确!");
                                 }
                                 else
                                 {
                                     System.out.println("回答错误!");
                                 }
                                 System.out.println("正确结果是"+rs);
                              }
                          else //有负数
                          {
                              a1=-1*a1;
                              System.out.print(a1+"+"+b1+"="); 
                              int rs=a1+b1;
                              int result=in.nextInt();
                              if(result==rs)
                              {
                                  count+=1;
                                  System.out.println("回答正确!");
                              }
                              else
                              {
                                  System.out.println("回答错误!");
                              }
                              System.out.println("正确结果是"+rs);
                          }
                        }
                      if(index==1)//减法运算
                        {
                          if(negative==0)
                          {//无负数
                              if(a1>b1)
                              {
                                  System.out.print(a1+"-"+b1+"=");
                                  int rs=a1-b1;
                                  int result=in.nextInt();
                                  if(result==rs)
                                  {
                                       count+=1;   
                                       System.out.println("回答正确!");
                                  }
                                  else
                                  {
                                      System.out.println("回答错误!");
                                  }
                                  System.out.println("正确结果是"+rs);
                              }
                              else
                              {
                                  System.out.print(b1+"-"+a1+"=");
                                  int rs=b1-a1;
                                  int result=in.nextInt();
                                  if(result==rs)
                                  {
                                      count+=1;
                                      System.out.println("回答正确!");
                                  }
                                  else
                                  {
                                      System.out.println("回答错误!");
                                  }
                                  System.out.println("正确结果是"+rs);
                              }
                           }
                          else//有负数
                          {
                              System.out.print(a1+"-"+b1+"=");
                              int rs=a1-b1;
                              int result=in.nextInt();
                              if(result==rs)
                              {
                                  count+=1;
                                  System.out.println("回答正确!");
                              }
                              else
                              {
                                  System.out.println("回答错误!");
                              }
                               
                              System.out.println("正确结果是"+rs);
                          }
                     }
                   if(index==2)//乘法运算
                   {
                       System.out.print(a1+"*"+b1+"=");
                       int rs=a1*b1;
                       int result=in.nextInt();
                       if(result==rs)
                       {
                           count+=1;   
                           System.out.println("回答正确!");
                       }
                       else
                       {
                           System.out.println("回答错误!");
                       }
                       System.out.println("正确结果是"+rs);
                   }
                   if(index==3)//除法运算
                   {
                       if(remainder==0)
                       {//没有余数
                           for(int kk=0;;)
                           { //余数不为0.继续生成随机数                           
                               if(a1!=0)
                               {   //判断分母是否为0
                                   if(b1%a1!=0)
                                   {
                                       a1=(int)(Math.random()*max);
                                       b1=(int)(Math.random()*max);
                                   }                           
                                   else
                                   break;
                               }
                           else
                           a1=(int)(Math.random()*max);}
                       }
                       System.out.print(b1+"/"+a1+"=");
                       String rs;
                       if(b1==0)
                       {
                           rs=0+"";//结果为0,不需要化简
                       }
                       int cd=simplify(b1,a1);
                       b1=b1/cd;a1=a1/cd;                 
                       if(a1==1)
                       {
                           rs=b1+"";  
                       }
                       else
                       {
                           rs=b1+"/"+a1;
                       
                       String result=in.next();
                       if(result.equals(rs))
                       {
                           count+=1;   
                           System.out.println("回答正确!");
                       }
                       else
                       {
                           System.out.println("回答错误!");
                       }
                       System.out.println("正确结果是"+rs);
                    }
                 
            }
            else//分数计算
            
                int s2=simplify(a1,a2);
                a1=a1/s2;
                a2=a2/s2;
                int s3=simplify(b1,b2);
                b1=b1/s3;
                b2=b2/s3;
                if(index==0)
                {
                    if(negative==0) //没有负数
                    {
                    /*分母不为1,分情况输出*/
                         if(a2!=1&&b2!=1)
                         {
                              System.out.println(a1+"/"+a2+"+"+b1+"/"+b2+"=");
                              int denom=a2*b2; //分母
                              int molecule=a1*b2+b1*a2;//分子
                              int cd=simplify(molecule,denom);//最大公约数
                              denom=denom/cd;
                              molecule=molecule/cd;
                              String rs;
                              if(denom==1)
                              {
                                  rs=molecule+"";  
                              }
                              else
                                  rs=molecule+"/"+denom;//化简结果
                              String result=in.next();
                              if(result.equals(rs))
                              {
                                  count+=1;
                                  System.out.println("回答正确!");
                              }
                              else
                              {
                                  System.out.println("回答错误!");
                              }
                              System.out.println("正确结果是"+rs);
                         }
                         else if(a2==1)
                         {
                               System.out.print(a1+"+"+b1+"/"+b2+"=");
                               int denom=b2; //分母
                               int molecule=a1*b2+b1;//分子
                               int cd=simplify(molecule,denom);//最大公约数
                               String rs;
                               denom=denom/cd;
                               molecule=molecule/cd;
                               if(denom==1)
                               {
                                    rs=molecule+"";  
                               }
                               else
                                    rs=molecule+"/"+denom;//化简结果
                               String result=in.next();
                               if(result.equals(rs))
                               {
                                   count+=1;   
                                   System.out.println("回答正确!");
                               }
                               else
                               {
                                   System.out.println("回答错误!");
                               }
                               System.out.println("正确结果是"+rs);
                          }
                          else if(b2==1)
                          {
                               System.out.print(a1+"/"+a2+"+"+b1+"=");
                               int denom=a2; //分母
                               int molecule=a1+b1*a2;//分子
                               int cd=simplify(molecule,denom);//最大公约数
                               denom=denom/cd;
                               molecule=molecule/cd;
                               String rs;
                               if(denom==1)
                               {
                                    rs=molecule+"";  
                               }
                               else
                                    rs=molecule+"/"+denom;//化简结果
                               String result=in.next();
                               if(result.equals(rs))
                               {
                                    count+=1;  
                                    System.out.println("回答正确!");
                               }
                               else
                               {
                                    System.out.println("回答错误!");
                               }
                               System.out.println("正确结果是"+rs);
                           }
                        
                    }
                    else{//有负数
                        if(a2!=1&&b2!=1)
                        {
                               System.out.print("-"+a1+"/"+a2+"+"+b1+"/"+b2+"=");
                               int denom=a2*b2; //分母
                               int molecule=b1*a2-a1*b2;//分子
                               int cd=simplify(molecule,denom);//最大公约数
                               denom=denom/cd;
                               molecule=molecule/cd;
                               String rs;
                               if(denom==1)
                               {
                                    rs=molecule+"";  
                               }
                               else
                                    rs=molecule+"/"+denom;//化简结果
                               String result=in.next();
                               if(result.equals(rs))
                               {
                                   count+=1;   
                                   System.out.println("回答正确!");
                               }
                               else
                               {
                                   System.out.println("回答错误!");
                               }
                               System.out.println("正确结果是"+rs);
                        }
                            else if(a2==1)
                            {
                               System.out.print("-"+a1+"+"+b1+"/"+b2+"=");
                               int denom=b2; //分母
                               int molecule=b1-a1*b2;//分子
                               int cd=simplify(molecule,denom);//最大公约数
                               denom=denom/cd;
                               molecule=molecule/cd;
                               String rs;
                               if(denom==1)
                               {
                                    rs=molecule+"";  
                               }
                               else
                                    rs=molecule+"/"+denom;//化简结果
                               String result=in.next();
                               if(result.equals(rs))
                               {
                                    count+=1;  
                                    System.out.println("回答正确!");
                               }
                               else
                               {
                                   System.out.println("回答错误!");
                               }
                               System.out.println("正确结果是"+rs);
                             }
                            else if(b2==1)
                            {
                                System.out.print("-"+a1+"/"+a2+"+"+b1+"=");
                                int denom=a2; //分母
                                int molecule=b1*a2-a1;//分子
                                
                                int cd=simplify(molecule,denom);//最大公约数
                                denom=denom/cd;
                                molecule=molecule/cd;
                                String rs;
                                if(denom==1)
                                {
                                    rs=molecule+"";  
                                }
                                else
                                    rs=molecule+"/"+denom;//化简结果
                                String result=in.next();
                                if(result.equals(rs))
                                {
                                    count+=1;  
                                    System.out.println("回答正确!");
                                }
                                else
                                {
                                    System.out.println("回答错误!");
                                }
                                System.out.println("正确结果是"+rs);
                            }
                    }
                }
                if(index==1)
                {
                    if(a1/a2>=b1/b2)
                    {/*分类输出:分母是否为1*/
                       if(a2!=1&&b2!=1)
                       {
                            System.out.print(a1+"/"+a2+"-"+b1+"/"+b2+"=");
                            int denom=a2*b2; //分母
                            int molecule=a1*b2-a2*b2;//分子
                           
                            int cd=simplify(molecule,denom);//最大公约数
                            denom=denom/cd;
                            molecule=molecule/cd;
                            String rs;
                            if(denom==1)
                            {
                                rs=molecule+"";  
                            }
                            else
                                rs=molecule+"/"+denom;//化简结果
                            String result=in.next();
                            if(result.equals(rs))
                            {
                                count+=1;  
                                System.out.println("回答正确!");
                            }
                            else
                            {
                                System.out.println("回答错误!");
                            }
                            System.out.println("正确结果是"+rs);
                       }
                       else if(a2==1)
                       {
                            System.out.print(a1+"-"+b1+"/"+b2+"=");
                            int denom=b2; //分母
                            int molecule=a1*b2-b1;//分子
                          
                            int cd=simplify(molecule,denom);//最大公约数
                            denom=denom/cd;
                            molecule=molecule/cd;
                            String rs;
                            if(denom==1)
                            {
                                rs=molecule+"";  
                            }
                            else
                                rs=molecule+"/"+denom;//化简结果
                            String result=in.next();
                            if(result.equals(rs))
                            {
                                count+=1;  
                                System.out.println("回答正确!");
                            }
                            else
                            {
                                System.out.println("回答错误!");
                            }
                            System.out.println("正确结果是"+rs);
                       }
                       else if(b2==1)
                       {
                            System.out.print(a1+"/"+a2+"-"+b1+"=");
                            int denom=a2; //分母
                            int molecule=a1-a2*b1;//分子
                            int cd=simplify(molecule,denom);//最大公约数
                            denom=denom/cd;
                            molecule=molecule/cd;
                            String rs;
                            if(denom==1)
                            {
                                rs=molecule+"";  
                            }
                            else
                                rs=molecule+"/"+denom;//化简结果
                            String result=in.next();
                            if(result.equals(rs))
                            {
                                count+=1;  
                                System.out.println("回答正确!");
                            }
                            else
                            {
                                System.out.println("回答错误!");
                            }
                            System.out.println("正确结果是"+rs);
                       }
                   }
                   else{
                        if(a2!=1&&b2!=1)
                        {
                            System.out.print(b1+"/"+b2+"-"+a1+"/"+a2+"=");
                            int denom=a2*b2; //分母
                            int molecule=b1*a2-a1*b2;//分子
                            int cd=simplify(molecule,denom);//最大公约数
                            denom=denom/cd;
                            molecule=molecule/cd;
                            String rs;
                            if(denom==1)
                            {
                                rs=molecule+"";  
                            }
                            else
                                rs=molecule+"/"+denom;//化简结果
                            String result=in.next();
                            if(result.equals(rs))
                            {
                                count+=1;  
                                System.out.println("回答正确!");
                            }
                            else
                            {
                                System.out.println("回答错误!");
                            }
                            System.out.println("正确结果是"+rs);
                        }
                        else if(a2==1)
                        {
                            System.out.print(b1+"/"+b2+"-"+a1+"=");
                            int denom=b2; //分母
                            int molecule=b1-a1*b2;//分子
                            int cd=simplify(molecule,denom);//最大公约数
                            denom=denom/cd;
                            molecule=molecule/cd;
                            String rs;
                            if(denom==1)
                            {
                                rs=molecule+"";  
                            }
                            else
                                rs=molecule+"/"+denom;//化简结果
                            String result=in.next();
                            if(result.equals(rs))
                            {
                                count+=1;  
                                System.out.println("回答正确!");
                            }
                            else
                            {
                                System.out.println("回答错误!");
                            }
                            System.out.println("正确结果是"+rs);
                        }
                        else if(b2==1)
                        {
                            System.out.print(b1+"-"+a1+"/"+a2+"=");
                            int denom=a2; //分母
                            int molecule=b1*a2-a1;//分子
                            int cd=simplify(molecule,denom);//最大公约数
                            denom=denom/cd;
                            molecule=molecule/cd;
                            String rs;
                            if(denom==1)
                            {
                                rs=molecule+"";  
                            }
                            else
                                rs=molecule+"/"+denom;//化简结果
                            String result=in.next();
                            if(result.equals(rs))
                            {
                                count+=1;  
                                System.out.println("回答正确!");
                            }
                            else
                            {
                                System.out.println("回答错误!");
                            }
                            System.out.println("正确结果是"+rs);
                        }
                    }
                }
                if(index==2)//分数乘法
                {
                    if(a2!=1&&b2!=1)
                    {
                        System.out.println(a1+"/"+a2+"*"+b1+"/"+b2+"=");
                        int denom=a2*b2; //分母
                        int molecule=a1*b1;//分子
                        int cd=simplify(molecule,denom);//最大公约数
                        denom=denom/cd;
                        molecule=molecule/cd;
                        String rs;
                        if(denom==1)
                        {
                            rs=molecule+"";  
                        }
                        else
                            rs=molecule+"/"+denom;//化简结果                      
                        String result=in.next();
                        if(result.equals(rs))
                        {
                            count+=1;  
                            System.out.println("回答正确!");
                        }
                        else
                        {
                            System.out.println("回答错误!");
                        }
                        System.out.println("正确结果是"+rs);
                    }
                    else if(a2==1)
                    {
                        System.out.print(a1+"*"+b1+"/"+b2+"=");
                        int denom=b2; //分母
                        int molecule=a1*b1;//分子
                        int cd=simplify(molecule,denom);//最大公约数
                        denom=denom/cd;
                        molecule=molecule/cd;
                        String rs;
                        if(denom==1)
                        {
                            rs=molecule+"";  
                        }
                        else
                            rs=molecule+"/"+denom;//化简结果                  
                        String result=in.next();
                        if(result.equals(rs))
                        {
                            count+=1;  
                            System.out.println("回答正确!");
                        }
                        else
                        {
                            System.out.println("回答错误!");
                        }
                        System.out.println("正确结果是"+rs);
                    }
                    else if(b2==1)
                    {
                        System.out.print(a1+"/"+a2+"*"+b1+"=");
                        int denom=a2; //分母
                        int molecule=a1*b1;//分子
                        int cd=simplify(molecule,denom);//最大公约数
                        denom=denom/cd;
                        molecule=molecule/cd;
                        String rs;
                        if(denom==1)
                        {
                            rs=molecule+"";  
                        }
                        else
                            rs=molecule+"/"+denom;//化简结果                  
                        String result=in.next();
                        if(result.equals(rs))
                        {
                            count+=1;  
                            System.out.println("回答正确!");
                        }
                        else
                        {
                            System.out.println("回答错误!");
                        }
                        System.out.println("正确结果是"+rs);
                         
                    }
                }
                if(index==3)//分数除法
                {
                    if(a2!=1&&b2!=1)
                    {
                        for(int kk=0;;)
                        { //余数不为0.继续生成随机数                          
                             if(b1==0)
                             {   //判断分母是否为0
                                 b1=(int)(Math.random()*max);
                             }
                             else
                                 break;
                        }
                        System.out.print("("+a1+"/"+a2+")"+"/"+"("+b1+"/"+b2+")"+"=");
                        int denom=a2*b1; //分母
                        int molecule=a1*b2;//分子
                         
                        int cd=simplify(molecule,denom);//最大公约数
                        denom=denom/cd;
                        molecule=molecule/cd;
                        String rs;
                        if(molecule==0)
                        {
                            rs=0+"";
                        }
                        if(denom==1)
                        {
                            rs=molecule+"";  
                        }
                        else
                            rs=molecule+"/"+denom;//化简结果                  
                        String result=in.next();
                        if(result.equals(rs))
                        {
                            count+=1;  
                            System.out.println("回答正确!");
                        }
                        else
                        {
                            System.out.println("回答错误!");
                        }
                        System.out.println("正确结果是"+rs);
                    }
                    else if(a2==1&&a1!=0)
                    {
                        System.out.print(a1+"/"+"("+b1+"/"+b2+")"+"=");
                        int denom=b1; //分母
                        int molecule=a1*b2;//分子
                        int cd=simplify(molecule,denom);//最大公约数
                        denom=denom/cd;
                        molecule=molecule/cd;
                        String rs;
                        if(denom==1)
                        {
                            rs=molecule+"";  
                        }
                        else
                            rs=molecule+"/"+denom;//化简结果                  
                        String result=in.next();
                        if(result.equals(rs))
                        {
                            count+=1;  
                            System.out.println("回答正确!");
                        }
                        else
                        {
                            System.out.println("回答错误!");
                        }
                        System.out.println("正确结果是"+rs);
                    }
                    else if(b2==1&&b1!=0)
                    {
                        System.out.print("("+a1+"/"+a2+")"+"/"+b1+"=");
                        int denom=a2*b1; //分母
                        int molecule=a1;//分子
                        int cd=simplify(molecule,denom);//最大公约数
                        denom=denom/cd;
                        molecule=molecule/cd;
                        String rs;
                        if(denom==1)
                        {
                            rs=molecule+"";  
                        }
                        else
                            rs=molecule+"/"+denom;//化简结果                  
                        String result=in.next();
                        if(result.equals(rs))
                        {
                            count+=1;  
                            System.out.println("回答正确!");
                        }
                        else
                        {
                            System.out.println("回答错误!");
                        }
                        System.out.println("正确结果是"+rs);
                    }
                }
                 
            }
        }
             
         
    }
        else//有括号<br>        {<br>            int szgs,fhgs,x,z,y;//szgs数字个数 fhgs符号个数<br>            double result;//结果    <br>            int sz[]=new int[20];//数字数组<br>            char fh[]=new char[10];//字符数组<br>            char operate[]={'+','-','*','/'};<br>            for(int p=0;p<number;p++)<br>            {<br>                szgs=(int)(Math.random()*6+3);//生成3-4个数字<br>                fhgs=szgs-1;//字符个数比数字少1<br>                float rs[]=new float[20];<br>                float c;String s;<br>                char op[]=new char[10];//储存字符<br>                for(y=0;y<szgs;y++)<br>                {<br>                    sz[y]=(int)(Math.random()*max+1);//随机生成数<br>                }<br>                for(y=0;y<fhgs;y++)<br>                {<br>                    x=(int)(Math.random()*4);//产生0~3的随机数  控制字符<br>                    fh[y]=operate[x];<br>                }<br>                fh[fhgs]='=';<br>                if(fhgs%2==0)//偶数个运算符<br>                {<br>                    System.out.print(sz[0]);System.out.print(fh[0]);    <br>                    rs[0]=sz[0];op[0]=fh[0];<br>                    int d=1;<br>                    for(y=1;y<fhgs;y++)<br>                    {<br>                        System.out.print("(");<br>                        if(fh[y]=='/'||fh[y]=='-') //判断是否是真分数和负数<br>                        {<br>                            if(sz[y]<sz[y+1])<br>                            {//交换<br>                                int temp;<br>                                temp=sz[y];<br>                                sz[y]=sz[y+1];<br>                                sz[y+1]=temp;<br>                                System.out.print(sz[y]);<br>                                System.out.print(fh[y]);<br>                                System.out.print(sz[y+1]);<br>                            }<br>                            else<br>                            {<br>                                System.out.print(sz[y]);<br>                                System.out.print(fh[y]);<br>                                System.out.print(sz[y+1]);<br>                            }<br>                         }<br>                         else<br>                         {<br>                             System.out.print(sz[y]);<br>                             System.out.print(fh[y]);<br>                             System.out.print(sz[y+1]);        <br>                         }<br>                         System.out.print(")");    <br>                         if(fh[y]=='+')<br>                         {<br>                             rs[d]=sz[y]+sz[y+1];<br>                         }<br>                         else if(fh[y]=='-')<br>                         {<br>                             rs[d]=sz[y]-sz[y+1];<br>                         }<br>                         else if(fh[y]=='*')<br>                         {<br>                            rs[d]=sz[y]*sz[y+1];<br>                            <br>                         }<br>                         else if(fh[y]=='/')<br>                         {<br>                             c=(float)sz[y]/sz[y+1]; <br>                             DecimalFormat df = new DecimalFormat("0.00");//格式化小数   <br>                             s = df.format(c);//返回的是String类型 <br>                             rs[d] = Float.valueOf(s);<br>                         }<br>                         System.out.print(fh[y+1]);<br>                         op[d]=fh[y+1];<br>                         y=y+1;     <br>                         d=d+1;<br>                 }<br>                  //先算乘除<br>                for(d=0;d<fhgs/2;d++)<br>                {<br>                    if(op[d]=='*')<br>                    {<br>                        rs[d]=rs[d]*rs[d+1];<br>                        rs[d+1]=0;<br>                        op[d]='+';<br>                    }<br>                    if(op[d]=='/')<br>                    {<br>                         c=(float)rs[d]/rs[d+1]; <br>                         DecimalFormat df = new DecimalFormat("0.00");//格式化小数   <br>                         s = df.format(c);//返回的是String类型 <br>                         rs[d] = Float.valueOf(s);<br>                         rs[d+1]=0;<br>                         op[d]='+';<br>                    }<br>                    <br>                } <br>                float sum=rs[0];<br>                //后算加减<br>                for(int ii=0;ii<fhgs/2;ii++)<br>                {<br>                    if(op[ii]=='+')<br>                    {<br>                        sum=sum+rs[ii+1];<br>                    }<br>                    if(op[ii]=='-')<br>                    {<br>                        sum=sum-rs[ii+1];<br>                    }<br>                }<br>                result=in.nextDouble(); <br>                if(result==sum)<br>                {<br>                    System.out.println("回答正确!");<br>                    count++;<br>                }<br>                else <br>                    System.out.println("回答错误!");<br>                 System.out.println("正确结果:"+sum);<br>           }<br>           else//奇数运算符<br>           {<br>               int id=0;<br>               for(y=0;y<fhgs;y++)<br>               {<br>                   System.out.print("("); <br>                   if(fh[y]=='/'||fh[y]=='-') //判断是否是真分数和负数<br>                   {<br>                       if(sz[y]<sz[y+1])<br>                       {<br>                           int temp;<br>                           temp=sz[y];<br>                           sz[y]=sz[y+1];<br>                           sz[y+1]=temp;<br>                           System.out.print(sz[y]);<br>                           System.out.print(fh[y]);<br>                           System.out.print(sz[y+1]);<br>                       }<br>                       else<br>                       {<br>                           System.out.print(sz[y]);<br>                           System.out.print(fh[y]);<br>                           System.out.print(sz[y+1]);<br>                       }<br>                    }<br>                    else<br>                    {                    <br>                        System.out.print(sz[y]);<br>                        System.out.print(fh[y]);<br>                        System.out.print(sz[y+1]);<br>                    }<br>                    System.out.print(")");<br>                    //计算括号里的结果<br>                    if(fh[y]=='+')<br>                    {<br>                        rs[id]=sz[y]+sz[y+1];<br>                    }<br>                    else if(fh[y]=='-')<br>                    {<br>                        rs[id]=sz[y]-sz[y+1];<br>                    }<br>                    else if(fh[y]=='*')<br>                    {<br>                        rs[id]=sz[y]*sz[y+1];<br>                    }<br>                    else if(fh[y]=='/')<br>                    {<br>                         c=(float)sz[y]/sz[y+1]; <br>                         DecimalFormat df = new DecimalFormat("0.00");//格式化小数   <br>                         s = df.format(c);//返回的是String类型 <br>                         rs[id] = Float.valueOf(s);<br>                    }<br>                    System.out.print(fh[y+1]);<br>                    op[id]=fh[y+1];<br>                    y=y+1;<br>                    id=id+1;<br>                } <br>               <br>                //先算乘除<br>                for(id=0;id<fhgs/2;id++)<br>                {<br>                    if(op[id]=='*')<br>                    {<br>                        rs[id]=rs[id]*rs[id+1];<br>                        rs[id+1]=0;<br>                        op[id]='+';<br>                    }<br>                    if(op[id]=='/')<br>                    {<br>                         c=(float)rs[id]/rs[id+1]; <br>                         DecimalFormat df = new DecimalFormat("0.00");//格式化小数   <br>                         s = df.format(c);//返回的是String类型 <br>                         rs[id] = Float.valueOf(s);<br>                         rs[id+1]=0;<br>                         op[id]='+';<br>                    }<br>                    <br>                } <br>                float sum=rs[0];<br>                //后算加减<br>                for(int ii=0;ii<fhgs/2;ii++)<br>                {<br>                    if(op[ii]=='+')<br>                    {<br>                        sum=sum+rs[ii+1];<br>                    }<br>                    if(op[ii]=='-')<br>                    {<br>                        sum=sum-rs[ii+1];<br>                    }<br>                }<br>                result=in.nextDouble();    <br>                if(result==sum)<br>                {<br>                    System.out.println("回答正确!");<br>                    count=count+1;<br>                }<br>                else <br>                    System.out.println("回答错误!");<br>                System.out.println("正确结果:"+sum);<br>          }<br>         }<br>                    <br>        }<br>        System.out.println("计算正确的个数:"+count);<br>    }<br>}<br><br>    <br><br>
 
    

 实验结果截图:

 

 

 

                                                                                            周活动总结表

                                                                  姓名:杜若憧                            日期 2016/3/25

 

听课

编写代码

阅读课本

准备考试

 

 

日总计

周日

 

 

 

 

 

 

 

周一

100

 

40

 

 

 

140

周二

 

48

 

 

 

 

48

周三

 

40

 

 

 

 

30

周四

 

50

 

 

 

 

50

周五

 

30

 

 

 

 

30

周六

 

77

 

 

 

 

77

周总计

100

245

30

 

 

 

375

 

 

                                                                                      时间记录日志

                   学生:杜若憧                    日期:2016/3/25

          教师:王建民                    课程:软件工程

日期

开始时间

结束时间

中断时间

净时间

活动

备注

C

U

3/21

8:00

9:50

10

100

听课

理论学时,课件

 

 

 

18:50

20:30

15

85

看书

看《构建之法》

 

 

3/22

18:55

20:55

20

90

编程序

查资料,休息

 

 

3/23

16:30

17:30

 

60

讨论,编程序

讨论,共同写程序

 

 

 

19:00

19:30

 

30

看书

 

 

 

3/24

15:30

18:00

30+10

110

编程序

作业,拿快递,玩手机

 

 

3/25

16:30

18:00

15

75

编程序

休息

 

 

3/26

8:45

10:21

 

96

编程序写报告

 

 

 

 

 

                      

 

 

 

 

 

  缺陷记录日志

                           学生:杜若憧

                      日期:2016/3/25

                      教员:王建民

                      程序号:四则运算3

日期

编号

类型

引入阶段

排除阶段

修复时间

修复缺陷

3/25

1

 

编码

编译

10min

 

描述:负数分数运算输出的结果化简失败

 

2

 

编码

编译

10min

 

描述:分数除法运算,第二个分数分母=0,导致计算结果错误,

 

3

 

编码

 

1min

 

描述:分数计算为0,不需要化简

 

4

 

编码

编译

60min

 

描述:有多个括号时,计算不出结果

posted @   听你笑  阅读(395)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示