有人可以帮我弄清楚为什么我的零没有附加?

时间:2015-02-24 04:29:02

标签: java append add multiplication bignum

所以我一直在研究这个BigNum乘法方法(简而言之,该方法接受了一个BigNum其他方法并且应该返回两个大正整数的乘积而不使用bigint类)一段时间我和#&#但是,差不多完成了,我仍然有附加零的问题。我的助手方法似乎也没有正确添加(因为例如444 * 4应返回为" 1776"然而它返回为" 161616"。)我需要有人来调试这个并帮我弄清楚它为什么不起作用。任何帮助表示赞赏。

这是我尝试以444 * 444为例时得到的结果

预期输出应为:

1776
17760
177600
197136
用我的代码

实际输出:

161616
1616160
1616160
3393936

我的方法

 /**Multiplies two <tt>BigNum<tt> values together and returns a new
  *<tt>BigNum<tt> object with the resulting value.
  *
  *@param other object
  *@returns a new BigNum with resulting value
  */
public BigNum mult(BigNum other) {
  BigNum tmp = new BigNum();
  BigNum acc = new BigNum();
  String s="";
  int count=0;
  for(int i= 0; i < other.num.length() ; i++) { //each digit x of other
     tmp = this.mult(Character.getNumericValue(other.num.charAt(i)));
     if(i > 0) {
        for(int j=0; j < i; j++) {
           s = tmp.num + "0";
        }
     }else {
     s = tmp.num;
     }
     tmp=new BigNum(s);
     count++;
     acc = acc.add(tmp);
 }
 return acc;
}

/**Helper method that adds the other value a set of number of times, 0-9
  *
  *@param and int n and other object
  *@returns resulting value
  */
public BigNum mult(int n) {
     String result;
     int carry;
     if(n==0){
        result="0";
     }
     else{
        carry =0;
        result = "";
     }
     for(int i=this.num.length()-1; i >=0; i--){
        int temp = n * Character.getNumericValue(this.num.charAt(i))
        result=(temp%10) + result;
        carry = temp/10;
        if(carry > 0){
           result = carry + result;
        }
     }
     return new BigNum(result);
}

2 个答案:

答案 0 :(得分:1)

使用String实现BIGNUM乘法相当慢,但是它可以工作。 按如下方式更改您的代码:

public BigNum mult(BigNum other) {
  BigNum tmp = new BigNum();
  BigNum acc = new BigNum();
  String s="";
  int count=0;
  for(int i= 0; i < other.num.length() ; i++) { //each digit x of other
     tmp = this.mult(Character.getNumericValue(other.num.charAt(i)));
     if(i > 0) {
         s = tmp.num;
        for(int j=i; j > 0 ; j--) {
           s = s + "0";
        }
     }else {
     s = tmp.num;
     }
     tmp=new BigNum(s);
     count++;
     acc = acc.add(tmp);
 }
 return acc;
}

public BigNum mult(int n) {
     String result;
     int carry;
     if(n==0){
        result="0";
     }
     else{
        carry =0;
        result = "";
     }
     for(int i=this.num.length()-1; i >=0; i--){
        int temp = n * Character.getNumericValue(this.num.charAt(i));
        // add carry first
        carry = temp/10;
        if(carry > 0){
            int lastnum=(result.length()==0)?0:
                Character.getNumericValue(result.charAt(result.length()-1));

            lastnum=lastnum+carry;
            result = (result.length()==0)?"":result.substring(0, result.length()-1); // remove the last num
            result = result + lastnum;
        } 
        result= result + (temp%10);
     }
     return new BigNum(result);
}

下次还应粘贴add()方法。这是我对add()的实现,如果有人感兴趣的话(非常难看,我不得不说):

private BigNum add(BigNum other) {
    StringBuilder sb = new StringBuilder();
    int sum, carry=0;
    String large, small;
    if(this.num.length()>=other.num.length()) {
        large=this.num; small=other.num;
    } else {
        large=other.num; small = this.num;
    }
    int len = Math.min(this.num.length(), other.num.length());
    for(int i=len-1; i>=0; i--) {
         sum = Character.getNumericValue(large.charAt(large.length()-len+i)) +
               Character.getNumericValue(small.charAt(i)) +
               carry;
         carry=(sum>=10)?1:0;
         sb.insert(0, String.valueOf(sum).charAt((sum>=10)?1:0));
    }
    if(large.length()==small.length()) {
        if(carry==1) sb.insert(0, 1);
    } else {
        sum = Character.getNumericValue(large.charAt(large.length()-(len+1)))+carry;
        sb.insert(0, String.valueOf(sum).charAt(0));
    }
    for(int i=large.length()-(len+2); i>=0; i--) {
        sb.insert(0, large.charAt(i));
    }
    num = sb.toString();
    return this;
}

所有这些方法都进入了这个BigNum类:

public class BigNum {
String num;

public BigNum() {
    num=new String();
}

public BigNum(String s) {
    this.num=s;
}

... methods here...
}

答案 1 :(得分:-1)

使用以下逻辑:5 * 2 = 5 + 5 + 5 + 5 + 5

public class BigNum {

    int value;

    public BigNum(int value) {
        this.value = value;
    }

    public BigNum mult(BigNum other) {
        int result = 0;
        for (int i = 0; i < value; i++) {
            result += other.getValue();
        };
        return new BigNum(result); 
    }

    public int getValue() {
        return value;
    }

    @Override
    public String toString() {
        return "BigNum [value=" + value + "]";
    }

    public static void main(String[] args) {
        System.out.println(new BigNum(444).mult(new BigNum(444)));
    }

}