189 8069 5689

java大数运算源代码 java超大数运算 算法

java程序代码求助关于HugeInteger(最大数)的四则运算,题目如下

错误是因为你的HugeInteger类里需要定义好多方法,但是你的HugeInteger类中都没有,我把你用到的这些方法的类型与作用说出来,你自己在HugeInteger类里面写。

霸州ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为成都创新互联公司的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:18980820575(备注:SSL证书合作)期待与您的合作!

1、void parse(String a) 把String a转换为HugeInteger

2、String toString() 返回HugeInteger的字符串表达形式

3、void add(HugeInteger other) 把other加到当前HugeInteger对象上

4、void substract(HugeInteger other) 用当前对象减去other

5、boolean isZero() 判断当前对象是否为0

6、boolean isNotEqualTo(HugeInteger other) 判断当前对象与other是否相等

7、boolean isGreaterThan(HugeInteger other) 判断当前对象是否比other大

8、boolean isLessThan(HugeInteger other) 判断当前对象是否比other小

9、boolean isGreaterThanOrEqualTo(HugeInteger other) 判断当前对象是否大于等于other

10、boolean isLessThanOrEqualTo(HugeInteger other) 判断当前对象是否小于等于other

运用JAVA中大数类实现大数的四则运算

import java.math.BigInteger;

public class BigIntegerGet {

public String getAdd(String Str1,String Str2){

String Str3=new String();

BigInteger BigInt1=new BigInteger(Str1);

BigInteger BigInt2=new BigInteger(Str2);

BigInt1=BigInt1.add(BigInt2);

Str3=BigInt1.toString();

return Str3;

}

public String getSubtract(String Str1,String Str2){

String Str3=new String();

BigInteger BigInt1=new BigInteger(Str1);

BigInteger BigInt2=new BigInteger(Str2);

BigInt1=BigInt1.subtract(BigInt2);

Str3=BigInt1.toString();

return Str3;

}

public String getMultiply(String Str1,String Str2){

String Str3=new String();

BigInteger BigInt1=new BigInteger(Str1);

BigInteger BigInt2=new BigInteger(Str2);

BigInt1=BigInt1.multiply(BigInt2);

Str3=BigInt1.toString();

return Str3;

}

public String getDivide(String Str1,String Str2){

String Str3=new String();

BigInteger BigInt1=new BigInteger(Str1);

BigInteger BigInt2=new BigInteger(Str2);

BigInt1=BigInt1.divide(BigInt2);

Str3=BigInt1.toString();

return Str3;

}

}

java数组实现大数相加

package com.nileader.big.entity;

public class BigInt {

private String data_str; //原始数据

private int digit; //数据位数

private int[] data; //大数

private boolean carry = false; //进位标识符

/**

* setter and getter

*/

public boolean isCarry() {

return carry;

}

public void setCarry(boolean carry) {

this.carry = carry;

}

public String getData_str() {

return data_str;

}

public void setData_str(String data_str) {

this.data_str = data_str;

}

public int[] getData() {

return data;

}

public void setData(int[] data) {

this.data = data;

}

public int getDigit() {

return digit;

}

public void setDigit(int digit) {

this.digit = digit;

}

//构造方法

public BigInt(){};

public BigInt(String str_data)

{

this.setData_str(str_data);

}

//基本操作

/**

* 形成大数 初始化

*/

public void initBInt()

{

this.setDigit( this.getData_str().length() );

this.data = new int[this.getDigit()];

//将字符组成的大数逆序放入int[] data中

for(int i = 0, j=this.getDigit() ; i

{

// 1104 -- data[0] = '4',data[1] = '0',data[2]=1, data[3]= '1'

this.data[i] = Integer.parseInt( this.getData_str().substring(j-1,j) );

}

}

/**

* 进行大数相加操作

*/

public void add( BigInt bint)

{

//this的位数大于bint的位数

if( this.getDigit() bint.getDigit() )

{

int[] datatemp = this.getData();

this.setData( bint.getData());

bint.setData( datatemp);

this.setDigit(this.getData().length);

bint.setDigit(bint.getData().length);

}

//将短的那个先加完

int i =0;

for(; i

{

int tdata = 0;

//上次运算有进位

if( this.isCarry())

{

tdata = this.getData()[i] + bint.getData()[i] +1;

//取消进位标识

this.setCarry(false);

}

else tdata = this.getData()[i] + bint.getData()[i] ;

//本次结果无进位

if(tdata 10) this.data[i] = tdata;

//本次结果有进位

else if(tdata =10)

{

this.data[i] = tdata -10;

this.setCarry(true);

}

} //短的那个加完了

//剩余数的处理

for(;i

{

//有个进位的

if(this.isCarry())

{

int tdata = this.data[i]+1;

if(tdata =10) this.data[i] = tdata -10;

else

{

this.data[i] = tdata;

this.setCarry(false);

}

}

}

//对最高位益处检测

if(this.data[this.getDigit()-1] == 0)

{

int[] tdata = new int[this.getDigit()+1];

System.arraycopy(this.getData(), 0, tdata, 0, this.getDigit());

tdata[this.getDigit()] = 1;

this.setData(tdata);

}

}

}

其中代码段

//对最高位益处检测

if(this.data[this.getDigit()-1] == 0)

{

int[] tdata = new int[this.getDigit()+1];

System.arraycopy(this.getData(), 0, tdata, 0, this.getDigit());

tdata[this.getDigit()] = 1;

this.setData(tdata);

}


新闻标题:java大数运算源代码 java超大数运算 算法
文章地址:http://jkwzsj.com/article/hgegii.html

其他资讯