1. 程式人生 > 實用技巧 >2020.8.30

2020.8.30

學習內容

分數類

  1 import java.util.Scanner;
  2 //分數類
  3 public class Fraction {
  4     private int x;
  5     private int y;
  6     public Fraction() {
  7     }
  8     public Fraction(int x, int y) {
  9         this.x = x;
 10         this.y = y;
 11     }
 12     // 分數的輸入
 13     public void input() {
 14         Scanner in = new Scanner(System.in);
 15         x = in.nextInt();
 16         y = in.nextInt();
 17     }
 18     // 分數的輸出
 19     public void output() {
 20 
 21         if (y < 0) {
 22             x = -x;
 23             y = -y;
 24         }
 25         System.out.println(x + "/" + y);
 26     }
 27     // 求分子、分母的最大公因數
 28     public static int max(int a, int b) {
 29         int c = 0;
 30         while (a % b != 0) {
 31             c = a % b;
 32             a = b;
 33             b = c;
 34         }
 35         return c;
 36     }
 37     // 化簡分數
 38     public Fraction simp() {
 39         int p = max(x, y);
 40         x = x / p;
 41         y = y / p;
 42         Fraction f = new Fraction(x, y);
 43         return f;
 44     }
 45     // 分數的加法
 46     public Fraction add(Fraction f) {
 47         int p = max(x * f.y + y * f.x, y * f.y);
 48         if (y == f.y) {
 49             Fraction c = new Fraction(x + f.x, y);
 50             return c;
 51         } else {
 52             Fraction c = new Fraction((x * f.y + y * f.x) / p, (y * f.y) / p);
 53             return c;
 54         }
 55     }
 56     // 分數的減法
 57     public Fraction sub(Fraction f) {
 58         int p = max(x * f.y - y * f.x, y * f.y);
 59         if (y == f.y) {
 60             Fraction c = new Fraction(x - f.x, y);
 61             return c;
 62         } else {
 63             Fraction c = new Fraction((x * f.y - y * f.x) / p, (y * f.y) / p);
 64             return c;
 65         }
 66     }
 67     // 分數的數乘
 68     public Fraction mul(int n) {
 69         int p = max(x * n, y);
 70         Fraction c = new Fraction((x * n) / p, y / p);
 71         return c;
 72     }
 73 
 74     // 判斷關係:分數1==分數2
 75     public boolean dengYu(Fraction c) {
 76         int p1 = max(x, y);
 77         int p2 = max(c.x, c.y);
 78         if (x / p1 == c.x / p2 && y / p1 == c.y / p2) {
 79             return true;
 80         } else {
 81             return false;
 82         }
 83     }
 84     // 判斷關係:分數1!=分數2
 85     public boolean buDengYu(Fraction c) {
 86         int p1 = max(x, y);
 87         int p2 = max(c.x, c.y);
 88         if (x / p1 == c.x / p2 && y / p1 == c.y / p2) {
 89             return false;
 90         } else {
 91             return true;
 92         }
 93     }
 94     // 判斷關係:分數1>=分數2
 95     public boolean daYu(Fraction c) {
 96         int n1 = x * c.y;
 97         int n2 = y * c.x;
 98         if (n1 >= n2) {
 99             return true;
100         } else {
101             return false;
102         }
103     }
104     // 判斷關係:分數1<=分數2
105     public boolean xiaoYu(Fraction c) {
106         int n1 = x * c.y;
107         int n2 = y * c.x;
108         if (n1 <= n2) {
109             return true;
110         } else {
111             return false;
112         }
113     }
114     public static void main(String[] args) {
115         int t, n;
116         Fraction f1 = new Fraction();
117         Fraction f2 = new Fraction();
118         // 選單列印
119         System.out.println("功能選擇:");
120         System.out.println("**************");
121         System.out.println("1.分數加法");
122         System.out.println("2.分數減法");
123         System.out.println("3.分數數乘");
124         System.out.println("4.分數關係比較");
125         System.out.println("**************");
126         System.out.print("請輸入你的選擇:");
127         Scanner in = new Scanner(System.in);
128         t = in.nextInt();
129         if (t == 1) {
130             System.out.print("請輸入分數1:");
131             f1.input();
132             System.out.print("請輸入分數2:");
133             f2.input();
134             System.out.print("相加結果:");
135             (f1.add(f2)).output();
136         }
137         if (t == 2) {
138             System.out.print("請輸入分數1:");
139             f1.input();
140             System.out.print("請輸入分數2:");
141             f2.input();
142             System.out.print("相減結果:");
143             (f1.sub(f2)).output();
144         }
145         if (t == 3) {
146             System.out.print("請輸入分數:");
147             f1.input();
148             System.out.print("請輸入乘數:");
149             n = in.nextInt();
150             System.out.print("數乘結果:");
151             (f1.mul(n)).output();
152         }
153         if (t == 4) {
154             System.out.print("請輸入分數1:");
155             f1.input();
156             System.out.print("請輸入分數2:");
157             f2.input();
158             if (f1.daYu(f2)) {
159                 System.out.print("分數1==分數2");
160             }
161             if (f1.buDengYu(f2)) {
162                 System.out.println("分數1!=分數2");
163             }
164             if (f1.daYu(f2)) {
165                 System.out.println("分數1>=分數2");
166             }
167             if (f1.xiaoYu(f2)) {
168                 System.out.println("分數1<=分數2");
169             }
170         }
171     }
172 }