|
import java.util.*;
import java.math.*;
class fraction{//分数类
BigInteger a, b;//a/b,b>0;
fraction(){//构造函数1
a = new BigInteger("0");
b = new BigInteger("1");
}
fraction(BigInteger val){//构造函数2
a = new BigInteger(val.toString());
b = new BigInteger("1");
}
fraction(String a){
this.a=new BigInteger(a);
this.b=new BigInteger("1");
}
fraction( BigInteger a0, BigInteger b0){//构造函数3
this.a = a0; this.b = b0;
this.reduction();
}
fraction(String a,String b){
this.a=new BigInteger(a);
this.b=new BigInteger(b);
this.reduction();
}
fraction( fraction other){
this.a=new BigInteger(other.a.toString());
this.b=new BigInteger(other.b.toString());
}
void reduction(){//化简
BigInteger tmp = a.gcd( b );//其值是 abs(a) 和 abs(b) 的最大公约数。
a = a.divide( tmp );
b = b.divide( tmp );
if( b.compareTo( BigInteger.ZERO ) == - 1 )
{
b = b.multiply( BigInteger.valueOf( -1 ));
a = a.multiply( BigInteger.valueOf( -1 ));
}
}
fraction add( fraction t ){//加一个
fraction tmp = new fraction( a.multiply( t.b ).add( b.multiply( t.a )) , b.multiply(t.b) );
tmp.reduction();
return tmp;
}
fraction sub( fraction t ){//减
fraction tmp = new fraction( a.multiply( t.b ).subtract( b.multiply( t.a )) , b.multiply(t.b) );
tmp.reduction();
return tmp;
}
fraction mult( fraction t){//乘
fraction tmp = new fraction( a.multiply( t.a ), b.multiply( t.b ));
tmp.reduction();
return tmp;
}
fraction div( fraction t){//除
fraction tmp = new fraction( a.multiply( t.b ), b.multiply( t.a ));
tmp.reduction();
return tmp;
}
fraction abs(){
fraction tmp=new fraction(this);
tmp.a=tmp.a.abs();
return tmp;
}
void show(){
this.reduction();
if( b.compareTo( BigInteger.ONE ) == 0 )
System.out.println(a);
else
System.out.println(a+"/"+b);
}
boolean Biger( fraction p )
{
return this.sub(p).a.compareTo(BigInteger.ZERO)==1;
}
int zero()
{
this.reduction();
return this.a.compareTo(BigInteger.ZERO);
}
}
public class Main
{
public static void main(String args[]){
/*2x1+2x2-x3=6
* x1-2x2+4x3=4
*5x1+7x2+x3=28
*/
fraction a[][]={{ new fraction("2"),new fraction("2"),new fraction("-1")},
{ new fraction("1"),new fraction("-2"),new fraction("4")},
{ new fraction("5"),new fraction("7"),new fraction("1")}};
fraction b[]={new fraction("6"),new fraction("3"),new fraction("28")};
gauss_cpivot(3,a,b);
for(int i=0;i< 3;i++){
b.show();
}
}
static boolean gauss_cpivot(int n,fraction a[][],fraction b[])
{//高斯消元法解方程组,有唯一解返回true,结果放在b[]中,否则返回false
int i,j,k,row=0;
for(k=0;k< n;++k)
{
fraction maxp=new fraction();
for(i=k;i< n;++i)
if(a[k].abs().Biger(maxp.abs()))
{
row=i;
maxp=new fraction(a[k]);
}
if(maxp.abs().zero()==0)return false;
if(row!=k)
{
for(j=k;j< n;++j)
{
fraction t=new fraction(a[k][j]);
a[k][j]=a[row][j];
a[row][j]=t;
}
fraction t=new fraction(b[k]);
b[k]=b[row];
b[row]=t;
}
for(j=k+1;j< n;++j)
{
a[k][j]=a[k][j].div(maxp);
for(i=k+1;i< n;++i)
a[j]=a[j].sub(a[k].mult(a[k][j]));
}
b[k]=b[k].div(maxp);
for(i=k+1;i< n;++i)
b=b.sub(b[k].mult(a[k]));
}
for(i=n-1;i>=0;--i)
for(j=i+1;j< n;++j)
b=b.sub(b[j].mult(a[j]));
return true;
}
}
运行:
1
3
2 |
|