java自定义大数(进制可在2-62之间相互转换)

java自定义大数(进制可在2~62之间相互转化)

闲着没事做,写了一个自定义大数,并实现了两个大数的加减乘除以及比较,

以及2~62进制任意数(包括但不限于正整数与负整数)的相互转化

大数类: BigNumber.java /*

* 0~9用0~9表示,10~35用A~Z表示,36~61用a~z表示 */

@SuppressWarnings(\)

public class BigNumber extends Exception{

private String MyNumber;//大数 private int SystemNumber;//进制数 public BigNumber(){ }

this(\,10);

public BigNumber(String MyNumber){ this(MyNumber,10);

}

public BigNumber(String MyNumber,int SystemNumber){ this.MyNumber=MyNumber;

this.SystemNumber=SystemNumber; this.Islegal();

}

public String getMyNumber() { }

public void setMyNumber(String myNumber) { }

public int getSystemNumber() { }

public void setSystemNumber(int systemNumber) { }

this.SystemNumber = systemNumber; return this.SystemNumber; this.MyNumber = myNumber; return this.MyNumber;

public void Islegal(){//判断该字符串是否合法

if(this.MyNumber==null||this.MyNumber.length()==0){//大数为空或大数字符串长度为0

throw (new NumberFormatException(\错误!大数为空或大数字符串长度为0

\+this.MyNumber)); }

if(this.SystemNumber<=1||this.SystemNumber>=63){//进制数不合法

throw (new NumberFormatException(\错误!进制数不合法

\+this.SystemNumber)); }

if(this.MyNumber.equals(\)||this.MyNumber.equals(\)||this.MyNumber.

throw (new NumberFormatException(\错误!大数不合法 \+this.MyNumber));

equals(\)){ }

if(this.MyNumber.equals(\)||this.MyNumber.equals(\)){ }

for(int i=0,a,k=-1;i

protected static String Add_Positive(String s1,String s2,int n){//自定义两个正数加法运算 String

Ins1=getInteger(s1),Dos1=getDecimal(s1),Ins2=getInteger(s2),Dos2=getDecimal(s2);//分别获取整数部分及小数部分

int Inlength=Math.max(Ins1.length(),

Ins2.length()),Dolength=Math.max(Dos1.length(), Dos2.length()); Ins1=AddToLeft(Ins1,Inlength+1)+AddToRight(Dos1,Dolength); Ins2=AddToLeft(Ins2,Inlength+1)+AddToRight(Dos2,Dolength); String s=\;

for(int i=Inlength+Dolength,m,y=0;i>=0;i--){

m=Chartoint(Ins1.charAt(i))+Chartoint(Ins2.charAt(i))+y; s=Inttochar(m%n)+s;

a=Chartoint(this.MyNumber.charAt(i)); if(i!=0&&(a==-1||a==-2)){ }

if(a>=this.SystemNumber||a==-4){ }

if(a==-3){ }

if(k==-1){ }

k=i;

throw (new NumberFormatException(\错误!出现了第二个小数点 }else{

throw (new NumberFormatException(\错误!该字符不在该进制合法字符中 throw (new NumberFormatException(\错误!符号位只能在大数首部 throw (new NumberFormatException(\错误!大数不合法 \+this.MyNumber));

\+this.MyNumber.charAt(i)+\+i+\));

\+this.MyNumber.charAt(i)+\+i+\));

\+this.MyNumber.charAt(i)+\+i+\));

}

y=m/n;

int pointplace=s.length()-Dolength;

s=Format(s.substring(0,pointplace)+\+s.substring(pointplace),true);

return s; }

protected static String Sub_Positive(String s1,String s2,int n){//自定义两个正数减法运算;

if(ComparetoString(s1,s2)<0){//如果s1

return \+Sub_Positive(s2,s1,n);

} String

Ins1=getInteger(s1),Dos1=getDecimal(s1),Ins2=getInteger(s2),Dos2=getDecimal(s2);//分别获取整数部分及小数部分

int Inlength=Math.max(Ins1.length(),

Ins2.length()),Dolength=Math.max(Dos1.length(), Dos2.length()); String R=getComplement(new

BigNumber(AddToLeft(Ins2,Inlength)+\+AddToRight(Dos2,Dolength),n)).MyNumber;

R=Add_Positive(s1,R,n);

s2=\+AddToLeft(\,Dolength); R=Add_Positive(R,s2,n);

return Format(R.substring(1),true); }

protected static String Mult_Positive(String s1,String s2,int n){//自定义两个正数乘法运算; String

Ins1=getInteger(s1),Dos1=getDecimal(s1),Ins2=getInteger(s2),Dos2=getDecimal(s2);//分别获取整数部分及小数部分 Ins1+=Dos1; Ins2+=Dos2;

int[]A=new int[Ins1.length()+Ins2.length()-1]; for(int i=Ins1.length()-1;i>=0;i--){ }

String s=\;

for(int i=A.length-1;i>=1;i--){ }

s=Inttochar(A[i]%n)+s; A[i-1]+=A[i]/n;

for(int j=Ins2.length()-1;j>=0;j--){ }

A[i+j]+=Chartoint(Ins1.charAt(i))*Chartoint(Ins2.charAt(j));

联系客服:779662525#qq.com(#替换为@) 苏ICP备20003344号-4