课外天地 李树青学习天地Java程序语言课件 → 程序代码——分数类(欢迎大家测试和探讨)


  共有16949人关注过本帖树形打印复制链接

主题:程序代码——分数类(欢迎大家测试和探讨)

帅哥哟,离线,有人找我吗?
admin
  1楼 博客 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信 管理员
等级:管理员 帖子:1945 积分:26736 威望:0 精华:34 注册:2003/12/30 16:34:32
程序代码——分数类(欢迎大家测试和探讨)  发帖心情 Post By:2006/2/26 21:49:55 [显示全部帖子]

1、最为简单的分数类:
public class Fraction
{
        //分子
        private long numerator;
        //分母
        private long denominator;
        
        //构造函数
        public Fraction()
        {}
        
        public Fraction(long n,long d)
        {
                setFraction(n,d);
        }
        
        //分子的设置方法
        public void setNumerator(long n)
        {
                numerator=n;
        }
        
        //分子的读取方法
        public long getNumerator()
        {
                return numerator;
        }
        
        //分母的设置方法,可以检查是否为零
        public void setDenominator(long d)
        {
                if(d==0)
                        denominator=1;
                else
                        denominator=d;
        }
        
        //分母的读取方法        
        public long getDenominator()
        {
                return denominator;
        }
        
        //设置分数的方法
        public void setFraction(long n,long d)
        {
                setNumerator(n);
                setDenominator(d);
        }
        
        //注意,此方法可以有效的解决负分数的显示问题,将负号放于最前面
        public String toString()
        {
                if(numerator*denominator<0)
                        return "-"+Math.abs(numerator)+"/"+Math.abs(denominator);
                else
                        return Math.abs(numerator)+"/"+Math.abs(denominator);
        }
}

测试程序为:
public class exec
{
        public static void main(String[] args)
        {
                Fraction f1=new Fraction();
                f1.setNumerator(1);
                f1.setDenominator(2);
                System.out.println(f1);
                
                Fraction f2=new Fraction(2,3);
                System.out.println(f2);        
        }
}

2、支持对象比较的分数类:
添加新的方法:

//测试对象是否一样
public boolean equals(Fraction f)
{
        if(numerator==f.numerator && denominator==f.denominator)
                return true;
        else
                return false;
}
        
//测试对象的大小,注意类型转换,否则会丢失精度
public boolean isGreater(Fraction f)
{
        if((double)numerator/denominator>(double)f.numerator/f.denominator)
                return true;
        else
                return false;
}


测试程序为:
public class exec
{
        public static void main(String[] args)
        {
                Fraction f1=new Fraction(1,2);
                Fraction f2=new Fraction(2,3);
                if(f1.equals(f2))
                        System.out.println("==");      
                if(f2.isGreater(f1))
                        System.out.println(">");        
        }
}

3、支持约分和将小数转变为分数的分数类:
添加新的方法:

//将浮点型数值转换为相应的分数,思路为反复将浮点型数值乘以10,最终以得到整数为止,这个整数即为分子,连续相乘的数值为分母
public Fraction(double d)
{
        long decimalCount=1;
                
        int dLen=String.valueOf(d).length();
                
        for(int i=0;i<dLen;i++)
        {
                d=d*10;
                decimalCount*=10;
                        
        }
        numerator=(long)d;
        denominator=decimalCount;
                
        Reduction();
}      
        

//实现约分的方法,思路为把分子从大到小所有的数值分别和分母和分子同时相除,一旦两者皆能整除时,商即是约分后的结果
private void Reduction()
{
        long minValue=Math.min(Math.abs(numerator),Math.abs(denominator));
                
        for(long i=minValue;i>=1;i--)
        {
                if(numerator%i==0 && denominator%i==0)
                {
                        numerator=numerator/i;
                        denominator=denominator/i;
                        break;
                }
        }
}


测试程序为:
public class exec
{
        public static void main(String[] args)
        {
                Fraction f1=new Fraction(-12,-24);
                System.out.println(f1);
                
                Fraction f2=new Fraction(0.24);
                System.out.println(f2);
        }
}

[此贴子已经被作者于2010-12-12 07:55:16编辑过]

 回到顶部