重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
import java.math.BigInteger;
创新互联专注于企业网络营销推广、网站重做改版、宁晋网站定制设计、自适应品牌网站建设、html5、购物商城网站建设、集团公司官网建设、成都外贸网站制作、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为宁晋等各大城市提供网站开发制作服务。
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;
}
}
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);
}
一下代码仅供参考
package com.kidd.test.zhidao;
import java.util.NoSuchElementException;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = 0;
int b = 0;
boolean next = false;
while (!next) {
System.out.print("请输入两个整数(用空格分隔):");
try {
a = sc.nextInt();
b = sc.nextInt();
next = true;
} catch (NoSuchElementException e) {
System.out.println("输入有误,请重新输入.");
sc.nextLine();
continue;
}
}
System.out.printf("最大值为:%d\n", a b ? a : b);
}
}
//用BigInteger
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.util.Arrays;
public class MenuNumber {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(
System.in));
while(true){
System.out.println("请输入正整数进行相关运算,输入over结束");
String str= br.readLine();
if(str.toLowerCase().equals("over")){
System.out.println("=程序结束=");
break;
}
char[] c=str.toCharArray();//从小到大排序
Arrays.sort(c);//排序
String str_1="",str_2="";
for(int i=0;ic.length;i++){
str_1+=c[i];//从小到大排序的数字
str_2+=c[c.length-1-i];//从大到小排序的数字
}
try{
//将字符串格式化为数字
BigInteger l1=new BigInteger(str_1);
BigInteger l2=new BigInteger(str_2);
//输出
System.out.println("原 数-"+str+"\n降 序-"+l2+"\n升 序-"+l1+"\n之 差-"+l2.subtract(l1));
}catch(NumberFormatException ne){
System.out.println("请输入正整数,谢谢合作!");//输入的不是数字时的处理。
}
}
}
}
错误是因为你的HugeInteger类里需要定义好多方法,但是你的HugeInteger类中都没有,我把你用到的这些方法的类型与作用说出来,你自己在HugeInteger类里面写。
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