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 }






浙公网安备 33010602011771号