CSCE-315: Programming Studio (Fall 2010)

Individual assignment: Prefix expression calculator

Write an interactive desk calculator that evaluates arithmetic expressions on integers. The grammar for expressions is defined as follows:

<expr> ::=  '+' <expr> <expr>;
	|  '-' <expr> <expr> ;
	|  '*' <expr> <expr> ;
	|  '/' <expr> <expr> ;
	|  '%' <expr> <expr> ;
	|  '^' <expr> <expr> ;
	|  <integer>

<integer>    ::= <digit> | <digit> <integer>

<digit>   ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9

The / and % are, respectively, the integral division (returns the quotient) and modulus operators.

The calculator should prompt for an expression, read in a line from the standard input stream, tokenize, parse, and evaluate the expression on that line, and write the result to the standard output stream. The line of input is expected to contain one valid expression. The calculator should inform the user if the input is not recognized as a valid expression. Note that the evaluator should allow any number of whitespace characters (tabs or spaces) anywhere within an expression. The single character 'q' is interpreted as the command to exit the program.

calc> / ^ 2 3 4
answer = 2

calc> + 1 HJK#$#
error  : unrecognized expression!

calc> * +2 3 - 1 3 
answer = -10

calc> q
Bye!
Writing a parser for this should be straight-forward. Scan along the input string, and when an operator is encountered, take specific operation performed on two recursive calls and return the resulting value. The recursion terminates when a integer string is encountered, where the value of the string is returned.

What to turn in

  1. The file calc.cpp, that contains the main function.
  2. All other .cpp and .hpp files that are necessary for building the application—you may or may not have some of these.
  3. A Makefile, or alternatively, the project directory from Code::Blocks in a single zip file (or tar.gz file).

    The first target of the Makefile should build the application, and the executable that results should be named calc. (Or calc.exe if you insist).

  4. A file named report.pdf that describes how to use the calculator, and what are its limitations, if any.

Please use C++ as the implementation language. You may use whatever C++ compiler you desire, but please write portable code; do not use any operation system, compiler, or library distribution specific language or library features. The instructor or TA may test your program using any standards conforming C++ compiler he desires. We will use Code::Blocks IDE in subsequent projects, so it is desirable that you use it. Start with a "Console Application" template.

About outside help and tools

The assignment is to be done individually.

  • This does not mean that you should not discuss your design with others. By all means, do talk to your classmates about ways of implementing a parser and an evaluator, ways to structure the code, a tricky piece of code that you or your classmate is struggling with to get working, etc. You can of course also use the literature to find ideas for how to solve this kind of a programming task, but remember to cite your sources.
  • This does mean, however, that will write your program wholly by yourself, without copying parts of it from codes that others have written.

You may NOT use lexical analyzers or parser generators such as lex or yacc (or flex or bison).

Grading

The assignment will be partially graded by whether your code works or not, but even more importantly, by how you structure your code, comment it, and lay it out. Please consult and try to follow the guidance given during the lectures and the course text.

Author

Original concept/design/most of the text by Jaakko Järvi. Modifications by Yoonsuck Choe.