網絡釋義
1.分數;分式
數學英語詞彙 (F-H)-英語詞彙彙總-詞...
fraction 分數;分式
.2.份數;餾分
紙業專業英語詞彙翻譯(F3)-英語閱讀網 ...
fraction 份數;餾分
.3.部分,成分
醫學翻譯詞彙F - 『原版英語』 閱讀網
fraction 部分,成分
c語言中
例:創建一個Fraction類執行分數運算。要求如下:(1)用整形數表示類的private成員變量:f1和f2.(2)提供構造方法,将分子存入f1和f2,分母存入f2.。1)用整形數表示類的private成員變量:f1和f2.(2)提供構造方法,将分子存入f1和f2,分母存入f2.。
public class Fraction {
public static void main(String[] args) {
//測試代碼
Fraction f1 = new Fraction(1, 2);
Fraction f2 = new Fraction(1, 3);
System.out.println("分數f1 ==" + f1);
System.out.println("分數f2 ==" + f2);
System.out.println("分數f1 浮點數 ==" + f1.doubleValue());
System.out.println("分數f2浮點數 ==" + f2.doubleValue());
System.out.println("分數f1+f2 ==" + f1.add(f2));
System.out.println("分數f1-f2 ==" + f1.sub(f2));
System.out.println("分數f1*f2 ==" + f1.multiply(f2));
System.out.println("分數f1/f2 ==" + f1.div(f2));
}
private int f1; //分子
private int f2; //分母
public Fraction(int f1, int f2) {
if(f2 == 0){ throw new IllegalArgumentException("分母不能為0");}
this.f1 = f1;
this.f2 = f2;
}
/** [+] */
public Fraction add(Fraction other){
int fm = this.f2 * other.f2;
int fz = this.f1 * other.f2 + other.f1 * this.f2;
return new Fraction(fz, fm);
}
/** [-] */
public Fraction sub(Fraction other){
int fm = this.f2 * other.f2;
int fz = this.f1 * other.f2 - other.f1 * this.f2;
return new Fraction(fz, fm);
}
/** [*] */
public Fraction multiply(Fraction other){
int fm = this.f2 * other.f2;
int fz = this.f1 * other.f1;
return new Fraction(fz, fm);
}
/** [/] */
public Fraction div(Fraction other){
if(other.doubleValue() == 0.0 ){
throw new IllegalArgumentException("0不能做為除數");
}
int fm = this.f2 * other.f1;
int fz = this.f1 * other.f2;
return new Fraction(fz, fm);
}
public String toString(){
return f1 + "/" + f2;
}
public double doubleValue(){
return 1.0 * f1 / f2;
}
}