使用加法方法乘以两个大整数并附加零

时间:2015-02-18 05:07:42

标签: java class append multiplication addition

所以我试图编写一个乘法方法,它使用一个加法方法(已经在类中预先写好)将两个大的正整数相乘。我现在最大的问题是弄清楚如何使用我的帮助方法将零添加到最后(将第一个数字从0-9添加一组次数(例如12 * 6将添加12次六次)。一旦我得到这个值我想把它加到12 * 2,它应该附加一个零(比如小学多次或多或少)。我画了与所讨论的方法有关的箭头。任何人都有任何建议或想法?

  12
x 26
----
  72
 240 <- how to append this zero?
----
 312

这是我的班级:

public class BigNum {

// F i e l d s

private String num;  // Representation of our number

// C o n s t r u c t o r s

/** Constructs a <tt>BigNum</tt> from a non-empty String of digits.
 *  @param num The string to be interpreted.
 *  @throws IllegalArgumentException if num is length zero or
 *      has any non-numeric digits
 *  @throws NullPointerException if num is null
 */
public BigNum(String num) {
    for (int i=0; i<num.length(); i++) {
        if ( ! Character.isDigit(num.charAt(i)) ) {
            throw new IllegalArgumentException();
        }
    }
    if ( num.length() == 0 ) {
        throw new IllegalArgumentException();
    }
    this.num = num;
}

/** Constructs a <tt>BigNum</tt> from a non-negative integer.
 *  @param num The non-negative integer to be interpreted.
 *  @throws IllegalArgumentException if num is negative
 */
public BigNum(int num) {
    // If num<0, redirected constructor will throw exception due to "-"
    this(""+num);
    if(num < 0) {
        throw new IllegalArgumentException();
    }
    //this(""+num);
}

/** Constructs a <tt>BigNum</tt> with value zero.
 */
public BigNum() {
    num="0";
}

// P u b l i c   M e t h o d s

/** Adds two <tt>BigNum</tt> objects' values together and returns a new
  * <tt>BigNum</tt> object with the resulting value.
  *
  * @param other this and other objects get added together
  * @return a new BigNum with the resulting value
  */
public BigNum add(BigNum other) {
    // Make shorter refer to the shorter num, longer to the longer num
    String shorter = other.num;
    String longer = this.num;
    if (this.num.length() < other.num.length()) {
        shorter = this.num;
        longer = other.num;
    }
    // Prepend zeros to make shorter as long as longer
    while (shorter.length() < longer.length()) {
        shorter = "0" + shorter;
    }
    // Add columns like we did in grade school
    int carry = 0;
    String result = "";
    for (int i=shorter.length()-1; i>=0; i--) {
        int temp = Character.getNumericValue(shorter.charAt(i)) +
                   Character.getNumericValue(longer.charAt(i)) + carry;
        result = (temp%10)+result;
        carry = temp/10;
    }
    // Handle carry-out, if there is one. Return result
    if (carry == 1) {
        result = "1"+result;
    }
    return new BigNum(result);
}

/**Multiplies two <tt>BigNum<tt> values together and returns a new
  *<tt>BigNum<tt> object with the resulting value.
  *
  *@param other
  *@returns a new BigNum with resulting value
  */
public BigNum mult(BigNum other) { //<---------------------Method in question
  BigNum result = new BigNum();
  String s = "";
  int current = 0;
  for(int i=0; i < this.num.toString().length();i++) {
     current += Character.getNumericValue(this.num.charAt(i));
     result = mult(current,other);
     s = result.toString();
   int count=0;
   for(int j=0; j < count; j++) {
        s += "0";
        count++;
   }
  }
   return new BigNum(s); 
}

/**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, BigNum other) { //<---------------Method in question
     BigNum result = new BigNum();
     for(int i=0;i < n;i++) {
        result=result.add(other);
     }
     return result;
}

/**Compares the other object with another value and only returns true if 
  *the value is less than the object the value.
  *
  *@param other
  *@return true or false
  */

public boolean less(BigNum other) {
    String shorter = other.num;
    String longer = this.num;
     if(other.toString().length() > this.toString().length()) {
        return true;
     }
     else if(other.toString().length() == this.toString().length()){
        for(int i=0; i < other.toString().length();i++) {
           if(Character.getNumericValue(shorter.charAt(i)) > Character.getNumericValue(longer.charAt(i))){
              return true;
           }
        }

     }
     return false;
}

/** Returns a string representation of the number. No leading zeros
  * will exist unless the overall value is zero.
  *
  * @return String representation of the BigNum
  */
public String toString() {
    return num;
}

/** Used only for unit testing. When run, should output only trues. */
public static void main(String[] args) {
    // Test constructors
    BigNum test = new BigNum("123");
    System.out.println(test.toString().equals("123"));
    test = new BigNum(123);
    System.out.println(test.toString().equals("123"));
    test = new BigNum();
    System.out.println(test.toString().equals("0"));
    // Test addition
    BigNum a = new BigNum();
    BigNum b = new BigNum();
    BigNum c = a.add(b);
    System.out.println(c.toString().equals("0"));
    a = new BigNum("999");
    b = new BigNum("101");
    c = a.add(b);
    System.out.println(c.toString().equals("1100"));
    a = new BigNum("237468273643278");
    b = new BigNum("87326487236437826");
    c = a.add(b);
    System.out.println(c.toString().equals("87563955510081104"));
    //Test multiplication
    BigNum j = new BigNum();
    BigNum k = new BigNum();
    BigNum l = j.mult(k);
    j = new BigNum("111");
    k = new BigNum("3");        
    l =j.mult(k);
    System.out.println(l.toString());
    System.out.println(j.less(k));

}

}

1 个答案:

答案 0 :(得分:0)

将第一个数字乘以第二个数字中的每个字符,然后将结果再次乘以10(或取决于其位置,即数十个位置为10,数百个位置为100,依此类推)以附加零。