5
我正在为BigIntegers(只是*,^和!)编写一个波兰记法计算器,我得到一个OutOfMemoryError
上线,我减去BigInteger.ONE
以获得阶乘的工作原因?BigInteger上的OutOfMemoryError
package polish_calculator;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.util.Stack;
public class Main {
static BigInteger factorial(BigInteger number){
Stack <BigInteger> factorialStack = new Stack<BigInteger>();
factorialStack.push(number);
while (!number.equals(BigInteger.ONE)){ //load the stack
factorialStack.push(number.subtract(BigInteger.ONE)); // here's the error
}
BigInteger result = BigInteger.ONE;
while(!factorialStack.empty()){ // empty and multiply the stack
result.multiply(factorialStack.pop());
}
return result;
}
public static void main(String[] args) throws IOException {
BigInteger testFactorial = new BigInteger("12");
System.out.println(factorial(testFactorial));
Stack <String> stack = new Stack<String>();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String readExpression = br.readLine();
while(!readExpression.equals("")){
String [] splittedExpression = readExpression.split(" ");
for(int i=0; i<splittedExpression.length;i++){
if(splittedExpression[i].equals("*"))
{
BigInteger operand1 = new BigInteger(stack.pop());
BigInteger operand2 = new BigInteger(stack.pop());
BigInteger result = operand1.multiply(operand2);
String stackString = result.toString();
stack.push(stackString);
}
if(splittedExpression[i].equals("^"))
{
BigInteger operand1 = new BigInteger(stack.pop());
BigInteger operand2 = new BigInteger(stack.pop());
BigInteger result = operand1.modPow(operand2, BigInteger.ONE);
String stackString = result.toString();
stack.push(stackString);
}
if(splittedExpression[i].equals("!"))
{
BigInteger operand1 = new BigInteger(stack.pop());
BigInteger result = factorial(operand1);
String stackString = result.toString();
stack.push(stackString);
}
else{ //it's an integer
stack.push(splittedExpression[i]);
}
} // end for splittedExpression.length
}
}
}
错误:
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.math.BigInteger.subtract(BigInteger.java:1118)
at polish_calculator.Main.factorial(Main.java:45)
at polish_calculator.Main.main(Main.java:65)
Java Result: 1
这个。 number.subtract()实际上并不修改数字。 – Doug 2011-05-14 16:49:32
+1非常好的一点。 @omgzor因此你必须使用'number = number.subtract(BigInteger.ONE);'然后'factorialStack.push(number);' – Boro 2011-05-14 16:50:20