首页 > 代码库 > Day10_23

Day10_23

  1 package com.lovo;  2   3 /**  4  * 分数类  5  * @author 叶鹏  6  *  7  */  8 public class Rational {  9     private int num;        // 分子 10     private int den;        // 分母 11      12     /** 13      * 构造器 14      * @param num 分子 15      * @param den 分母 16      */ 17     public Rational(int num, int den) { 18         this.num = num; 19         this.den = den; 20         normalize(); 21     } 22      23     /** 24      * 构造器 25      * @param str 代表一个分数的字符串 26      */ 27     public Rational(String str) { 28         String s1 = str.split("/")[0];    // 分子的字符串 29         String s2 = str.split("/")[1];    // 分母的字符串 30         this.num = Integer.parseInt(s1); 31         this.den = Integer.parseInt(s2); 32         normalize(); 33     } 34      35     /** 36      * 构造器 37      * @param x 一个小数 38      */ 39     public Rational(double x) { 40         this.num = (int) (x * 10000); 41         this.den = 10000; 42         simplify(); 43         normalize(); 44     } 45      46     /** 47      * public   Rational add(Rational other) { ... } 48      * 访问修饰符  返回类型     方法名(参数列表)       { ... } 49      * 加法 50      * @param other 另一个分数 51      * @return 两个分数相加的结果 52      */ 53     public Rational add(Rational other) { 54         return new Rational(this.num * other.den + this.den * other.num, this.den * other.den).normalize(); 55     } 56      57     /** 58      * 减法 59      * @param other 另一个分数 60      * @return 两个分数相减的结果 61      */ 62     public Rational sub(Rational other) { 63         return new Rational(this.num * other.den - this.den * other.num, this.den * other.den).normalize(); 64     } 65      66     /** 67      * 乘法 68      * @param other 另一个分数 69      * @return 两个分数相乘的结果 70      */ 71     public Rational mul(Rational other) { 72         return new Rational(this.num * other.num, this.den * other.den).normalize(); 73     } 74      75     /** 76      * 除法 77      * @param other 另一个分数 78      * @return 两个分数相除的结果 79      */ 80     public Rational div(Rational other) { 81         return new Rational(this.num * other.den, this.den * other.num).normalize(); 82     } 83      84     /** 85      * 化简 86      * @return 化简后的分数对象 87      */ 88     public Rational simplify() { 89         if(num != 0) { 90             int divisor = gcd(Math.abs(num), Math.abs(den)); 91             num /= divisor; 92             den /= divisor; 93         } 94         return this; 95     } 96      97     /** 98      * 正规化 99      * @return 正规化以后的分数对象100      */101     public Rational normalize() {102         if(this.num == 0) {103             this.den = 1;104         }105         else if(this.den < 0) {106             this.den = - this.den;107             this.num = - this.num;108         }109         return this;110     }111     112     public String toString() {113         return num + (den != 1? ("/" + den) : "");114     }115     116     private int gcd(int x, int y) {117         if(x > y) {118             int temp = x;119             x = y;120             y = temp;121         }122         for(int i = x; i > 1; i--) {123             if(x % i == 0 && y % i == 0) {124                 return i;125             }126         }127         return 1;128     }129 }
 1 package com.lovo; 2  3 import java.util.Scanner; 4  5 public class Test01 { 6  7     public static void main(String[] args) {         8         Scanner sc = new Scanner(System.in); 9         System.out.print("r1 = ");10         String str1 = sc.next();11         Rational r2 = new Rational(0.3);12         13         Rational r1 = new Rational(str1);14         15         System.out.printf("%s + %s = %s\n", r1, r2, r1.add(r2).simplify());16         System.out.printf("%s - %s = %s\n", r1, r2, r1.sub(r2).simplify());17         System.out.printf("%s * %s = %s\n", r1, r2, r1.mul(r2).simplify());18         System.out.printf("%s / %s = %s\n", r1, r2, r1.div(r2).simplify());19         20         sc.close();21     }22 }

 

Day10_23