Java 前缀表达式计算器有问题
我的前缀表达式计算器出错 我尝试运行它时遇到的错误是Java 前缀表达式计算器有问题,java,stack,expression,prefix,Java,Stack,Expression,Prefix,我的前缀表达式计算器出错 我尝试运行它时遇到的错误是 Expression (+ (- 6) (* 2 3 4) (/ (+ 3) (- 2 3 1))) Expression in thread "main" java.lang.NumberFormatException: For input string: "(+ (- 6) (* 2 3 4) (/ (+ 3) (* 1) (- 2 3 1)))" at sun.misc.FloatingDecimal.readJavaFormatExc
Expression (+ (- 6) (* 2 3 4) (/ (+ 3) (- 2 3 1)))
Expression in thread "main" java.lang.NumberFormatException: For input string: "(+ (- 6) (* 2 3 4) (/ (+ 3) (* 1) (- 2 3 1)))"
at sun.misc.FloatingDecimal.readJavaFormatException: For input string "(+ (- 6) (* 2 3 4) (/ (+ 3) (* 1) (- 2 3 1)))"
at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)
//代码从这里开始
导入java.util.*;
公共类SimpleLispExpressionEvaluator{
//当前输入Lisp表达式
私有字符串inputExpr;
//主堆栈和临时堆栈,请参见evaluate()中的算法
私有堆栈表达式堆栈;
私有栈tempStack;
//默认构造函数
//将inputExpr设置为“”
//创建堆栈对象
公共SimpleLispExpressionEvaluator()
{
inputExpr=“”;
expressionStack=新堆栈();
tempStack=新堆栈();
}
//默认构造函数
//将inputExpr设置为inputExpression
//创建堆栈对象
公共SimpleLispExpressionEvaluator(字符串输入表达式)
{
inputExpr=inputExpression;
expressionStack=新堆栈();
tempStack=新堆栈();
}
//将inputExpr设置为inputExpression
//清除堆栈对象
公共无效重置(字符串inputExpression)
{
inputExpr=inputExpression;
堆栈表达式堆栈=新堆栈();
Stack tempstack=新堆栈();
}
私有布尔checkifNumber(){
返回false;
}
//此函数用于计算当前运算符及其操作数
//请参阅evaluate()中的完整算法
//
//主要步骤:
//从expressionStack中弹出操作数并将其推送到
//临时堆栈,直到找到运算符
//将运算符应用于tempStack上的操作数
//将结果推入expressionStack
//
`私有双加(){
double op1=tempStack.pop();
double op2=tempStack.pop();
双温=op1+op2;
返回温度;
}`
私有双倍乘{
double op1=tempStack.pop();
double op2=tempStack.pop();
双温=op1*op2;
返回温度;
}
私有双减法(){
if(tempStack.size()==1){
double temp=-tempStack.pop();
返回温度;
}否则{
double op1=tempStack.pop();
double op2=tempStack.pop();
双温=op1-op2;
返回温度;
}
}
私人双重划分(){
if(tempStack.size()==1){
double temp=1/tempStack.pop();
返回温度;
}else if(tempStack.pop()==0 | | tempStack.pop()==null){
抛出新的IndexOutOfBoundsException();}否则{
double op1=tempStack.pop();
double op2=tempStack.pop();
双温=op1-op2;
返回温度;
}
}
私有void evaluateCurrentOperation()
{
while(expressionStack.peek().getClass().getName().equals(“java.lang.Double”)){
tempStack.push((双)表达式stack.pop());
}
Character操作符=(Character)expressionStack.pop();
双结果=空;
开关(操作员){
格“+”:
结果=添加();
打破
案例“*”:
结果=乘法();
打破
案例'-':
结果=减法();
打破
案例“/”:
结果=除法();
打破
}
expressionStack.push(结果);
}
/**
*此函数用于计算inputExpr中的Lisp表达式
*它返回表达式的结果
*
*算法:
*
*步骤1扫描表达式字符串中的标记。
*步骤2如果看到操作数,请将操作数对象推到expressionStack上
*步骤3如果看到“(”,则下一个标记应该是运算符
*步骤4如果看到操作符,将操作符对象推到expressionStack上
*步骤5如果您看到“)”//evaluateCurrentOperation()中的步骤:
*步骤6:弹出操作数并将其推送到tempStack上
*直到你找到接线员
*步骤7将运算符应用于tempStack上的操作数
*步骤8将结果推送到expressionStack中
*步骤9如果令牌用完,则expressionStack顶部的值为
*是表达式的结果。
*/
公众双重评价()
{
//只提供了大纲。。。
//您需要添加语句
//您可以删除或修改此方法中的任何语句
//使用scanner标记inputExpr
扫描仪inputExprScanner=新扫描仪(inputExpr);
//使用零个或多个空格作为分隔符,
//它将字符串拆分为单字符标记
inputExprScanner=inputExprScanner.useDelimiter(“\\s*”);
//步骤1:扫描字符串中的令牌。
while(inputExprScanner.hasNext())
{
//步骤2:如果看到操作数,请将操作数对象推到expressionStack上
if(inputExprScanner.hasNextInt())
{
//这迫使扫描仪抓取所有的数字
//否则,它只会得到一个字符
字符串dataString=inputExprScanner.FindLine(\\d+);
expressionStack.push(新的双精度(数据字符串));
//更多。。。
}
//code starts here
import java.util.*;
public class SimpleLispExpressionEvaluator {
// Current input Lisp expression
private String inputExpr;
// Main stack & temp stack, see algorithm in evaluate()
private Stack<Object> expressionStack;
private Stack<Double> tempStack;
// default constructor
// set inputExpr to ""
// create stack objects
public SimpleLispExpressionEvaluator()
{
inputExpr = "";
expressionStack = new Stack<Object>();
tempStack = new Stack<Double>();
}
// default constructor
// set inputExpr to inputExpression
// create stack objects
public SimpleLispExpressionEvaluator(String inputExpression)
{
inputExpr = inputExpression;
expressionStack = new Stack<Object>();
tempStack = new Stack<Double>();
}
// set inputExpr to inputExpression
// clear stack objects
public void reset(String inputExpression)
{
inputExpr = inputExpression;
Stack<Object> expressionStack = new Stack<Object>();
Stack<Double> tempstack = new Stack<Double>();
}
private boolean checkifNumber() {
return false;
}
// This function evaluate current operator with its operands
// See complete algorithm in evaluate()
//
// Main Steps:
// Pop operands from expressionStack and push them onto
// tempStack until you find an operator
// Apply the operator to the operands on tempStack
// Push the result into expressionStack
//
`private double add() {
double op1 = tempStack.pop();
double op2 = tempStack.pop();
double temp = op1 + op2;
return temp;
}`
private double multiply() {
double op1 = tempStack.pop();
double op2 = tempStack.pop();
double temp = op1 * op2;
return temp;
}
private double subtract() {
if (tempStack.size() == 1) {
double temp = -tempStack.pop();
return temp;
} else {
double op1 = tempStack.pop();
double op2 = tempStack.pop();
double temp = op1 - op2;
return temp;
}
}
private double divide() {
if (tempStack.size() == 1) {
double temp = 1 / tempStack.pop();
return temp;
} else if (tempStack.pop() == 0 || tempStack.pop() == null) {
throw new IndexOutOfBoundsException(); } else {
double op1 = tempStack.pop();
double op2 = tempStack.pop();
double temp = op1 - op2;
return temp;
}
}
private void evaluateCurrentOperation()
{
while( expressionStack.peek().getClass().getName().equals("java.lang.Double") ) {
tempStack.push( (Double)expressionStack.pop() );
}
Character operator = (Character)expressionStack.pop();
Double result = null;
switch( operator ) {
case '+':
result = add();
break;
case '*':
result = multiply();
break;
case '-':
result = subtract();
break;
case '/':
result = divide();
break;
}
expressionStack.push( result );
}
/**
* This function evaluates Lisp expression in inputExpr
* It return result of the expression
*
* The algorithm:
*
* Step 1 Scan the tokens in the expression string.
* Step 2 If you see an operand, push operand object onto the expressionStack
* Step 3 If you see "(", next token should be an operator
* Step 4 If you see an operator, push operator object onto the expressionStack
* Step 5 If you see ")" // steps in evaluateCurrentOperation() :
* Step 6 Pop operands and push them onto tempStack
* until you find an operator
* Step 7 Apply the operator to the operands on tempStack
* Step 8 Push the result into expressionStack
* Step 9 If you run out of tokens, the value on the top of expressionStack is
* is the result of the expression.
*/
public double evaluate()
{
// only outline is given...
// you need to add statements
// you may delete or modify any statements in this method
// use scanner to tokenize inputExpr
Scanner inputExprScanner = new Scanner(inputExpr);
// Use zero or more white space as delimiter,
// which breaks the string into single character tokens
inputExprScanner = inputExprScanner.useDelimiter("\\s*");
// Step 1: Scan the tokens in the string.
while (inputExprScanner.hasNext())
{
// Step 2: If you see an operand, push operand object onto the expressionStack
if (inputExprScanner.hasNextInt())
{
// This force scanner to grab all of the digits
// Otherwise, it will just get one char
String dataString = inputExprScanner.findInLine("\\d+");
expressionStack.push(new Double(dataString));
// more ...
}
else
{
// Get next token, only one char in string token
String aToken = inputExprScanner.next();
char item = aToken.charAt(0);
String nextToken;
char nextItem;
switch (item)
{
// Step 3: If you see "(", next token should be an operator
case '(':
nextToken = inputExprScanner.next();
nextItem = nextToken.charAt(0);
// Step 4: If you see an operator, push operator object onto the expressionStack
if (nextItem == '+') {
expressionStack.push(nextItem);
} else if (nextItem == '-') {
expressionStack.push(nextItem);
} else if (nextItem == '*') {
expressionStack.push(nextItem);
} else {
expressionStack.push(nextItem);
}
break;
// Step 5: If you see ")" // steps 6,7,8 in evaluateCurrentOperation()
case ')':
try {
evaluateCurrentOperation();
} catch (EmptyStackException e) {
break;
}
break;
default: // error
throw new RuntimeException(item + " is not a legal expression operator");
} // end switch
} // end else
} // end while
// Step 9: If you run out of tokens, the value on the top of expressionStack is
// is the result of the expression.
//
// return result
double result = new Double(inputExpr);
return result;
}
// This static method is used by main() only
private static void evaluateExprt(String s, SimpleLispExpressionEvaluator expr)
{
Double result;
System.out.println("Expression " + s);
expr.reset(s);
result = expr.evaluate();
System.out.printf("Result %.2f\n", result);
System.out.println("-----------------------------");
}
// simple tests
public static void main (String args[])
{
SimpleLispExpressionEvaluator expr= new SimpleLispExpressionEvaluator();
String test1 = "(+ (- 6) (* 2 3 4) (/ (+ 3) (* 1) (- 2 3 1)))";
String test2 = "(+ (- 632) (* 21 3 4) (/ (+ 32) (* 1) (- 21 3 1)))";
String test3 = "(+ (/ 2) (* 2) (/ (+ 1) (+ 1) (- 2 1 )))";
String test4 = "(+ (/2))";
String test5 = "(+ (/2 3 0))";
String test6 = "(+ (/ 2) (* 2) (/ (+ 1) (+ 3) (- 2 1 ))))";
evaluateExprt(test1, expr);
evaluateExprt(test2, expr);
evaluateExprt(test3, expr);
evaluateExprt(test4, expr);
evaluateExprt(test5, expr);
evaluateExprt(test6, expr);
} }
// Step 9: If you run out of tokens, the value on the top of expressionStack is
// is the result of the expression.
//
// return result
double result = new Double(inputExpr);
double result = (Double)expressionStack.pop();