Real expressions (Calculator)

Now a line is a full expression: numbers, + - /, parentheses and a leading minus (-3, 2 -(1 + 1)), spaces optional. Multiplication and division happen before addition and subtraction, left to right. Errors as before (Error: bad expression for anything malformed).

Input: One expression per line.

Output: One result per line.

Difficulty: Medium. Write your answer in the browser editor, run the examples, then submit it against hidden tests. Python and JavaScript are free; other languages are part of licode Club.

Hint

  • Write a recursive descent parser: expression = term (+|- term), term = unary (|/ unary), unary = -unary | atom, atom = number | ( expression ).

More Calculator practice