Divide two large numbers as strings without using Bignumbers in java

旧街凉风 提交于 2019-12-08 08:13:10

问题


I need to divide two large integers WITHOUT using Biginteger since the Numbers can't be stored inside a primitive type , since I need to do it char by char from the strings I am given,I have already created a class called BigNumber, with this class I can:

  1. Add
  2. multiply
  3. compare two strings with large integers inside

Now I only need to implement the Dividing method but I can't get my head around how to do it with two strings instead of one String and an Int, here's what I got so far, it works if the number we are dividing the String by is small enough to be an int

class BigNumber {
    String Number;
BigNumber div(BigNumber other) {
        String result= "";
        String num1= this.Number;
        long Divisor = Integer.parseInt(other.Number);
        int index = 0;
        long NumTemp = num1.charAt(index)-'0';
        while (NumTemp < Divisor){
            NumTemp = NumTemp * 10 +(num1.charAt(index+1) - '0');
            index++;
        }
        while (num1.length()-1 > index){
            result += (NumTemp/Divisor) ;
            NumTemp = (NumTemp % Divisor) * 10 + num1.charAt(index+1) - '0';
            index++;
        }
        result += (NumTemp/Divisor);
        System.out.println(result);
        System.out.println(NumTemp);
        BigNumber Big = new BigNumber(result);
        return Big;
    }
}
`

PS: My class can also subtract one large number to another, if that helps with the division


回答1:


I tried what you all told me this morning and got it, thank you all, if you've some improvement over it please tell me, since this is just the rough code without cleaning the inefficiencies, thank you all

BigNumber div(BigNumber other) {
            String result = "";
            String num1 = this.Number;
            String num2 = other.Number;
            int Select = num2.length();
            String temp = num1.substring(0, Select);
            BigNumber tempNum = new BigNumber(temp);
            int NumbersLeft = num1.length() - temp.length();
            BigNumber MultObject = new BigNumber("1");
            if (tempNum.compareTo(other) < 0) {
                temp = num1.substring(0, Select+1);
                tempNum.Number = temp;
                NumbersLeft--;
                Select++;
            }
            do {
                MultObject.Number = "0";
                int Index = 0;
                while (other.mult(MultObject).compareTo(tempNum) < 0) {
                    Index++;
                    MultObject.Number = Integer.toString(Index);
                }
                Index--;
                MultObject.Number = Integer.toString(Index);
                String Carry = tempNum.sub(other.mult(MultObject)).Number;
                if (NumbersLeft > 0) {
                    Select++;
                    Carry += num1.charAt(Select - 1);
                    NumbersLeft--;
                }
                result += Index;
                tempNum.Number = Carry;
            }while (NumbersLeft > 0);
            MultObject.Number = "0";
            int Index = 0;
            while (other.mult(MultObject).compareTo(tempNum) < 0) {
                Index++;
                MultObject.Number = Integer.toString(Index);
            }
            Index--;
            MultObject.Number = Integer.toString(Index);
            String Carry = tempNum.sub(other.mult(MultObject)).Number;
            if (NumbersLeft > 0) {
                Select++;
                Carry += num1.charAt(Select - 1);
                NumbersLeft--;
            }
            result += Index;
            tempNum.Number = Carry;
                BigNumber Big = new BigNumber(result);
                return Big;
            }


来源:https://stackoverflow.com/questions/48582606/divide-two-large-numbers-as-strings-without-using-bignumbers-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!