测试项目:减一四则运算生成程序

项目成员:张金生  张政

工程地址:https://coding.net/u/jx8zjs/p/paperOne/git

ssh://git@git.coding.net:jx8zjs/paperOne.git

 

测试单元概览

1. Fraction: 分数类,支持分数加减乘除法,约分,取相反数等

2.QuestionGen:题目生成类,支持生成各种难度的题目,和答案。

 

待测单元:

Fraction类:

 1 public class Fraction {
 2     public int up;
 3     public int down;
 4 
 5     public Fraction(int up, int down) {
 6         if (down == 0 || up == 0) {
 7             System.out.println("divided by zero error");
 8             return;
 9         }
10         int smaller = up > down ? up : down;
11         int maxCommonFactor = 1;
12         for (int i = 1; i <= smaller; i++) {
13             if (up % i == 0 && down % i == 0) {
14                 maxCommonFactor = i;
15             }
16         }
17 
18         this.up = up / maxCommonFactor;
19         this.down = down / maxCommonFactor;
20     }
21 
22 
23     public Fraction gcd(Fraction f) {
24         int smaller = f.up > f.down ? f.up : f.down;
25         int maxCommonFactor = 1;
26         for (int i = 1; i <= smaller; i++) {
27             if (f.up % i == 0 && f.down % i == 0) {
28                 maxCommonFactor = i;
29             }
30         }
31         f.up = f.up / maxCommonFactor;
32         f.down = f.down / maxCommonFactor;
33 
34         return f;
35     }
36 
37     public String toString() {
38         if (down == 1)
39             return "" + up;
40         if(Math.abs(up)/down>0){
41             return up>0?up/down+" "+up%down+"/"+down:"-"+Math.abs(up)/down+" "+Math.abs(up)%down+"/"+down;
42         }
43         return up + "/" + down;
44     }
45 
46     public Fraction add(Fraction f) {
47         Fraction a = new Fraction(up, down);
48         a.up = f.up * a.down + a.up * f.down;
49         a.down = a.down * f.down;
50 
51         return a.gcd(a);
52     }
53 
54     public Fraction minus(Fraction f) {
55         Fraction a = new Fraction(up, down);
56         a.up = a.up * f.down - f.up * a.down;
57         a.down = a.down * f.down;
58 
59         return a.gcd(a);
60     }
61 
62     public Fraction multiply(Fraction f) {
63         Fraction a = new Fraction(up, down);
64         a.up = a.up * f.up;
65         a.down = a.down * f.down;
66         return a.gcd(a);
67     }
68 
69     public Fraction divide(Fraction f) {
70         Fraction a = new Fraction(up, down);
71         a.up = a.up * f.down;
72         a.down = a.down * f.up;
73         return a.gcd(a);
74     }
75 
76     public Fraction changeSign(){
77         up = -up;
78         return this;
79     }
80 
81     public static Fraction getRandiom(int Max) {
82         return new Fraction((int) (Math.random() * Max / 2) + 1, (int) (Math.random() * Max / 2) + 1);
83     }
84 
85     public static Fraction getRandiom(int Max, boolean isInt) {
86         return new Fraction((int) (Math.random() * Max / 2) + 1, isInt ? 1 : (int) (Math.random() * Max / 2) + 1);
87     }
View Code

相关文章:

  • 2021-10-27
  • 2021-12-18
  • 2021-07-21
猜你喜欢
  • 2021-05-20
  • 2022-02-05
  • 2021-08-31
  • 2021-12-27
  • 2022-01-13
相关资源
相似解决方案