Java 使用加法将两个大整数相乘并附加零
因此,我试图编写一个乘法方法,使用加法(已经在类中预先编写)将两个大的正整数相乘。我现在最大的问题是如何使用我的helper方法(它将第一个数字加上一组从0到9的次数(例如12*6将加12-6次)。一旦我得到这个值,我想将它加到12*2,它应该加上一个零(类似于小学乘法).我画了一些有关方法的箭头。有人有什么建议或想法吗Java 使用加法将两个大整数相乘并附加零,java,class,append,multiplication,addition,Java,Class,Append,Multiplication,Addition,因此,我试图编写一个乘法方法,使用加法(已经在类中预先编写)将两个大的正整数相乘。我现在最大的问题是如何使用我的helper方法(它将第一个数字加上一组从0到9的次数(例如12*6将加12-6次)。一旦我得到这个值,我想将它加到12*2,它应该加上一个零(类似于小学乘法).我画了一些有关方法的箭头。有人有什么建议或想法吗 12 x 26 ---- 72 240 <- how to append this zero? ---- 312 12 x26 ---- 72 240=0;
12
x 26
----
72
240 <- how to append this zero?
----
312
12
x26
----
72
240=0;i--){
int temp=Character.getNumericValue(较短的.charAt(i))+
Character.getNumericValue(较长的.charAt(i))+进位;
结果=(温度%10)+结果;
进位=温度/10;
}
//如果有,请搬运。返回结果
如果(进位==1){
结果=“1”+结果;
}
返回新的BigNum(结果);
}
/**将两个BigNum值相乘并返回一个新值
*具有结果值的BigNum对象。
*
*@参数其他
*@返回带有结果值的新BigNum
*/
public BigNum mult(BigNum other){//Character.getNumericValue(longer.charAt(i))){
返回true;
}
}
}
返回false;
}
/**返回数字的字符串表示形式。没有前导零
*将存在,除非总值为零。
*
*@返回BigNum的字符串表示形式
*/
公共字符串toString(){
返回num;
}
/**仅用于单元测试。运行时,应仅输出True*/
公共静态void main(字符串[]args){
//测试构造函数
BigNum测试=新的BigNum(“123”);
System.out.println(test.toString()等于(“123”);
test=新的BigNum(123);
System.out.println(test.toString()等于(“123”);
test=新的BigNum();
System.out.println(test.toString()等于(“0”);
//测试添加
BigNum a=新的BigNum();
BigNum b=新的BigNum();
BigNum c=a.add(b);
System.out.println(c.toString()等于(“0”);
a=新的BigNum(“999”);
b=新的BigNum(“101”);
c=a。添加(b);
System.out.println(c.toString()等于(“1100”);
a=新的BigNum(“237468273643278”);
b=新的BigNum(“87326487236437826”);
c=a。添加(b);
System.out.println(c.toString()等于(“87563955510081104”);
//测试乘法
BigNum j=新的BigNum();
BigNum k=新的BigNum();
BigNum l=j.mult(k);
j=新的BigNum(“111”);
k=新的BigNum(“3”);
l=j.mult(k);
System.out.println(l.toString());
系统输出println(j.less(k));
}
}将第一个数字与第二个数字中的每个字符相乘,然后将结果再次与10相乘(或根据其位置,即10表示十位,100表示百位,依此类推),以追加零 整个代码是否与您的问题相关?在一个数字的右边加上0的一般解决方案是将它乘以10…可能不会,但不会有什么坏处。至于如何在第二次乘法/迭代后精确地将其追加,我曾考虑使用for循环,但我不知道从何处开始或放置它。
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));
}