java数组实现大数相加

Python017

java数组实现大数相加,第1张

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)

}

将大整数存入字符数组,按位相加。 给你写一段伪代码。

String a = "12389839843958394"

String b = "23445655234343"

char ac [] = a.toCharArray()

char bc [] = b.toCharArray()

这里要将数组ac 和bc 倒序排列,因为"123"转换后为{'1','2','3'} 高位在前,倒序是为了低位在前。这部分代码自己实现把。

char longc[]

char shortc[]

if (ac.length>=bc.length) {

longc=ac

shortc=bc

} else {

longc=bc

shortc=ac

}

下面做一个for循环,按位相加乘以10的i次方。就像小学学的列竖式子一样

int sum=0

for (int i=longc.lengthi<longc.lengthi++) {

if (i<shortc.length) {

sum+=(longc[i]+shortc[i]-96)*Math.pow(10, i)

} else {

sum+=(longc[i]-48)*Math.pow(10, i)

}

}

其中字符相加的时候减48是将char 转换成int

public

static void main(String[] args) {

// TODO Auto-generated method stub

List<Integer>bigNumber =

new LinkedList<Integer>()

for(int i = 0i <30i++) {

bigNumber.add(7)

}

List<Integer>bigNumber1 =

new LinkedList<Integer>()

for(int i = 0i <30i++) {

bigNumber1.add(6)

}

System.

out.println("bigNumber:"+bigNumber)

System.

out.println("bigNumber1:"+bigNumber1)

List<Integer>m = addMethod(bigNumber, bigNumber1)

for(int i = m.size()-1i >=0i--){//因为是顺着放进去的,所以应该倒着取出来。

System.

out.print(m.get(i))

}

}

public static List<Integer>addMethod(List<Integer>a, List<Integer>b) {

List<Integer>sum =

new LinkedList<Integer>()

int ii = 0 //存储十位上的数

for(int i = 0i <30i++) {

int temp

if(ii >0){

temp = a.get(i) + b.get(i) + ii

//当十位上有数字是就应该加上

ii = 0

}

else{

temp = a.get(i) + b.get(i)

}

if(temp >= 10) {

int u = temp%10

int y = temp/10

sum.add(u)

ii = y

if(i == 29){ //当加到最后一位时,应该把十位也放进结果里

sum.add(ii)

}

}

else {

sum.add(temp)

}

}

return sum

}