안녕하세요 여러분 저는 거의 코드 작성을 끝 냈고이 바보 같은 짓을하고 있습니다. 대괄호 (- [4 + 4]) 앞에 단항 마이너스가있는 경우를 식별 할 수 있습니다. 여기 내 코드는 다음과 같습니다.계산기에서 단항 마이너스 확인
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}와 같은 함수가 될 수 있습니다. expression()은 텍스트를 구문 분석하기 위해 작성한 tokenizer를 사용하고 sumExpression()은 expression()을 사용하여 오른쪽 연산자에 따라 두 Expression 개체의 조합 인 새 Expression입니다.
나는 분명히 희망합니다. 내가 전에 말했듯이, 단항 마이너스 (- [4]는 -4 일 것)를 식별하는 방법을 알 수 없습니다. 나는 tokenizer 코드를 넣지 않았고 필요하다고 생각하지 않았다.
감사합니다.
P. 계산 순서는 연산자의 유형에 관계없이 왼쪽에서 오른쪽으로 정의됩니다.