日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java:一个分数类的简单设计

發布時間:2025/3/20 java 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java:一个分数类的简单设计 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
這個類對于分數提供化簡和加減乘除四種操作,基于"不變"的設計原則,因此是線程安全的.

其中使用了幾個算法:
[1]Fraction simpler(Fraction f);//分數化簡
[2]Fraction[] RCD(Fraction f1, Fraction f2);//通分
[3]int GCD(int s, int b);//最大公約數
[4]int LCM(int a, int b);//最小公倍數

使用的形式:
[1]在控制臺輸入[分數][回車] //化簡
[2]在控制臺輸入[分數][空格][運算符][空格][分數][回車] //計算
其中[運算符]為+ - * / 之一.

Fraction.java
package net.zj.fraction;

import java.io.IOException;

public class Fraction {
????private int numerator;
????private int denominator;

????public Fraction(int numerator, int denominator) {
????? this.numerator = numerator;
????? this.denominator = denominator;
????}

????public Fraction(int numerator) {
????? this(numerator, 1);
????}

????public static Fraction add(Fraction f1, Fraction f2) {
????? Fraction[] fs = RCD(f1, f2);
????? Fraction add = new Fraction(fs[0].numerator + fs[1].numerator,
??????? fs[0].denominator);
????? return simpler(add);
????}

????public static Fraction minus(Fraction f1, Fraction f2) {
????? Fraction[] fs = RCD(f1, f2);
????? Fraction minus = new Fraction(fs[0].numerator - fs[1].numerator,
??????? fs[0].denominator);
????? return simpler(minus);
????}

????public static Fraction multi(Fraction f1, Fraction f2) {
????? Fraction multi = new Fraction(f1.numerator * f2.numerator,
????? f1.denominator * f2.denominator);
????? return simpler(multi);
????}

????public static Fraction div(Fraction f1, Fraction f2) {
????? return multi(f1, new Fraction(f2.denominator, f2.numerator));
????}

????public static void input(String s) {
????? String[] ss = s.split(" ");
????? if (ss.length == 1) {
??????? Fraction f = StringToFraction(ss[0]);
??????? if (f == null)
????????? output("Usage: Should input a numeric");
??????? else
????????? output(f);
????? } else if (ss.length == 3) {
??????? Fraction f1 = StringToFraction(ss[0]);
??????? Fraction f2 = StringToFraction(ss[2]);
??????? if (f1 == null) {
????????? output("Usage: The first input should be numeric/numeric");
????????? return;
??????? }
??????? if (f2 == null) {
????????? output("Usage: The third input should be numeric/numeric");
????????? return;
??????? }
??????? switch (ss[1].charAt(0)) {
??????? case '+':
????????? output(add(f1, f2));
????????? break;
??????? case '-':
????????? output(minus(f1, f2));
????????? break;
??????? case '*':
????????? output(multi(f1, f2));
?? ? ? ?? break;
?? ?? ? case '/':
? ? ? ? ? output(div(f1, f2));
?? ? ? ?? break;
?? ?? ? default:
?? ? ?? ? output("Usage: The second input should be one of +-*/");
?? ? ?? ? break;
???? ?? }
????? } else
??????output("Usage: Should input one fraction or two fractions and a operator with the style 'f1 + f2'");
????}

????public static void output(Fraction f) {
????? if (f.denominator == 1) {
???? ?? System.out.println(f.numerator);
???? ?? return;
? ? ? }
? ? ? StringBuilder sb = new StringBuilder();
? ? ? sb.append(f.numerator);
? ? ? sb.append('/');
? ? ? sb.append(f.denominator);
????? System.out.println(sb.toString());
????}

????public static void output(String s) {
? ? ? System.out.println(s);
????}

????private static Fraction StringToFraction(String s) {
? ? ? String[] ss = s.split("/");
? ? ? try {
? ?? ?? if (ss.length == 2)
? ? ? ??? return simpler(new Fraction(Integer.valueOf(ss[0]), Integer
?? ? ?? ? ? .valueOf(ss[1])));
??? ? ? else if (ss.length == 1)
? ? ? ? ? return new Fraction(Integer.valueOf(ss[0]));
??????? else
?? ? ? ?? return null;
? ? ? } catch (NumberFormatException e) {
????????? output("Usage: Should input one fraction or two fractions and a operator with the style 'f1 + f2'");
????? }
????? return null;
????}

?/**
??* both the numerator and denominator are divided by GCD
??*/

????private static Fraction simpler(Fraction f) {
? ? ? int gcd = GCD(f.numerator, f.denominator);
? ? ? if (gcd > 1)
??????? return new Fraction(f.numerator / gcd, f.denominator / gcd);
? ? ? else
?? ?? ? return f;
????}

?/**
??* reduction to common denominator
??*/

????private static Fraction[] RCD(Fraction f1, Fraction f2) {
? ? ? int lcm = LCM(f1.denominator, f2.denominator);
? ? ? int m = lcm / f1.denominator;
? ? ? if (m > 1)
?? ?? ? f1 = new Fraction(f1.numerator * m, f1.denominator * m);
? ? ? m = lcm / f2.denominator;
? ? ? if (m > 1)
?? ?? ? f2 = new Fraction(f2.numerator * m, f2.denominator * m);
? ? ? return new Fraction[] { f1, f2 };
????}

?/**
??* greatest common divisor
??*/

????private static int GCD(int s, int b) {
? ? ? // s-small,b-big
? ? ? if (s > b) {
?? ?? ? int temp = s;
??? ? ? s = b;
???? ?? b = temp;
? ? ? }
? ? ? while (b != 0) {
?? ?? ? int temp = s % b;
??? ? ? s = b;
??? ? ? b = temp;
? ? ? }
? ? ? return s;
????}

?/**
??* a lowest common multiple
??*/

????private static int LCM(int a, int b) {
????? return a * b / GCD(a, b);
????}

????public static void main(String[] args) throws IOException {
????? int c;
? ? ? StringBuilder sb = new StringBuilder();
? ? ? while ((c = System.in.read()) != '\n')
? ?? ?? sb.append((char) c);
? ? ? input(sb.toString());
? ? ? }
}



轉載于:https://blog.51cto.com/zhangjunhd/77859

總結

以上是生活随笔為你收集整理的Java:一个分数类的简单设计的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。