⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fraction.java

📁 [数学问题]用分支定界发解线性混合整数规划问题
💻 JAVA
字号:
package com.sea0108.simpmethod;

public final class Fraction
{
	public long b; //numerator
	public long a; //denominator;
	public static final Fraction MAX_VALUE = new Fraction(Integer.MAX_VALUE);
	
	public Fraction(long b,long a)
	{
		long gcd = greatestCommonDivisor(a,b);
		int c = a<0 ? -1 : 1;
		this.a = c*a/gcd;
		this.b = c*b/gcd;
	}
	
	public Fraction(String s)
	{
		int i = s.indexOf("/");
		int k = s.indexOf(".");
		if(i==-1 && k==-1)
		{
			b = Long.valueOf(s);
			a = 1;
		}
		else
		{	
			Fraction f;
			if(i>-1 && k==-1)			
				f = new Fraction(Long.valueOf(s.substring(0,i)),Long.valueOf(s.substring(i+1)));		
			else if(i ==-1 && k>-1)		
				f = new Fraction(Double.valueOf(s));			
			else
				f = Fraction.divide(new Fraction(Double.valueOf(s.substring(0,i))),
				                           new Fraction(Double.valueOf(s.substring(i+1))));			
			b = f.b;
			a = f.a;
		}
	}
	
	public Fraction(long b)
	{	
		this.a = 1;
		this.b = b;
	}
	
	public Fraction(double d)
	{
		String s = String.valueOf(d);
		int i =s.indexOf(".");
		if(i==-1)
		{		
			b = Long.valueOf(s);
			a = 1;
		}
		else
		{
			int neg = s.contains("-")? -1:1;
			long p = neg*Long.valueOf(s.substring(0,i));	
			String ss = s.substring(i+1);
			long q = Long.valueOf(ss);
			long r = (long)Math.pow(10,ss.length());
			Fraction f = Fraction.add(new Fraction(p),Fraction.divide(new Fraction(q),new Fraction(r)));
			b = neg*f.b;
			a = f.a;			
		}
	}
	
	private static long greatestCommonDivisorPositive(long p,long q)
	{
		if(p<q)
			return greatestCommonDivisorPositive(q,p);
		else
			return q==0 ? p : greatestCommonDivisorPositive(q,p%q);
	}
	
	public static long greatestCommonDivisor(long p,long q)
	{
		return greatestCommonDivisorPositive(Math.abs(p),Math.abs(q));
	}	
	
	public static Fraction add(Fraction fp,Fraction fq)
	{
		return new Fraction(fp.b*fq.a + fp.a*fq.b,  fp.a*fq.a);
	}
	
	public static Fraction minus(Fraction fp,Fraction fq)
	{
		return new Fraction(fp.b*fq.a - fp.a*fq.b,  fp.a*fq.a);
	}
	
	public static Fraction multiply(Fraction fp,Fraction fq)
	{
		return new Fraction(fp.b*fq.b, fp.a*fq.a);
	}
	
	public static Fraction divide(Fraction fp,Fraction fq)
	{
		return new Fraction(fp.b*fq.a, fp.a*fq.b);
	}
	
	public boolean isInteger()
	{
		return a==1? true : false; 
	}
	
	public Fraction contraryNumber()
	{
		return new Fraction(-this.b,this.a);
	}
	
	public void contrary()
	{
		b *= -1;
	}
	
	public boolean equals(Fraction fq)
	{
		if(this.b == fq.b && this.a==fq.a)
			return true;
		else
			return false;
	}
	
	public int compareTo(Fraction fq)
	{
		Fraction f = minus(this,fq);
		return f.b>0 ? 1 : (f.b ==0 ? 0 : -1);
	}
	
	public String toString()
	{
		if(a==1)
			return String.valueOf(b);
		else
			return b + "/" + a;
	}
	
	public Fraction abs()
	{
		return new Fraction(Math.abs(b),a);
	}
	
	public Fraction floor()
	{
		return new Fraction(b/a);
	}
	
	public Fraction ceil()
	{
		return new Fraction(b/a + 1);
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -