嗨,大家好 我快写完我的代码了,我被这个愚蠢的东西卡住了。我可以确定在括号前有一个一元减号的情况( - [4 + 4])。这里是我的代码:识别一元减号在计算器中,java
package oop.ex2.expression;
import java.io.IOException;
import java.util.HashMap;
import oop.ex2.exception.*;
import oop.ex2.main.Tokenizer;
/**
* This class contains 3 public static methods. All 3 methods are used
* to parse text into a mathematical expression. The information is "thrown"
* back and forth from one method to another.
*/
public class ExpressionParser {
/**
* This method uses expression() method to parse the text into mathematical
* expressions, and returns an expression which is the sum of all
* expressions returned from expression() [the sum is calculated according
* to the right operator]
*
* @param st - the Tokenizer parsing the text
* @return - Expression, the sum of all expressions from expression()
* @throws InputException
* @throws IOException
*/
public static Expression sumExpressions(Tokenizer st)
throws InputException, IOException {
boolean endOfLine = false;
Expression temp = expression(st);
int token = Tokenizer.TT_NOTHING;
while (!endOfLine) {
token = st.nextToken();
if ((token == Tokenizer.TT_OPERATOR)
|| (token == Tokenizer.TT_OVERLOADED_OP))
temp = new FatherExpression(st.op, temp, expression(st));
else
endOfLine = true;
}
return temp;
}
public static Expression expression(Tokenizer st) throws InputException, IOException {
Expression result = null;
switch (st.nextToken()) {
case Tokenizer.TT_NUMBER:
result = new NumberExpression(st.nval);
break;
case Tokenizer.TT_VARIABLE:
result = new VariableExpression(st.sval);
break;
case Tokenizer.TT_FUNC:
result = createFunction(st);
break;
case '[':
result = sumExpressions(st);
if (st.ttype != ']')
throw new BracketException("BracketException: "
+ "one too many ']'");
break;
default:
throw new UnexpectedTokenException("Unexpected token on" +
"ExpressionParser.elements(st)");
}
return result;
}
private static Expression createFunction(Tokenizer st)
throws IOException, InputException {
if (InlineManager.getAllInlineFunctions().containsKey(st.sval)) {
InlineFunction temp = InlineManager.getInlineFunction(st.sval);
temp.setArguments(st);
return temp;
}
if (st.sval.equals("MAX"))
return new Max(st);
if (st.sval.equals("MIN"))
return new Min(st);
if (st.sval.equals("POW"))
return new Pow(st);
if (st.sval.equals("MOD"))
return new Mod(st);
if (st.sval.equals("ABS"))
return new Abs(st);
throw new FunctionNameException("Wrong funcion entred " + st.sval);
}
public static HashMap<String, Expression> parseArguments(Tokenizer st)
throws IOException, InputException {
HashMap<String, Expression> result = new HashMap<String, Expression>();
if (st.nextToken() != '{')
throw new UnexpectedTokenException("Missing {");
int argument = 0;
while (true) {
st.ignoreToken(',', true);
switch (st.nextToken()) {
case '}':
st.ignoreToken(',', false);
return result;
case '[':
result.put(String.valueOf(argument++), sumExpressions(st));
break;
case Tokenizer.TT_NUMBER:
result.put(String.valueOf(argument++), new NumberExpression(st.nval));
break;
case Tokenizer.TT_VARIABLE:
result.put(String.valueOf(argument++), new VariableExpression(st.sval));
break;
case Tokenizer.TT_FUNC:
result.put(String.valueOf(argument++), createFunction(st));
break;
default:
throw new UnexpectedTokenException("Unexpected token on function arguments");
}
}
}
}
它只要我知道。表达式对象可以是常量,变量或函数,例如MAX {3,2},它是3. expression()使用我构建的用于将文本解析为表达式的标记器,sumExpression()使用expression()创建一个新的Expression,它是根据右操作符的两个Expression对象的组合。
我希望它清楚。正如我之前所说,我不知道如何识别一元减号( - [4]将是-4)的事情。我没有放置我的tokenizer代码,没有想到它的必要性。
谢谢!
P.S. 计算顺序被定义为从左到右而不考虑操作符的类型。