Problem: The majority of the functionality for this assignment remains the same as for program 9, as the calculator will have no new operators. However, where your program gets input and writes output will now depend on the presence or absence of two new command-line arguments. The files to be submitted will be named pA.cpp, rpnA.h and stackA.h. (This is in part to help you distinguish between files as you work since we have not discussed and I have not required you to use version control. Names for classes you have defined for prior assignments should remain consistent with the assignment name, so your class rpn9 should be renamed rpnA, etc.)
New to this program are the command-line arguments -i and -o , which will be used to indicate input and output file names. The command-line arguments -rpn , -i , and -o may appear in any order, but the -i and -o must both be immediately followed by white-space, then the file name to be use for input and output respectively. Both of these arguments must be present, or neither, with cases where only one of them is present in the command line treated as an error condition. Your error message must accurately describe the error. If both arguments are present, the next token after each must be treated as a file name. (Fully qualified names that include the full path, as well as relative paths should work just fine, and need not be tested for.) Note that this means cases such as the following will result in an error, since the -rpn command-line argument will be missed.
pA -i infile.txt -o -rpn
As stated, these two command-line arguments are optional. Thus, if they are not present your program should do as it does now: read from stdin and output to stdout and stderr . If present, your output is more complex. In main() , where you should be handling command-line arguments, output for the usage material should be to stdout , and error messages to stderr , regardless of whether the -i and -o are present. The differences are in the handling of non-error output and input changes according to whether these arguments are present. When not present, you will use stdin , stdout and stderr . When present, input must be from the input file, non- error-output to the output file, while error-output is to stderr.
There are a number of ways to deal with the change in output, but you will need two constructors, one with no parameters that can be called if file I/O is not required, and one with two parameters (for the file objects) when file I/O is required. A suggestion for within the rpnA class is to create two functions, one for input and one for output. These will check whether a pointer to file object is NULL or not, and read or write accordingly. The output function will require a string to be printed as a parameter, and the input function will return a string.
Naming: Your submitted files are to be named p pA.cpp , rpnA.h , and stackA.h.
Output: Output requirements are basically those of assignment 9 as modified above.
Your program's usage material is as follows.
Usage: < program_name>
"< program_name> --help"
display thi i s usage material.
"< program_name> -i < infile> - o < outfile> - rpn" or
"< program_name> -o < < out file> -i < < in file> -rpn" or
"< program_name> -o < < out file> - rpn -i < < in file>" or
"< program_name> - rpn -o < < out file> -i < < in file>" or
"< program_name> - rpn -i < < in file> -o < < out file>" or
"< program_name> -i < < in file> - rpn -o < < out file>"
The program reads from < infile> and writes non- error text
to < outfile>, with input from < infile> conforming to the
description of RPN notation/expressions below.
"< program_name> -rpn"
The program accepts input from standard input as a sequence
of numbers and operators. The numbers (operands, as
integers or floating point numbers) read are push ed on a
stack until needed. When an operator is read, the required
operands are popped from the stack and used to perform the
calculation, with the result placed o n the stack. Valid
operators are +, -, * and /, are interpret ed as addition,
subtraction, multiplication and division, respectively, as
described below. An additional operator is =, which
indicates that the value at the top of the stack is popped
from th e stack and displayed along with the number of
values remaining on the stack, whereupon the program
terminates.
Stack underflows generate an error message and halt the
program, as do a stack overflows. Unrecognized input
tokens produce error messages and result in program
termination, as do unrecognized command line arguments.
The size of the stack is 10.
Stack operations are performed so as to produce results
identically as indicated here.
+ : push(pop() + pop());
- : temp = pop(); push(pop() – – temp;
* : push(pop() * pop());
/ : temp = pop(); push(pop() / temp;
' : push(1.0 / pop());
~ : push(- pop());
** : temp = pop(); push(pow(pop(),temp));
swap : t1 = pop(); t2 = pop(); push(t1); push (t2);
copy : temp = pop(); push(temp); push(temp);
= : pop stack top and display it as the result
with the number of stack element s remaining.
dump : display the contents of the calculator stack.
peek : display the top- of- stack value.
peek("< text>") : display the top-of-stack value following the
specified text.
=< variable> : pop the value from the top of the stack and
Assign that value to the name specified.
:=< constant> : pop the value from the top of the stack and
Assign that value to the name specified if not
Previously defined.
< variable> : push the value of the variable onto the stack.
< constant> : push the value of the constant onto the stack.
Variable and constant n n ames must be alphanumeric, both
upper- and lower- case pe rmitted, and must start with an
alphabetic character.
Upon completing evaluation of an expression read from stdin (using cin ), output to stdout (using cout ) is the same as for previous assignments:
Result = < result popped from top of stack>.
< number of operands on the stack> values remain on the stack.
For all error conditions, print the required output to stderr.
Remember that any item delimited by angle brackets ("< >") is to be replaced with the appropriate information, without the angle brackets.