Lab04 1

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

CSCI 152, Performance and Data Structures,

Lab Exercise 4

The deadline for his lab exercise is Thursday 15th of February at 11PM. All
code must be written in the file rpn.cpp.
In this exercise, you will use a stack for the evaluation of RPN (Reverse
Polish Notation). RPN is described in the slides of video lesson B1, stack.pdf,
slides 24-28, and it was explained in the lecture on Thursday 8th.
We will use the stack from the standard template library (STL). Its interface
has a few differences from the interface that we have been using during the
lectures:
• In order to declare or construct a stack, you have to specify the type that
you want to put on the stack. This is done by writing the type between
< >. In our case, we use std::stack< double >.
• In order to access the top of the stack, use method top( ).
In order to make it possible to print an std::stack, we created a print function
in file printstack.h. It is not possible to print std::stack efficiently, because
it doesn’t allow access to elements in the middle. Our print function makes a
copy of the stack and then prints the 10 topmost elements while popping them.
Copying is done automatically, because we declared the print function as
std::ostream& operator << ( std::ostream& out, std::stack<T> s ) in-
stead of the usual
std::ostream& operator << ( std::ostream& out, const std::stack<T> & s )
The resulting program will be an RPN evaluator, that asks for input in RPN
and prints the result. File rpn.cpp contains a variable show that determines if
intermediate steps are shown. It is initially set to true. This is an example of
a possible input:
90 pi * 180 / sin = // must print 1.0
The RPN evaluator has the following subtasks: Read input from std::cin
and decide if the input is a number or an operator. Cutting the input into
small pieces and classifying them is called tokenizing, and the function that
does it, is called tokenizer. In this task, the tokenizer is called read( ) and is
implemented in file input.cpp. It returns a pair, containing the type of input
that was recognized, together with its length. The possible types are:

1
inp_plus, inp_minus, inp_times, inp_div, inp_mod, inp_pow,
inp_sin, inp_cos, inp_tan, inp_exp, inp_log,
inp_sqrt, inp_abs,
inp_pi, inp_e,
inp_num, inp_ident, inp_scanerror, inp_comment, inp_whitespace,
inp_end
For example, if the user types sin, the tokenizer will return a pair ( inp_sin, 3 ).
If the user types 100.5, the tokenizer will return ( inp_num, 5 ). The meanings
of most of the possible input types are clear from their name. inp_ident means
identifier, which unfortunately always results in a error, because we are not
storing values of identifiers in this exercise. inp_comment denotes a C-style
comment, which will be silently ignored. Similarly, inp_whitespace denotes
whitespace characters, which also will be ignored. inp_end denotes a symbol
that ends the RPN expression. It is =, ;, or end-of-file.

1 Tasks
1. Write function apply0( inputtype op ) in file rpn.cpp. It must work
for inp_e (the Euler constant), and inp_pi (number π.)
Don’t be lazy when definining these constants! double has a precision of
approximately 16 decimal digits. We want to see them.
2. Write function apply1( inputtype op, double d1 ) in file rpn.cpp. It
must work for inp_exp, inp_log, inp_sqrt,
inp_sin, inp_cos, inp_tan, and inp_abs.
3. Write function apply2( inputtype op, double d1, double d2 ) in file
rpn.cpp. It must work for inp_plus, inp_minus, inp_times,
inp_div, inp_mod, and inp_pow. Computing modulo on double is a bit
tricky, use: d1 - static_cast<long int> (( d1 / d2 )) * d2 ;

At this point, the RPN evaluator should work. Test it carefully, for example
with
1 1 + = // prints 2
1 exp e - = // prints 0
2 3 * 4 + = // prints 10
Files test1.rpn, test2.rpn, test3.rpn, test4.rpn contain a few more ex-
amples. In linux, you can type ./rpn < testX.rpn.

2 Submission
Submit the file rpn.cpp before the deadline. Don’t use any archivers.

You might also like