好吧,下面是我爲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.
}
}
你的問題遠遠超出了設計的目標。我建議你找到適合你的課程的助教,並花一些時間與他們一起交流。您需要比SO可提供的更多幫助。 – 2011-02-15 04:46:23
不錯,想過但我們沒有助教,更不用說很少有時間從教授那裏得到幫助,至少在我的情況下,我有充實的課餘時間和工作。 – Tempus35 2011-02-15 04:50:15