Create a program that will evaluate mathematical expressions like

 

 General
Instructions


Create a program that will evaluate mathematical expressions
like

 23 − 2 ∗ 5

 4 − 9 ∗ 11 + 155 ∗ ( 21 − 17 ) / 3

( ( 15 / ( 7 − ( 1 + 1 ) ) ) − ( 2 + ( 1 + 1 ) )

and determines their value.

Special Instructions

Input File

Your program is given the name of the input text file. The
text file contains mathematical expressions (like the ones above). There is
always only one expression per line. Each valid expression consists of tokens
separated by one or more spaces. A token is either an integer, an operator
(+,-,*,/), or an open or a closed parenthesis.

If the filename is omitted, it is an error. If the filename
is given but it does not exist or cannot be opened for reading by the program,
for any reason, it is an error. The program should display an error message if
any of these conditions occur, and it should be as specific as possible (it is
not enough for it to say “could not open file”). Your program is NOT ALLOWED to
hardcode the input filename in its own code.

 

Output File

Your program is given the name of the output text file. As
your program evaluates each of the expressions in the input file, the results
of computations should be printed to the output time. If a corresponding
expression in the input file is not valid, your program should print INVALID
instead of the result and continue with the next expression. The results should
be printed one per line.

If the filename is omitted from the command line, it is an
error. If the filename is given but it does not exist or cannot be opened for
reading by the program, for any reason, it is an error. The program should
display an error message if any of these conditions occur, and it should be as
specific as possible (it is not enough for it to say “could not open file”). Your
program is NOT ALLOWED to hardcode the input filename in its own code.

 

User Input

This program is not interactive.

 

Program Design

Your program must contain multiple classes and must be
developed in an object oriented way. You will need to develop the following
classes:

 ConsoleCalculator
- the class that provides the main() method. This class is responsible for all input and output
operations (including validation of the input).

ExpressionTools - the class that provides the methods for infix to postfix
conversion and for postfix evaluation.

 

Stack - the class that provides an array based implementation of
the stack we covered in class.

PostFixException - the class that
represent the exception that should be thrown when errors in the expression
occur.

 

The program should read each of the expressions from the
input file, convert it to the postfix expression, evaluate the postfix
expression and print the result to the output file.

 

 

Converting From Infix to Postfix


The algorithm for converting the infix expressions to postfix
expressions is as follows:

If any errors are encountered, your method should throw an
exception of type PostFixException with
an appropriate message.

If any errors are encountered, your method should throw an
exception of type PostFixException with
an appropriate message.

 

 
Powered by