2011-02-15 27 views
0

好吧,下面是我爲Java中的自己的BigInteger類所寫的完整代碼,我寧願使用已有的東西,但教授想要這樣做。編程幫助,讓它工作的方法

事情是我讓它製作BigInt,然後將數據存儲爲int [],符號爲1表示正數,-1表示負數。在那裏,我想在要求時加上,減去和乘以數字。如果它是一個典型的int類型,long類型等等,這將會很好,但是現在數字在數組中,所以我需要找到一種方法來處理這些方法。

我已經嘗試了很多方法,正如你所看到的,當兩個數字都是正數或負數時,像add這樣的東西都很好,但是混合時,我沒有得到它。我需要幫助的是不知道如何做每種類型,我知道方法,點陣乘法等,但我需要知道我將如何做這個東西,我完全失去了。

順便說一下,我知道我已經爲每種方法在這裏和那裏要求了類似的事情,對不起,我還沒有得到它,這是在幾天內,因爲我整個週末都絞盡腦汁。

package BigInteger; 


public class BigInt{ 

    private int[] store; 
    private int sign; 

    public BigInt(){ 
     store = new int[10]; 
     sign = 1; 
    } 

    public BigInt(int[] data){ 
     store = data; 
    } 

    public BigInt(int data, int maxSize){ 
     int size = String.valueOf(data).length(); 
     if (maxSize == 0){ 
      store = new int[size]; 
     } 

     else if(size >= maxSize){ 
       store = new int[size]; 
      } 
     else{ 
       store = new int[maxSize]; 
      } 
     if (data > 0){ 
      sign = 1; 
     }else{ 
      sign = -1; 
     } 
     for(int i = 0; i < size; i++){ 
      store[i] = data % 10; 
      data = data/10; 
     } 
    } 

    public BigInt(String num){ 
     store = new int[num.length()]; 
     try{ 
      for(int i = 0; i < num.length();i++){ 
       store[i] = Integer.parseInt(num.substring(i,i+1)); 
      } 
     }catch(IndexOutOfBoundsException e){ 
       System.out.println("Index out of bounds"); 
      } 
    } 

    public BigInt add(BigInt val){ 
     int[] bigger; 
     int[] smaller; 
     int[] dStore; 

     //Compare sizes and set which is bigger in first and create a new int[] 
     if(val.getSize() >= this.getSize()){ 
      bigger = val.getData(); 
      smaller = this.getData(); 

      dStore = new int[val.getSize()+1]; 
     }else{ 

      bigger = this.getData(); 
      smaller = val.getData(); 

      dStore = new int[this.getSize()+1]; 
     } 


     //Loop through till the end of small and add cells 
     for(int i = 0;i<smaller.length;i++){ 
      if((this.getSign() == 1 && val.getSign() == 1) || (this.getSign() == -1 && val.getSign() == -1)){ 
       dStore[i] = Math.abs(bigger[i] + smaller[i]); 
      }else if((this.getSign() == -1 || val.getSign() == -1) && (this.getSign() == 1 || val.getSign() == 1)){ 
       if(this.getSign() < 0 && this.getSize() < val.getSize()){ 
        smaller = this.getData(); 
        bigger = val.getData(); 
        bigger[i] = bigger[i] + 10; 
       }else if(val.getSign() < 0 && val.getSize() < this.getSize()){ 
        smaller = val.getData(); 
        bigger = this.getData(); 
        bigger[i] = bigger[i] + 10; 
       } 
        dStore[i] = bigger[i] + smaller[i]; 
       } 
      } 

     for(int i=0;i<dStore.length;i++){ 
      if(dStore[i] >= 10){ 
       dStore[i] = dStore[i] % 10; 
       dStore[i+1] = dStore[i+1] + 1; 
      } 
     } 


     //Finish adding numbers after small is done 
     for(int i=smaller.length;i<bigger.length;i++){ 
      dStore[i] = Math.abs(bigger[i] + dStore[i]); 
     } 

     //Create new BigInt from int[] 
     BigInt rVal = new BigInt(dStore); 

     //Set sign of new BigInt 
     if(this.getSign() == 1 && val.getSign() == 1){ 
      rVal.setSign(1); 
     }else if(this.getSign() == -1 && val.getSign() == -1){ 
      rVal.setSign(-1); 
     }else if((this.getSign() == 1 && val.getSign() == -1) || (this.getSign() == -1 && val.getSign() == 1)){ 
      if(this.getSize() > val.getSize()){ 
       rVal.setSign(1); 
      }else{ 
       rVal.setSign(-1); 
      } 
     } 

     return rVal; 
    } 

