我需要在没有使用Biginteger的情况下划分两个大整数,因为Numbers不能存储在一个基本类型中,因为我需要从我给出的字符串中按char来表示char,我已经创建了一个名为BigNumber的类,通过这门课,我可以:
现在我只需要实现Dividing方法,但我无法理解如何使用两个字符串而不是一个字符串和一个Int来实现它,这是我到目前为止所得到的,如果我们的数字是将String除以得足够小就可以成为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:我的班级也可以将一个大数字减去另一个,如果这对分裂有帮助
答案 0 :(得分:0)
我尝试了你今天早上告诉我的所有内容并且得到了它,谢谢大家,如果你有一些改进请告诉我,因为这只是粗略的代码而没有清除低效率,谢谢大家
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;
}