我如何添加表示为字符串的非常大的数字,即50位数字

时间:2014-02-10 16:25:23

标签: java

在这个程序中,我已经使用BigInterger创建了一个版本,但我仍然坚持制作只使用字符串的版本。我将如何添加两个非常大的字符串,如50个数字,只使用字符串。谢谢

public class BigStringNumber {

    int []f;
    String J;
public BigStringNumber(String H) {
    this.J=H;

    for(int i=0; i<H.length(); i++){
        f=new int[i];
    }
    // TODO Auto-generated constructor stub
}   

public String toString(){ 

    return ""+J;
}

public BigStringNumber add(BigStringNumber d){


    return new BigStringNumber();
}

public static void main(String[]args){
    BigStringNumber A=new BigStringNumber("46376937677490009712648124896970078050417018260538");
    BigStringNumber a=new BigStringNumber("46376937677490009712648124896970078050417018260538");
    System.out.print(a.toString());

}

3 个答案:

答案 0 :(得分:2)

自己编写add方法并不困难 这个add做的是一年级,二年级,三年级的学生 实际上,成年人长期做同样的事情。

public class BigStringNumber {

    private int[] digit;
    // private String str;

    public BigStringNumber(String str) {
        // this.str = str;
        this.digit = new int[str.length()];
        for (int i = 0; i < str.length(); i++) {
            digit[i] = str.charAt(i) - '0';
        }
    }

    public String toString() {
        StringBuilder sb = new StringBuilder();
        for (int i=0; i<digit.length; i++){
            sb.append((char) ('0' + digit[i]));
        }
        return sb.toString();
    }

    public BigStringNumber add(BigStringNumber d) {

        StringBuilder sb = new StringBuilder();
        int i1 = this.digit.length - 1;
        int i2 = d.digit.length - 1;
        int dgt = 0;
        int carry = 0;
        while (i1 >= 0 && i2 >= 0) {
            dgt = this.digit[i1] + d.digit[i2] + carry;
            carry = dgt / 10;
            dgt = dgt % 10;
            i1--;
            i2--;
            sb.append((char) ('0' + dgt));
        }
        while (i1 >= 0) {
            dgt = this.digit[i1] + carry;
            carry = dgt / 10;
            dgt = dgt % 10;
            i1--;
            sb.append((char) ('0' + dgt));
        }
        while (i2 >= 0) {
            dgt = d.digit[i2] + carry;
            carry = dgt / 10;
            dgt = dgt % 10;
            i2--;
            sb.append((char) ('0' + dgt));
        }
        sb = sb.reverse();
        return new BigStringNumber(sb.toString());
    }

    public static void main(String[] args) {
        BigStringNumber a = new BigStringNumber("2020202029");
        BigStringNumber b = new BigStringNumber("4040404046");
        System.out.println(a.add(b).toString());
    }

}

答案 1 :(得分:0)

以下代码执行此操作:

public class BigStringNumber {

    private int[] digit;
    // private String str;

    public BigStringNumber(String str) {
        // this.str = str;
        this.digit = new int[str.length()];
        for (int i = 0; i < str.length(); i++) {
            digit[i] = str.charAt(i) - '0';
        }
    }

    public String toString() {
        StringBuilder sb = new StringBuilder();
        for (int i=0; i<digit.length; i++){
            sb.append((char) ('0' + digit[i]));
        }
        return sb.toString();
    }

    public BigStringNumber add(BigStringNumber d) {

        StringBuilder sb = new StringBuilder();
        int i1 = this.digit.length - 1;//contains first number
        int i2 = d.digit.length - 1;//second number
        int carry = 0;//variable
        int dgt = 0;//variable
        while (i1 >= 0 && i2 >= 0) {
            carry = this.digit[i1] + d.digit[i2] + dgt;//adds numbers together
            dgt = carry / 10;
            carry = carry % 10;
            i1--;
            i2--;
            sb.append((char) ('0' + carry));
        }
        while (i1 >= 0) {
            carry = this.digit[i1] + dgt;
            dgt = carry / 10;
            carry = carry % 10;
            i1--;
            sb.append((char) ('0' + carry));
        }
        while (i2 >= 0) {
            carry = d.digit[i2] + dgt;
            dgt = carry / 10;
            carry = carry % 10;
            i2--;
            sb.append((char) ('0' + carry));
        }
        sb = sb.reverse();
        return new BigStringNumber(sb.toString());
    }

    public static void main(String[] args) {
        BigStringNumber a = new BigStringNumber("2020202029");
        BigStringNumber b = new BigStringNumber("4040404046");
        System.out.println(a.add(b).toString());
    }

}

答案 2 :(得分:0)

/**
 * Created by SidMore on 5/6/2016.
 */
public class AddLargeString {

    private String s1;
    private String s2;

    public AddLargeString(String s1, String s2){
        this.s1 = new StringBuilder(s1).reverse().toString();
        this.s2 = new StringBuilder(s2).reverse().toString();
    }

    public String add() {

        int[] d = new int[s1.length() + 1];

        if (s1.length() > s2.length()) {

            for (int i = 0; i < s1.length(); i++) {
                d[i] = s1.charAt(i) - '0';
            }

            for (int j = 0; j < s2.length(); j++) {
                d[j] = d[j] + s2.charAt(j) - '0';
            }
        } else {

            for (int i = 0; i < s2.length(); i++) {
                d[i] = s1.charAt(i) - '0';
            }

            for (int j = 0; j < s1.length(); j++) {
                d[j] = d[j] + s1.charAt(j) - '0';
            }
        }

        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < d.length; i++) {
            int mod = d[i] % 10;
            int carry = d[i] / 10;
            if (i + 1 < d.length)
                d[i + 1] += carry;
            sb.insert(0, mod);
        }

        //remove front 0's
        while (sb.charAt(0) == '0' && sb.length() > 1) {
            sb.deleteCharAt(0);
        }
        return sb.toString();
    }

   public static void main(String[] args){
       AddLargeString aLS = new AddLargeString("3560","9");
       System.out.println("Addition: " + aLS.add());
   }
}