    public BigInt subtract(BigInt val){ 
     int[] bigger; 
     int[] smaller; 
     int[] dStore; 
     int carryOver = 0; 

     //Compare sizes and set which is bigger in first and create a new int[] 
     if(val.getSize() >= this.getSize()){ 
      bigger = val.getData(); 
      smaller = this.getData(); 

      dStore = new int[val.getSize()+1]; 
     }else{ 

      bigger = this.getData(); 
      smaller = val.getData(); 

      dStore = new int[this.getSize()+1]; 
     } 

     //Loop through till the end of small and add cells 
     for(int i = 0; i < smaller.length;i++){ 
      dStore[i] = Math.abs(bigger[i] - smaller[i]); 
     } 

     for(int i=0;i<dStore.length;i++){ 
      if(dStore[i] >= 10){ 
       dStore[i] = dStore[i] % 10; 
       dStore[i+1] = dStore[i+1] + 1; 
      } 
     } 


     //Finish adding numbers after small is done 
     for(int i=smaller.length;i<bigger.length;i++){ 
      dStore[i] = Math.abs(bigger[i] + dStore[i]); 
     } 

     //Create new BigInt from int[] 
     BigInt rVal = new BigInt(dStore); 

     //Set sign of new BigInt 
     if(this.getSign() == 1 && val.getSign() == 1){ 
      rVal.setSign(1); 
     }else if(this.getSign() == -1 && val.getSign() == -1){ 
      rVal.setSign(-1); 
     }else if((this.getSign() == 1 && val.getSign() == -1) || (this.getSign() == -1 && val.getSign() == 1)){ 
      if(this.getSize() > val.getSize()){ 
       rVal.setSign(1); 
      }else{ 
       rVal.setSign(-1); 
      } 
     } 

     return rVal; 
    } 

