- Java code
package learn;//一个主要的想法就是,怎么能让分数最简化,也就是去掉分子和分母的相同公约数。public class fraction { /**创建一个Fraction类执行分数运算,要求如下: * 1.用整形数表示类的private成员变量:f1和f2。 * 2.提供构造函数,将分子存入f1,分母存入f2。 * 3.提供两个分数相加的运算方法,结果分子存入f1,分母存入f2。 * 4.提供两个分数相减的运算方法,结果分子存入f1,分母存入f2。 * 5.提供两个分数相乘的运算方法,结果分子存入f1,分母存入f2。 * 6.提供两个分数相除的运算方法,结果分子存入f1,分母存入f2。 * 7.以f1/f2的形式打印Fraction数。 * 8.以浮点数的形式打印Fraction数。 * 9.编写主函数运行分数运算。 * @param args */ private static int []f1=new int[2056]; private static int []f2=new int[2056]; private static double j1,j11; private static double j2,j22; private static double c1,c11; private static double c2,c22; public static void getf(String[] args){ fraction.f1[0]=Integer.parseInt(args[0]); fraction.f1[1]=Integer.parseInt(args[1]); fraction.f2[0]=Integer.parseInt(args[2]); fraction.f2[1]=Integer.parseInt(args[3]); } public static double jia(){ j1=fraction.f1[0]*fraction.f2[1]+fraction.f1[1]*fraction.f2[0]; j11=fraction.f2[1]*fraction.f2[0]; return j1/j11; } public static double jian(){ j2=fraction.f1[0]*fraction.f2[1]-fraction.f1[1]*fraction.f2[0]; j22=fraction.f2[1]*fraction.f2[0]; return j2/j22; } public static double cheng(){ c1=fraction.f1[0]*fraction.f1[1]; c11=fraction.f2[0]*fraction.f2[1]; return c1/c11; } public static double chu(){ c2=fraction.f1[0]*fraction.f2[1]; c22=fraction.f2[0]*fraction.f1[1]; return c2/c22; } public static void main(String[] args) { // TODO Auto-generated method stub fraction.f1[0]=Integer.parseInt(args[0]); fraction.f1[1]=Integer.parseInt(args[2]); fraction.f2[0]=Integer.parseInt(args[1]); fraction.f2[1]=Integer.parseInt(args[3]);//这段写上了,想当于上面的这个构造函数没有用。怎么调用前面的方法呢? System.out.println("输入的分数是:"+fraction.f1[0]+"/"+fraction.f2[0]+" "+fraction.f1[1]+"/"+fraction.f2[1]); System.out.println("浮点数形式是:"+(double)(fraction.f1[0])/(double)(fraction.f2[0])+" "+(double)(fraction.f1[1])/(double)(fraction.f2[1])); System.out.println("相加的结果是:"+fraction.j1+"/"+fraction.j11+" or "+fraction.jia());//像j1这种变量,我怎么能把他们在前面方法的值调用出来?设成全局变量? System.out.println("相减的结果是:"+fraction.j2+"/"+fraction.j22+" or "+fraction.jian()); System.out.println("相乘的结果是:"+fraction.c1+"/"+fraction.c11+" or "+fraction.cheng()); System.out.println("相除的结果是:"+fraction.c2+"/"+fraction.c22+" or "+fraction.chu()); }}
------解决方案--------------------
原来使用BigInteger写了个用于大数操作的分数类,仅实现了加法,可以参考一下,自行改成基本数据类型吧
public class MyFraction {
private BigInteger numerator;
private BigInteger denominator;
public MyFraction(int numerator, int denominator) {
this.numerator = new BigInteger(numerator + "");
this.denominator = new BigInteger(denominator + "");
}
public MyFraction(BigInteger numerator, BigInteger denominator) {
this.numerator = numerator;
this.denominator = denominator;
}
public MyFraction add(MyFraction fra) {
BigInteger num1 = this.numerator.multiply(fra.denominator);
BigInteger num2 = fra.numerator.multiply(this.denominator);
BigInteger num = num1.add(num2);
BigInteger den = this.denominator.multiply(fra.denominator);
return new MyFraction(num, den);
}
/**
* 化简当前分数,这里采用了BigInteger的求最大公约数方法
*/
public void reduce() {
BigInteger gcd = numerator.abs().gcd(denominator);