Skip to content

Instantly share code, notes, and snippets.

@TomasDrozdik
Created December 10, 2018 18:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TomasDrozdik/8726544b96982f2084d1d4e2067f8f14 to your computer and use it in GitHub Desktop.
Save TomasDrozdik/8726544b96982f2084d1d4e2067f8f14 to your computer and use it in GitHub Desktop.
<p><strong><em>CZ (English version below):</em></strong></p>
<p>Napište výrazovou kalkulačku, která dostává na standardní vstup instrukce a na
standardní výstup vypisuje výsledky, případně chybové hlášky. Na jednom řádku
vstupu je vždy jediný příkaz. Kalkulačka zpracovává příkazy jeden po druhém,
přičemž nejprve dokončí jeden příkaz než začne načítat další. Kalkulačka končí
svou práci, pokud dojdou vstupní data (čtení řádku vrátí <code>null</code>) nebo narazí
na řádek obsahující pouze řetězec <code>"end"</code>. Kalkulačka rozeznává tři příkazy:</p>
<ul>
<li>Řádek začínající symbolem <code>"="</code>, za kterým následuje právě jedna mezera a výraz v preorder formátu (viz níže). Kalkulačka si výraz načte a nadále bude pracovat pouze s ním. Pokud již dříve načetla jiný výraz, starý výraz zapomene a nahradí jej novým. Předchozí správně načtený výraz program zapomene i v případě, že při zpracování příkazu <code>"="</code> došlo k chybě.</li>
<li>Samostatný řetězec <code>"i"</code> vyhodnotí naposledy načtený výraz celočíselně a výsledek (jedno celé číslo) vytiskne na jeden řádek výstupu.</li>
<li>Samostatný řetězec <code>"d"</code> vyhodnotí naposledy načtený výraz v reálné aritmetice s dvojitou přesností (64 bitů) a výsledek vytiskne na pět desetinných míst na jeden řádek výstupu.</li>
</ul>
<p>V načítaném výrazu (za znaménkem <code>"="</code>) se mohou vyskytovat celá kladná čísla,
která se vejdou do 32-bit. integeru se znaménkem (tj. menší než 231 =
2147483648), binární operátory <code>+</code>, <code>-</code>, <code>*</code> a <code>/</code> a operátor unárního minus
<code>~</code>. Operátory i čísla jsou odděleny mezerami.</p>
<p>Při vyhodnocování výrazu v celočíselné aritmetice (příkaz <code>"i"</code>) počítejte s
tím, že všechny mezivýsledky se vejdou do 32-bit. integeru se znaménkem. Pokud
by v průběhu výpočtu došlo k přetečení (mezivýsledek se nevejde do <code>int</code>u se
znaménkem), vypište na samostatný řádek chybový řetězec <code>"Overflow Error"</code>.
Pokud dojde při výpočtu v celočíselné aritmetice k dělení nulou, vypište
řetězec <code>"Divide Error"</code>.</p>
<p>Vyhodnocování výrazu v reálné aritmetice probíhá podle standardních pravidel
IEEE - tj. včetně práce s hodnotami nekonečno, "not a number", apod. Pokud je
výsledkem výrazu nějaká taková hodnota, vypište ji ve standardní textové
reprezentaci poskytované knihovnami .NET, ideálně pomocí <code>.ToString("f05")</code>.</p>
<p>Pokud narazíte na chybně zadaný příkaz nebo zjistíte, že zápis výrazu je z
jakéhokoli důvodu neplatný (objevují se v něm neznámé tokeny, nedodržuje
preorder formát, apod.), vypište na samostatný řádek chybovou hlášku <code>"Format Error"</code>. Pokud se příkaz <code>"i"</code> nebo <code>"d"</code> objeví ve stavu, kdy není načtený
žádný platný výraz, vypište chybovou hlášku <code>"Expression Missing"</code>. Pokud se
na standardním vstupu objeví zcela prázdný řádek, tak ho program ignoruje a
žádnou chybovou hlášku nevypíše.</p>
<p>Pro čtení ze standardního vstupu a výpisech na standardní výstup počítejte s
tím, že váš program bude automaticky v CodExu spuštěn s vhodným nastavením
národního prostředí.</p>
<p><strong>Poznámka:</strong></p>
<p>Při hodnocení bude kladen důraz na zvolený objektový návrh. Úloha je
koncipována tak, abyste si vyzkoušeli návrhový vzor
<a href="http://en.wikipedia.org/wiki/Visitor_pattern">Visitor</a>. Za korektní
implementaci tohoto vzoru můžete dostat až 5 bonusových bodů.</p>
<p><strong>Příklad (řádky začínající <code>"$&gt;"</code> jsou vstupní):</strong></p>
<pre><code>**$&gt;** i
Expression Missing
**$&gt;** $#!%
Format Error
**$&gt;** = + 2 3
**$&gt;** i
5
**$&gt;** d
5.00000
**$&gt;** = / 5 2
**$&gt;** i
2
**$&gt;** d
2.50000
**$&gt;** = +
Format Error
**$&gt;** i
Expression Missing
**$&gt;** end
</code></pre>
<p>Testovací sadu vstupních a vzorových výstupních dat naleznete
<a href="https://recodex.mff.cuni.cz:4000/v1/uploaded-files/09e935ba-aba6-11e7-a937-00505601122b/download">zde</a>.</p>
<p><strong><em>EN (česká verze výše):</em></strong></p>
<p>The goal is write a program capable of serving as a simple expression
calculator. The program processes commands from standard input and prints
results (or error messages) to standard output. There is only one command per
line. Commands are processed sequentially; the previous command is fully
executed before the next one starts loading. The calculator should terminate
after it processes all input data (attempting to read a line results in
<code>null</code>), or when it encounters a line containing only the string <code>"end"</code>. The
calculator recognizes the following commands:</p>
<ul>
<li>A line starting with the <code>"="</code> symbol followed by exactly one space and an expression in preorder format (see below). Such a line should be interpreted by parsing the expression and storing it; following operations will be done over the last parsed expression. If an expression was already stored, the previous expression should be discarded and replaced by the new expression. The previous expression will be discarded even if an error was encountered when processing the <code>"="</code> command.</li>
<li>A single string <code>"i"</code> should be interpreted by evaluating the last expression using integer arithmetic and printing out the result (one integer) on a single output line.</li>
<li>A single string <code>"d"</code> should be interpreted by evaluating the last expression using double-precision floating-point arithmetic (64 bits) and printing out the result on a single output line using 5 decimal places.</li>
</ul>
<p>The expression that follows the <code>"="</code> symbol may contain positive integers
that should fit in a 32-bit signed integer type (i.e. are smaller than 231 =
2147483648), binary operators <code>+</code>, <code>-</code>, <code>*</code> and <code>/</code>, as well as the unary
minus operator <code>~</code> representing additive inverse. Operators and numbers are
separated with spaces.</p>
<p>When evaluating using the integer arithmetic (<code>"i"</code> command), it is safe to
assume that all subresults will fit in a 32-bit signed integer. If an overflow
was encountered when evaluating the expression (i.e. subresult will not fit in
a signed <code>int</code>), the program should print the following line as a result:
<code>"Overflow Error"</code>. If division by zero was encountered, the following string
should be printed: <code>"Divide Error"</code>.</p>
<p>Evaluation of the expression using floating-point arithmetic is done in
accordance with standard IEEE rules, i.e. including special values such as
"infinity", "not a number", etc. If any such value is the result of an
expression evaluation, it should be printed out in the standard text
representation provided by the .NET libraries.</p>
<p>If an invalid command is encountered, or if the textual representation of the
expression is invalid for any reason (unknown tokens, not in preorder
notation, etc.), the program should print the following line: <code>"Format Error"</code>. If the commands <code>"i"</code> or <code>"d"</code> are encountered and no valid
expression is loaded, the program should print <code>"Expression Missing"</code> on a
single line. If an empty line is encountered when processing the input, the
program should ignore it without printing out any error.</p>
<p>When reading from standard input and writing to standard output, it is safe to
assume that your application will be run in CodEx with suitable culture and
regional settings.</p>
<p><strong>Note:</strong></p>
<p>The marking process will put strong emphasis on the (object-oriented)
application design. The purpose of the assignment is to allow you to gain
experience with the <a href="http://en.wikipedia.org/wiki/Visitor_pattern">Visitor</a>
design pattern. Correct use of the pattern will be rewarded with up to bonus 5
points.</p>
<p><strong>Example (lines starting with <code>"$&gt;"</code> represent input):</strong></p>
<pre><code>**$&gt;** i
Expression Missing
**$&gt;** $#!%
Format Error
**$&gt;** = + 2 3
**$&gt;** i
5
**$&gt;** d
5.00000
**$&gt;** = / 5 2
**$&gt;** i
2
**$&gt;** d
2.50000
**$&gt;** = +
Format Error
**$&gt;** i
Expression Missing
**$&gt;** end
</code></pre>
<p>A test suite of example input and output data can be found
<a href="https://recodex.mff.cuni.cz:4000/v1/uploaded-files/09e935ba-aba6-11e7-a937-00505601122b/download">here</a>.</p>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment