将两个大数分隔为字符串,而无需在Java中使用Bignumbers [英] Divide two large numbers as strings without using Bignumbers in java

查看:92
本文介绍了将两个大数分隔为字符串,而无需在Java中使用Bignumbers的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要不使用Biginteger将两个大整数相除,因为数字不能存储在原始类型内,因为我需要从给出的字符串中逐个字符地进行处理,所以我已经创建了一个名为BigNumber的类,通过这个课程,我可以:

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. 添加
  2. 相乘
  3. 比较两个带有大整数的字符串

现在,我只需要实现Dividing方法,但是我无法用两个字符串而不是一个String和一个Int来解决这个问题,到目前为止,这就是我可以得到的结果,如果我们使用的数字是将String除以足够小以成为int

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:我的班级也可以将一个大数减去另一个大数,如果这有助于除法

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

推荐答案

今天早上我尝试了大家都告诉我的内容,谢谢大家,如果您对此有所改进,请告诉我,因为这仅仅是粗略的代码,没有消除效率低下的情况,谢谢大家

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;
            }

这篇关于将两个大数分隔为字符串,而无需在Java中使用Bignumbers的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