Difference between revisions of "PLC Laboratory 1"
(4 intermediate revisions by the same user not shown) | |||
Line 3: | Line 3: | ||
Implement an interpreter of arithmetic expressions. These expressions contain <code>+, -, *, /</code> operators (with common priorities and left associativity) and parentheses. | Implement an interpreter of arithmetic expressions. These expressions contain <code>+, -, *, /</code> operators (with common priorities and left associativity) and parentheses. | ||
− | To simplify the task, consider we have only binary operators. There are no unary operators in our language. Moreover, we can use only positive integers in our expressions. | + | To simplify the task, consider we have only binary operators. There are no unary operators in our language. Moreover, we can use only positive integers in our expressions. So, also the operator <code>/</code> is in fact integer division of two integer numbers (function <code>div</code> in most programming languages). |
+ | |||
+ | Expressions are written in free format, spaces or tabulators should be ignored. | ||
== Input specification == | == Input specification == | ||
Line 15: | Line 17: | ||
== Example == | == Example == | ||
* Input | * Input | ||
− | <syntaxhighlight lang=" | + | <syntaxhighlight lang="haskell" > |
2 | 2 | ||
2 * (3+5) | 2 * (3+5) | ||
Line 22: | Line 24: | ||
* Output | * Output | ||
− | <syntaxhighlight lang=" | + | <syntaxhighlight lang="haskell" > |
16 | 16 | ||
ERROR | ERROR | ||
</syntaxhighlight > | </syntaxhighlight > |
Latest revision as of 15:27, 18 February 2025
Contents
Interpreter of Arithmetic Expressions
Implement an interpreter of arithmetic expressions. These expressions contain +, -, *, /
operators (with common priorities and left associativity) and parentheses.
To simplify the task, consider we have only binary operators. There are no unary operators in our language. Moreover, we can use only positive integers in our expressions. So, also the operator /
is in fact integer division of two integer numbers (function div
in most programming languages).
Expressions are written in free format, spaces or tabulators should be ignored.
Input specification
The first line of the input contains a number N
. It defines the number of expressions your program should evaluate. These expressions are on next N
lines. Each line contains exactly one expression.
Output specification
For each expression write one line containing the result – the computed value of the expression. If there is any error in the input, write text ERROR
instead.
Example
- Input
2
2 * (3+5)
15 - 2**7
- Output
16
ERROR