hon sojava
import java.util.*;
class HonSo extends PhanSo
{
private double t,m,z;
HonSo(){};
HonSo(double t,double m,double z)
{
super(t,m);
this.z=z;
}
HonSo(PhanSo ps,double z)
{
super(ps.t,ps.m);
this.z=z;
}
HonSo(HonSo p)
{
super(p.t,p.m);
this.z=p.z;
}
public String toString()
{
return "("+t+" /"+m+"*"+z+")";
}
public HonSo cong(HonSo p)
{
this.t = this.z * this.m + this.t;
PhanSo ps = super.cong(new PhanSo( p.z * p.m + p.t, p.m) );
if(ps.t/ps.m>0)
{
return new HonSo(ps.t%ps.m,ps.m,ps.t/ps.m);
}
else
return new HonSo(ps.t%ps.m,ps.m,1);
}
public HonSo tru(HonSo p)
{
this.t = this.z * this.m + this.t;
PhanSo ps = super.tru(new PhanSo( p.z * p.m + p.t, p.m) );
if(ps.t/ps.m>0)
{
return new HonSo(ps.t%ps.m,ps.m,ps.t/ps.m);
}
else
return new HonSo(ps.t%ps.m,ps.m,1);
}
public HonSo nhan(HonSo p)
{
this.t = this.z * this.m + this.t;
PhanSo ps = super.nhan(new PhanSo( p.z * p.m + p.t, p.m) );
if(ps.t/ps.m>0)
{
return new HonSo(ps.t%ps.m,ps.m,ps.t/ps.m);
}
else
return new HonSo(ps.t%ps.m,ps.m,1);
}
public HonSo chia(HonSo p)
{
this.t = this.z * this.m + this.t;
PhanSo ps = super.chia(new PhanSo( p.z * p.m / p.t, p.m) );
if(ps.t/ps.m>0)
{
return new HonSo(ps.t%ps.m,ps.m,ps.t/ps.m);
}
else
return new HonSo(ps.t%ps.m,ps.m,1);
}
public static void main(String[] args)
{
PhanSo t=new PhanSo(3,4);
PhanSo m=new PhanSo(6,7);
System.out.println(t.cong(m));
System.out.println(t.tru(m));
System.out.println(t.nhan(m));
System.out.println(t.chia(m));
}
}
Bạn đang đọc truyện trên: AzTruyen.Top