「大数求和java」大数求和python

博主:adminadmin 2022-12-27 10:39:07 61

今天给各位分享大数求和java的知识,其中也会对大数求和python进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

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编程中“两个大整数求和”怎么编写

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

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.length;ilongc.length;i++) {

if (ishortc.length) {

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

} else {

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

}

}

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

java怎么处理大数相加

package eshop.framework.util;

import java.math.BigDecimal;

public class AmountUtil {

// 默认除法运算精度

private static final int DEFAULT_DIV_SCALE = 2;

/**

* 提供精确的加法运算。

*

* @param v1

* @param v2

* @return 两个参数的和

*/

public static double add(double v1, double v2) {

BigDecimal b1 = new BigDecimal(Double.toString(v1));

BigDecimal b2 = new BigDecimal(Double.toString(v2));

return b1.add(b2).doubleValue();

}

/**

* 提供精确的加法运算

*

* @param v1

* @param v2

* @return 两个参数数学加和,以字符串格式返回

*/

public static String add(String v1, String v2) {

BigDecimal b1 = new BigDecimal(v1);

BigDecimal b2 = new BigDecimal(v2);

return b1.add(b2).toString();

}

/**

* 提供精确的减法运算。

*

* @param v1

* @param v2

* @return 两个参数的差

*/

public static double subtract(double v1, double v2) {

BigDecimal b1 = new BigDecimal(Double.toString(v1));

BigDecimal b2 = new BigDecimal(Double.toString(v2));

return b1.subtract(b2).doubleValue();

}

/**

* 提供精确的减法运算

*

* @param v1

* @param v2

* @return 两个参数数学差,以字符串格式返回

*/

public static String subtract(String v1, String v2) {

BigDecimal b1 = new BigDecimal(v1);

BigDecimal b2 = new BigDecimal(v2);

return b1.subtract(b2).toString();

}

/**

* 提供精确的乘法运算。

*

* @param v1

* @param v2

* @return 两个参数的积

*/

public static double multiply(double v1, double v2) {

BigDecimal b1 = new BigDecimal(Double.toString(v1));

BigDecimal b2 = new BigDecimal(Double.toString(v2));

return b1.multiply(b2).doubleValue();

}

/**

* 提供精确的乘法运算

*

* @param v1

* @param v2

* @return 两个参数的数学积,以字符串格式返回

*/

public static String multiply(String v1, String v2) {

BigDecimal b1 = new BigDecimal(v1);

BigDecimal b2 = new BigDecimal(v2);

return b1.multiply(b2).toString();

}

/**

* 提供(相对)精确的除法运算,当发生除不尽的情况时,精确到 小数点以后2位,以后的数字四舍五入,舍入模式采用ROUND_HALF_EVEN

*

* @param v1

* @param v2

* @return 两个参数的商

*/

public static double divide(double v1, double v2) {

return divide(v1, v2, DEFAULT_DIV_SCALE);

}

/**

* 提供(相对)精确的除法运算。当发生除不尽的情况时,由scale参数指 定精度,以后的数字四舍五入。舍入模式采用ROUND_HALF_EVEN

*

* @param v1

* @param v2

* @param scale

* 表示需要精确到小数点以后几位。

* @return 两个参数的商

*/

public static double divide(double v1, double v2, int scale) {

return divide(v1, v2, scale, BigDecimal.ROUND_HALF_EVEN);

}

/**

* 提供(相对)精确的除法运算。当发生除不尽的情况时,由scale参数指 定精度,以后的数字四舍五入。舍入模式采用用户指定舍入模式

*

* @param v1

* @param v2

* @param scale

* 表示需要精确到小数点以后几位

* @param round_mode

* 表示用户指定的舍入模式

* @return 两个参数的商

*/

public static double divide(double v1, double v2, int scale, int round_mode) {

if (scale 0) {

throw new IllegalArgumentException(

"The scale must be a positive integer or zero");

}

BigDecimal b1 = new BigDecimal(Double.toString(v1));

BigDecimal b2 = new BigDecimal(Double.toString(v2));

return b1.divide(b2, scale, round_mode).doubleValue();

}

/**

* 提供(相对)精确的除法运算,当发生除不尽的情况时,精确到 小数点以后10位,以后的数字四舍五入,舍入模式采用ROUND_HALF_EVEN

*

* @param v1

* @param v2

* @return 两个参数的商,以字符串格式返回

*/

public static String divide(String v1, String v2) {

return divide(v1, v2, DEFAULT_DIV_SCALE);

}

/**

* 提供(相对)精确的除法运算。当发生除不尽的情况时,由scale参数指 定精度,以后的数字四舍五入。舍入模式采用ROUND_HALF_EVEN

*

* @param v1

* @param v2

* @param scale

* 表示需要精确到小数点以后几位

* @return 两个参数的商,以字符串格式返回

*/

public static String divide(String v1, String v2, int scale) {

return divide(v1, v2, DEFAULT_DIV_SCALE, BigDecimal.ROUND_HALF_EVEN);

}

/**

* 提供(相对)精确的除法运算。当发生除不尽的情况时,由scale参数指 定精度,以后的数字四舍五入。舍入模式采用用户指定舍入模式

*

* @param v1

* @param v2

* @param scale

* 表示需要精确到小数点以后几位

* @param round_mode

* 表示用户指定的舍入模式

* @return 两个参数的商,以字符串格式返回

*/

public static String divide(String v1, String v2, int scale, int round_mode) {

if (scale 0) {

throw new IllegalArgumentException(

"The scale must be a positive integer or zero");

}

BigDecimal b1 = new BigDecimal(v1);

BigDecimal b2 = new BigDecimal(v2);

return b1.divide(b2, scale, round_mode).toString();

}

/**

* 提供精确的小数位四舍五入处理,舍入模式采用ROUND_HALF_EVEN

*

* @param v

* 需要四舍五入的数字

* @param scale

* 小数点后保留几位

* @return 四舍五入后的结果

*/

public static double round(double v, int scale) {

return round(v, scale, BigDecimal.ROUND_HALF_EVEN);

}

/**

* 提供精确的小数位四舍五入处理

*

* @param v

* 需要四舍五入的数字

* @param scale

* 小数点后保留几位

* @param round_mode

* 指定的舍入模式

* @return 四舍五入后的结果

*/

public static double round(double v, int scale, int round_mode) {

if (scale 0) {

throw new IllegalArgumentException(

"The scale must be a positive integer or zero");

}

BigDecimal b = new BigDecimal(Double.toString(v));

return b.setScale(scale, round_mode).doubleValue();

}

/**

* 提供精确的小数位四舍五入处理,舍入模式采用ROUND_HALF_EVEN

*

* @param v

* 需要四舍五入的数字

* @param scale

* 小数点后保留几位

* @return 四舍五入后的结果,以字符串格式返回

*/

public static String round(String v, int scale) {

return round(v, scale, BigDecimal.ROUND_HALF_EVEN);

}

/**

* 提供精确的小数位四舍五入处理

*

* @param v

* 需要四舍五入的数字

* @param scale

* 小数点后保留几位

* @param round_mode

* 指定的舍入模式

* @return 四舍五入后的结果,以字符串格式返回

*/

public static String round(String v, int scale, int round_mode) {

if (scale 0) {

throw new IllegalArgumentException(

"The scale must be a positive integer or zero");

}

BigDecimal b = new BigDecimal(v);

return b.setScale(scale, round_mode).toString();

}

public static String doubleToString(double num, int scale, int round_mode) {

if (scale 0) {

throw new IllegalArgumentException(

"The scale must be a positive integer or zero");

}

BigDecimal b = new BigDecimal(num);

return b.setScale(scale, round_mode).toString();

}

}

关于大数求和java和大数求和python的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

The End

发布于:2022-12-27,除非注明,否则均为首码项目网原创文章,转载请注明出处。