    public int multiply(BigInt val){ 
     int[] bigger; 
     int[] smaller; 
     int[] dStore; 

     int[][] tempResult; 



     //Checks to see which is bigger and then adds that to bigger 
     if(val.getSize() >= this.getSize()){ 
      bigger = val.getData(); 
      smaller = this.getData(); 
      dStore = new int[val.getSize()+this.getSize()]; 
     }else{ 
      bigger = this.getData(); 
      smaller = val.getData(); 
      dStore = new int[val.getSize()+this.getSize()]; 
     } 

     tempResult = new int[smaller.length][bigger.length]; 
     String resultString = ""; 
     String[] tempStr = new String[smaller.length]; 
     int[] intResults = new int[smaller.length*bigger.length]; 
     int loop = 0; 

     //Makes one long string of the numbers 
     for(int i=smaller.length-1;i >= 0;i--){ 
      for(int j = bigger.length-1;j >= 0;j--){ 
       tempResult[i][j] = smaller[i] * bigger[j]; 
       resultString = new StringBuffer(resultString).insert(resultString.length(), tempResult[i][j]).toString(); 
      } 
     } 

     //Splits the string up into loop amount of strings smaller.length size 
     for(int i=0;i < resultString.length();i = i + smaller.length){ 
      tempStr[loop] = (resultString.substring(i - loop, (i + smaller.length))); 
      //System.out.println(tempStr[loop]); 
      loop++; 
     } 

     //Adds 0's to make a full matrix 
     for(int i = 0; i < loop;i++){ 
      while(tempStr[i].length() < tempStr[loop-1].length()){ 
       tempStr[i] = new StringBuffer(tempStr[i]).insert(tempStr[i].length(), "0").toString(); 
      } 
     } 

     int[] tempNum = new int[smaller.length]; 
     int[] finalNum = new int[bigger.length]; 

     for(int i=0;i<smaller.length;i++){ 
      tempNum[i] = tempNum[i] + (Integer.parseInt((tempStr[i].substring(0,tempStr[i].length()))) % 10); 
     } 
     for(int i =0; i < smaller.length;i++){ 
      finalNum[0] =+ tempNum[i]; 
     } 
     System.out.println(tempNum[1]); 



     //Makes a new string that has all the digits in equal length. 
     resultString = ""; 
     for(int i=0; i < smaller.length;i++){ 
      resultString = new StringBuffer(resultString).insert(resultString.length(), tempStr[i]).toString(); 
     } 

     for(int i = 0; i<resultString.length();i++){ 
      for(int j = 0; j < 1;j++){ 
       tempNum[j] = tempNum[j] + Integer.parseInt(resultString.substring(i,i+1)); 
       //System.out.println(tempNum[j]); 
      } 
     } 


     //System.out.println(resultString); 


     return 0; 
    } 


    public void reverse(){ 
     for (int left=0, right=this.store.length-1; left<right; left++, right--) { 
      int temp = store[left]; store[left] = store[right]; store[right] = temp; 
     } 
    } 

    public int getSize(){ 
     int size = this.store.length - 1; 
     return size; 
    } 

    public int[] getData(){ 
     return store; 
    } 

    public void displayData(){ 
     for(int i=0;i<this.store.length;i++){ 
      System.out.println(this.store[i]); 
     } 
     System.out.println("Sign: " + this.sign); 
    } 

    public int getSign(){ 
     return this.sign; 
    } 

    public void setSign(int tempSign){ 
     this.sign = tempSign; 
    } 

    public boolean isPositive(){ 
     return this.sign == 1; 
    } 

    public boolean isGreater(){ 
     return this. 
    } 
} 
+1

你的問題遠遠超出了設計的目標。我建議你找到適合你的課程的助教,並花一些時間與他們一起交流。您需要比SO可提供的更多幫助。 – 2011-02-15 04:46:23

+0

不錯,想過但我們沒有助教,更不用說很少有時間從教授那裏得到幫助,至少在我的情況下,我有充實的課餘時間和工作。 – Tempus35 2011-02-15 04:50:15

回答

1

你需要考慮你是如何在小學的時候像你一樣重複,增加,減少和分裂的。也就是說,你需要考慮如何用鉛筆和一張紙手工完成這些操作。然後,您需要編寫代碼以相同的方式執行操作。最重要的部分是,以相同的方式執行這些操作時,您必須在您的BigInteger版本提供的陣列的每個插槽中存儲一位數字。

在開始編寫代碼之前,您應該清楚如何解決問題,然後編寫代碼以與您的解決方案相匹配。

即使您提供了最終解決方案,賠率非常好,您不會理解它,因爲您迄今提供的代碼未能實現比常規整數大的BigInteger。

吉姆·加里森建議與電話諮詢部門交談,我會推薦這個建議。這不是選擇正確方法的問題。你或者誤解了教授問你的任務,或者你誤解了一個人如何能夠表示一個大於Integer.MAX_INT的數字。對於正在學習如何編程的人來說,誤解是可以接受的,但從長遠來看,要求別人提供答案會對你造成嚴重傷害。

如果你不要求別人爲你做出「硬」的部分,你會得到你的文憑,但第一個問你一個嚴格的面試問題的人會知道你只有一張紙說話「文憑」,而不是電腦的理解。