开发者

Programming Help, methods to make it work

Ok below is the full code I have written for what is suppose to be my own BigInteger class in java, I rather use what is there already but the professor wants this.

Thing is I have it making a BigInt and then store the data as an int[] and the sign as 1 for positive and -1 for negative. From there I am suppose to add, subtract, and multiply the numbers when asked. This would be fine if it was a typical type int, long, etc, but as the numbers are in an array now, I need to find a way to to these methods.

I have tried many ways, as you can see, and things like add are fine when both numbers are positive or negative, but when mixed, I am not getting it.

What I need help with is not idea of how to do each type, I know methods, lattice multiplication etc, but I need to know the psuedocode for how I would do this stuff, I am completely lost.

By the way I know I have asked for similar things here and there for each method, sorry I am still not getting it, and it is due in a couple of days, and I have racked my brain all weekend.

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开发者_StackOverflow.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.
    }
}


You need to think about how you multiplied, added, subtracted, and divided like you did back when you were in grade school. That is, you need to think about how you would do these operations by hand with a pencil and a piece of paper. Then you need to write your code to do the operations the same way. The important part is that when doing these operations the same way, you must store one digit in each slot of the array that your version of the BigInteger provides.

Before you start writing code, you should have a clear idea of how to solve the problem, and then write the code to match your solution.

Even if you were presented with a final solution, odds are very good you would not understand it, as the code you have provided so far fails to implement a BigInteger which is larger than a regular Integer.

Jim Garrison suggested talking to a TA, and I would second the recommendation. It isn't a matter of picking the right methods. You either misunderstand the task the professor is asking you, or you misunderstand how one might be able to represent a number larger than Integer.MAX_INT. Either misunderstanding is acceptable in a person who is learning how to program, but asking someone else to provide an answer is going to hurt you badly in the long run.

If you skip understanding by asking for others to do the "hard" parts for you, you will get your Diploma, but the first person who asks you a hard interview question will know that you only have the piece of paper saying "Diploma", not the understanding of computers.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