Difference between revisions of "PLC Laboratory 2"

From Marek Běhálek Wiki
Jump to navigation Jump to search
(Created blank page)
 
Line 1: Line 1:
 +
== Interpreter of Arithmetic Expressions ==
  
 +
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.
 +
 +
== Input specification ==
 +
 +
The first line of the input contains a number <code>N</code>. It defines the number of expressions your program should evaluate. These expressions are on next <code>N</code> 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 <code>ERROR</code> instead.
 +
 +
== Example ==
 +
* Input
 +
<syntaxhighlight lang="haskell" >
 +
2
 +
2 * (3+5)
 +
15 - 2**7
 +
</syntaxhighlight >
 +
 +
* Output
 +
<syntaxhighlight lang="haskell" >
 +
16
 +
ERROR
 +
</syntaxhighlight >

Revision as of 08:25, 26 January 2022

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.

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