堆栈行为与数学
我添加了两个非常大的整数,最多125位数字,不使用Integer类或BigInteger类,只使用java的Stack实用程序。它只是将两个大整数加载到一个堆栈中,然后比较每个pop()
。堆栈行为与数学
我最初具有加载栈,A的方法和B从自己JTextArea.getText()
public Stack<Integer> loadStack(String numA)
{
Scanner scan = new Scanner(numA);
Stack<Integer> stack = new Stack<Integer>();
while (scan.hasNext())
{
stack.push(scan.nextInt());
}
//System.out.println(stack.toString());
return stack;
}
,然后我的方法,其显示所得叠层被称为resTF.setText(num.addStacks(stackA, stackB).toString());
其中resTF是另一个的JTextArea为的结果。
我的方法,增加了带两个Stack<Integer>
的
public Stack<Integer> addStacks(Stack<Integer> stackA, Stack<Integer> stackB)
{
Stack<Integer> resultStack = new Stack<Integer>();
while(!stackA.empty() && !stackB.empty())
{
try
{
int carry = 0;
//get the digits to add
int tokenA = stackA.pop();
int tokenB = stackB.pop();
//add them and mod 10
int result = tokenA + tokenB + carry;
int resultDigit = result % 10;
//push the result on to the new stack
resultStack.push(resultDigit);
//the updated carry
carry = result/10;
if (carry > 0)
{
resultStack.push(carry);
}
}
catch(ArithmeticException e)
{
e.printStackTrace();
}
}
System.out.println(resultStack.toString());
return resultStack;
}
1:我的筹码是给我的输出,例如,[6, 66]
加入555
和111
时,当所需的输出将[6,6,6]
我觉得呢?为什么是这样?因为它被读入的方式?我相信我可能会加入其中。当我输入非常非常大的数字,如100000000000000000000000000000000000000
和200000000000000000000000000000000000000
我得到了,所以我知道它的loadStacks方法是导致问题的原因,尤其是扫描它。我缺少什么?
Exception in thread "AWT-EventQueue-0" java.util.InputMismatchException: For input string: "100000000000000000000000000000000000000"
at java.util.Scanner.nextInt(Scanner.java:2123)
at java.util.Scanner.nextInt(Scanner.java:2076)
at GUI.BigNumber.loadStack(BigNumber.java:19)
EDIT 1 *****
public void checkJagged(Stack<Integer> stackA, Stack<Integer> stackB)
{
int stackSizeA = stackA.size();
int stackSizeB = stackB.size();
if (stackA.size() < stackB.size())
{
for (int i = 0; i < stackSizeB; ++i)
{
if (stackA.elementAt(i) == null)
{
stackA.push(0);
}
}
}
if (stackA.size() > stackB.size())
{
for (int i = 0; i < stackSizeA; ++i)
{
if (stackB.elementAt(i) == null)
{
stackB.push(0);
}
}
}
}
输入处理是造成所描述的问题的一部分 - 扫描仪会读取整个号码作为一个值。做类似的事情
for (int i = 0; i < numA.length(); i++) {
stack.push(Integer.parseInt(numA.substring(i, i + 1));
}
另一个问题是,你推循环进位。这将导致1 2 1 2 1 2为666 + 666与一个固定的解析器。它'足以在循环中增加进位,并且仅在循环之后推进最终进位值。另外,在循环之前将它设置为0,所以前一个进位实际上被添加(相反被0覆盖)。
此外,您需要考虑堆栈大小不同的情况。最简单的方法是在一个堆栈不空的情况下继续前进,并将耗尽的堆栈视为包含零。
我认为你的问题是你期望nextInt()
只返回一个数字,但它确实会返回所有连续的数字。
您需要使用文本框内容String
并处理这些字符。
public Stack<Integer> loadStack(String numA)
{
if(numA == null) throw new IllegalArgumentException("...");
char[] chars = numA.toCharArray();
Stack<Integer> stack = new Stack<>();
for (char c : chars) {
if (Character.isDigit(c))
stack.push((c - '1') < 9 ? (c - '1' + 1) : 0);
}
return stack;
}
我明白了,我已经采取了所有这些事情考虑并固定它,一切都显示正确的数字和正确的文本:)唯一的问题,我现在是不是真的在栈上的故障的问题我需要保持与队列类似的顺序,而不是颠倒它。 [2。 3. 3.1]我想要[1,3,3,2] – SenjuXo
您可以使用大小为(Math.max(stackA.size(),stackB.size())+ 1)的整数数组作为中间存储,然后从那里构建堆栈(如果你必须使用堆栈,否则我只需要在任何地方使用数组)。请注意,storig sigle数字和使用对象数组在这里的内存效率相当低。你可以在每个条目中存储多个数字而不需要修改太多(基本上你会从基数10转换为基数1000000或类似) –
我已经编辑了我的问题,以确保两个堆栈的大小与输入0相同if他们不是,我原来的帖子里面编辑的东西是关闭的吗? @Stefan Haustein – SenjuXo