【发布时间】:2021-10-04 05:05:06
【问题描述】:
我有这个任务,我应该在其中添加或减去非常大的数字(超长)。我需要使用 String 和/或 StringBuilder 使其工作。
我这里有一些代码,但大多数情况下都不起作用,而且减法确实有问题。
/** This class contains the method meant to calculate the sum of two large numbers.
*/
public class SummingLargeNumbers {
private SummingLargeNumbers() {}
private static final char ZERO = '0';
/**
* Implements addition of two large numbers.
*
* @param s1 First number as string.
* @param s2 Second number as string.
* @return The answer as a string.
*/
static String findSum(String s1, String s2) {
if (s1 == null || s2 == null || s1.equals("") || s2.equals("")) {
return "";
}
if (s1.length() > s2.length()) {
String temp = s1;
s1 = s2;
s2 = temp;
}
StringBuilder result = new StringBuilder();
int n1 = s1.length();
int n2 = s2.length();
int diff = n2 - n1;
int denom = 0;
for (int i = n1 - 1; i >= 0; i--) {
int sum = ((int) (s1.charAt(i) - ZERO) + (int) (s2.charAt(i + diff) - ZERO) + denom);
result.append((char) (sum % 10 + ZERO));
denom = sum / 10;
}
for (int i = n2 - n1 - 1; i >= 0; i--) {
int sum = ((int) (s2.charAt(i) - ZERO) + denom);
result.append((char) (sum % 10 + ZERO));
denom = sum / 10;
}
if (denom > 0) {
result.append((char) (denom + ZERO));
}
return result.reverse().toString();
}
/**
* Checks whether the first number is smaller than the second.
*
* @param s1 First number as a string.
* @param s2 Second number as a string.
* @return True if the first number is smaller than the second and false if otherwise.
*/
static boolean isSmaller(String s1, String s2) {
int firstLength = s1.length();
int secondLength = s2.length();
if (firstLength < secondLength) return true;
if (secondLength < firstLength) return false;
for (int i = 0; i < firstLength; i++) {
if (s1.charAt(i) < s2.charAt(i)) return true;
else if (s1.charAt(i) > s2.charAt(i)) return false;
}
return false;
}
/**
* Implements subtraction of two large numbers.
*
* @param s1 First number as a string.
* @param s2 Second number as a string.
* @return The answer as a string.
*/
static String findDiff(String s1, String s2) {
if (s1 == null || s2 == null || s1.equals("") || s2.equals("")) {
return "";
}
if (isSmaller(s1, s2)) {
String temp = s1;
s1 = s2;
s2 = temp;
}
StringBuilder result = new StringBuilder();
int n1 = s1.length();
int n2 = s2.length();
int diff = n1 - n2;
int denom = 0;
for (int i = n2 - 1; i >= 0; i--) {
int sum =
(((int) s1.charAt(i + diff) - (int) '0') - ((int) s2.charAt(i) - (int) '0') - denom);
if (sum < 0) {
sum = sum + 10;
denom = 1;
} else {
denom = 0;
}
result.append(String.valueOf(sum));
}
for (int i = n1 - n2 - 1; i >= 0; i--) {
if (s1.charAt(i) == '0' && denom > 0) {
result.append("9");
continue;
}
int sub = (((int) s1.charAt(i) - (int) '0') - denom);
if (i > 0 || sub > 0) {
result.append(String.valueOf(sub));
denom = 0;
}
}
if (result.reverse().indexOf("0") == 0) {
result.deleteCharAt(0);
}
return result.toString();
}
}
我真的不明白我哪里出错了,现在我还必须在一种方法中实现加法和减法,这对我来说真的很难。 如果我能对这整件事有所了解,我会很高兴,这样我就可以尝试让它发挥作用。
例如,一些不起作用的情况是:
-88131415555612 + 77020304444501 = 11111111111111
在这种情况下,它应该返回 -11111111111111
-1021315510 + 2183194 = 1019132316
这里也应该返回 -1019132316
与减法有关的大多数情况都是错误的。
【问题讨论】:
-
我建议查看
BigInteger并使用该类或至少从中收集一些实现提示。至于“我哪里出错了” - 您应该使用调试器逐步检查您的代码并检查每一步的值。如果您了解您正在实施的算法,您应该能够发现错误。 -
@Thomas 我只能为此使用 String 或 StringBuilder。关于我使用学校逻辑的实现,我计算当前数字并进位,完成后我只需将剩余数字添加到较小的数字中。
-
我只允许使用 String 或 StringBuilder,这就是问题所在。
-
如果您提供一些不起作用的示例,可能会有所帮助。请注意,我们正在投入时间努力使其发挥作用。
标签: java string stringbuilder