Note that there are some explanatory texts on larger screens.

plurals
  1. POIdentifying Unary minus in calculator, java
    primarykey
    data
    text
    <p>Hi guys i'm almost done writing my code and i'm stuck with this stupid thing. I can identify cases in which there's a unary minus before brackets (-[4 + 4]). here's my code:</p> <pre><code>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&lt;String, Expression&gt; parseArguments(Tokenizer st) throws IOException, InputException { HashMap&lt;String, Expression&gt; result = new HashMap&lt;String, Expression&gt;(); 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"); } } } } </code></pre> <p>it long i know. Expression object could be a constant, a variable or a function such as MAX{3,2} which is 3. expression() uses a tokenizer i have built to parse text into an expression, and sumExpression() uses expression() to create a new Expression which is a combination of two Expression object according to the right operator.</p> <p>i hope its clear. as i said before i can't figure out how to identify the unary minus (-[4] would be -4) thing. i didn't put my tokenizer code, didn't think its necessary.</p> <p>thanks! </p> <p>P.S. the order of calculations is defined to be left to right with no regards to type of operator.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload