日期:2014-05-20  浏览次数:20797 次

谁能帮帮我,JAVA如何约分结果。
Java code

import java.util.*;
public class Rational<finalfenzi> {
    private int fenzi;
    private int fenmu;      
    public int getfenzi(){return fenzi;}
    public void setfenzi(int fenzi){this.fenzi=fenzi;}
    public int getfenmu(){return fenmu;}    
    public void setY(int fenmu){this.fenmu=fenmu; }
    public Rational (int fenzi,int fenmu){ 
          this.fenzi=fenzi;   
         if(fenmu==0){ 
       System.out.print("fenmu cannot be zero!");    
            }        
         this.fenmu=fenmu;    
         }    
        public String toString()
        { 
        return ""+fenzi+"/"+fenmu;
     }
        public Rational common(int finalfenzi, int finalfenmu) {
            Rational rational = new Rational(finalfenzi,finalfenmu);
            if(finalfenmu < 0 && finalfenzi > 0 || finalfenmu < 0 && finalfenzi < 0)
            rational = new Rational(-finalfenzi,-finalfenmu);
            return rational;  //结果正负号;
            }

        public Rational add(Rational a){
            int finalfenmu=this.fenmu*a.getfenmu();    
            int finalfenzi=this.fenzi*a.getfenmu()+a.getfenzi()*this.fenmu;                                        
            return common(finalfenzi,finalfenmu);   //加法;
     
        }
       public Rational subtract(Rational a){  
           int finalfenmu=this.fenmu*a.getfenmu();
           int finalfenzi=this.fenzi*a.getfenmu()-a.getfenzi()*this.fenmu;        
       return common(finalfenzi,finalfenmu);    //减法;
    }  
       public Rational multiply(Rational a){  
           int finalfenmu=this.fenmu*a.getfenmu();    
            int finalfenzi=this.fenzi*a.getfenzi();        
            return common(finalfenzi,finalfenmu);    //乘法;
     } 
      public Rational divide(Rational a){    
        int finalfenmu=this.fenmu*a.getfenzi();     
           int finalfenzi=this.fenzi*a.getfenmu();        
            return common(finalfenzi,finalfenmu);    //除法;
            }      
      
      
    public static void main(String[] args) {    
         Scanner in=new Scanner(System.in);
           System.out.print("Enter the first fenzi=");   
           int a = in.nextInt();
           System.out.print("Enter the first fenmu=");
           int b = in.nextInt();    
           System.out.print("Enter the second fenzi=");
           int c = in.nextInt();
           System.out.print("Enter the second fenmu=");
           int d = in.nextInt();
        Rational s=new Rational(a,b);    
        Rational s1=new Rational(c,d);                
            Rational s2=s.add(s1);        
        System.out.println(s+"+"+s1+"="+s2);        
            Rational s3=s.subtract(s1);
        System.out.println(s+"-"+s1+"="+s3);        
            Rational s4=s.multiply(s1);        
        System.out.println(s+"*"+s1+"="+s4);        
            Rational s5=s.divide(s1);    
        System.out.println(s+"/"+s1+"="+s5);
        
}
}



应该插入什么方法比较好?

------解决方案--------------------
加个约分方法
Java code
public Rational reduce(){
    int fz = Math.abs(this.fenzi); //取分子分母的绝对值
    int fm = Math.abs(this.fenmu);
    int mod = fz%fm;
    while (mod > 0) { //求分子分母的最大公因数
        fz = fm;
        fm = mod;
        mod = fz%fm;
    }
    return common(this.fenzi/fm, this.fenmu/fm); //分子分母都除以最大公因数
}