簡體   English   中英

通過在Java中使用String創建我自己的BigInteger

[英]Create my own BigInteger by using String in Java

我必須創建一個類MyBigInteger來計算操作:具有非常大的整數(約60位,十進制或更多)的mod逆和mod power。 為了解決這個問題,我使用String來存儲我的數字並創建一些基本函數,例如加,減,mod,div等。但是我得到的問題是:雖然我的加和減方法工作正常,但是多個功能僅適用於>小數字,並且如果我使用具有7、8或更大數字的輸入,則我的程序將不會響應。 我>我想用String存儲大數字的想法可能不是一個好主意,如果我使用數組存儲大數字,>我的課程會更快地工作嗎? 下面是我的代碼。add和減去方法似乎正常工作,所以我只會發布方法> multiple。 首先是MyBigInteger乘以整數的方法。 我用它來創建兩個> MyBigInteger之間的倍數:

public class MyBigInteger {
private String val;
    public static final MyBigInteger ZERO = new MyBigInteger("0");
        ...
    private MyBigInteger mutiple( int k){
    MyBigInteger result = ZERO; 
    if( k == 0) return result;
    for( int i = 1; i <= Math.abs(k); i++) result = result.add(this);
    if( k > 0) return result;
    else return result.getOpposite(); // result.add(result.getOpposite()) == ZERO
}


    public MyBigInteger mutiple( MyBigInteger mbi){
    MyBigInteger result = ZERO;
    if( mbi.toString().charAt(0) != '-'){
        for( int i = mbi.toString().length() - 1; i >= 0; i--){
        result =  result.add(this.mutiple(Integer.parseInt(mbi.toString().charAt(mbi.toString().length() - i -1) + "")).mutiple((int)Math.pow(10, i)));
        }
    } else{
        for( int i = mbi.toString().length() - 1 ; i >= 1; i--){
            result = result.add(this.mutiple(Integer.parseInt(mbi.toString().charAt(mbi.toString().length() - i) + "")).mutiple((int)Math.pow(10, i-1)));
        }
        result = result.getOpposite();
    }
    return result;
}

非常感謝您可能提供的幫助

抱歉,但是乘法方法是固定的,可以完美地工作。 但這不是我班上唯一的問題。 我通過使用減法創建了一個mod方法。 在減法中,我使用subAbs方法,這是對兩個正數MyBigNumber的特殊減法。

public MyBigInteger subAbs( MyBigInteger mBI){      
String result = "";
int i = this.getLength();
int j = mBI.getLength();
int s = 0;
int r = 0;
String temp = "";       
String val1 = this.toString();
String val2 = mBI.toString();
if( this.equalsTo(mBI) == true ) return ZERO;
else
if( this.greaterThan(mBI) == true){         
    for( int k = 0; k < i - j; k++) temp += "0";
    val2 = temp + val2;
    for( int k = i-1; k > 0; k-- ){
     //And the statement right behind this comment is the wrong line (224) in the image
        s = 10 + Integer.parseInt(val1.charAt(k) + "") - Integer.parseInt(val2.charAt(k) + "") - r;             
        if( s >= 10){
            s = s - 10;
            r = 0; 
        } else  r = 1;
        result = Integer.valueOf(s).toString() + result;
    }
    s = Integer.parseInt(val1.charAt(0) + "") - Integer.parseInt(val2.charAt(0)+"") - r;
    if( s >= 0 ) result = s + result;               
    else result = Integer.valueOf(s).toString() + result;   
    return new MyBigInteger(result);
} else return new MyBigInteger("-" + mBI.subAbs(this).toString());

}

如果我輸入大量數字,則會出現異常: 在此處輸入圖片說明

問題可能始於方法subAbs。

您的乘法方法就是一遍又一遍地相加。 這對於較小的數字效果很好,但是當您輸入大量數字時,您需要進行大量的計算,因此計算機必須花費很長時間才能弄清楚。

您將如何手動將100x12345相乘? 您要先添加12345 + 12345,然后再加上12345,然后再加上12345,再重復100次嗎? 那就是您現在的主旨。 您應該嘗試以與100x12345相乘的相同方式來實現乘法算法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM