Description
Write a C++ program that implements a simple arithmetic calculator. Input to the
calculator consists of lines composed of real numbers combined together using the
arithmetic operators and The arithmetic expressions in input lines are given in
postfix forms, which eliminates the need of using parentheses.
You can use a container (of type ) to evaluate a postfix expression. The
routine defines an empty and gets the input from the token by token until
where a token is defined a C++ that doesn’t contain any white spaces. For each
token in the input stream, it calls the following routine to process the token.
This routine inspects each
character in the in sequence and takes the proper action according to the
inspected character in the If the character is a digit, a unary sign that
can be determined by the predicate or a valid decimal point that can
be determined by the predicate it calls the routine to
get all characters of the corresponding number from the whose description
and the descriptions of the two helper functions in this process are given below)
and puts the number in
This function (a
predicate) checks if the character in position in the is a valid unary
sign, which is considered valid if it’s not the last character in the and the
character follows it is either a digit or a decimal point.
This function (a
predicate) checks if the character in position in the is a valid decimal
point, which is considered valid if it’s not the last character in the and the
character follows it is a digit.
This function starts with the character in position in the
and constructs a double–precision floating number from a set of consecutive
characters in the It also counts the total number of decimal points in the
corresponding number, and in case the number contains more than one decimal
point, it indicates this error by printing out an error message on Otherwise,
it calls the routine to convert the processed substring to a
number and returns it to the routine The last argument
indicates that if is a decimal point.
If a character inspected in the routine is a valid binary operator that can
be determined by the predicate then the pops two top
numbers from the computes the corresponding operation on these numbers by
A Simple Calculator
2
calling the helper function and puts the resulting number in the To
pop a number from the the routine can be used.
This function (a predicate) checks if the character
is one of the four valid operators in
This function applies
the operator on the numbers and and returns the resulting number to the
calling routine.
This routine first checks if the is empty,
and if it’s, then it prints out an error message on to indicate the error.
Otherwise, it removes and returns the number at the top of to the calling
routine.
If a character inspected in is an equal sign ( ), it calls the routine
to print out the number at the top of the and if the character is ‘ it
calls the routine to empty the If the inspected character is not part
of a number and if it’s not one of the valid operators, prints out an error
message on to indicate the error.
This routine first checks if the is
empty, and if it’s, then it prints out an error message on to indicate the
error. Otherwise, it prints out the top number in on by exactly two digits
after the decimal point.
This routine pops the top element from the
and continue by popping the top element until becomes empty.
This is an interactive calculator, so you can enter numbers (positive or negative) and
operators from the keyboard, and in case of an error, your program needs to print out an
error message on the screen but it doesn’t stop its execution. Execution is terminated
only when you enter from the keyboard.
Put the declarations of all routines that you use in your program in your header file
and the implementation of all routines in your source file At the top of
your source file, insert the following statement:
To compile your source file and link the generated object file with system library
routines, first make a link to in directory: from your
working directory, and then execute:
For a final test of your program, execute: This will test your program
with data files and and generate the output files and
Data files and the correct output files are all in the same directory with
When your program is ready, mail its source and header files to your TA by executing the
following command:

