The Unix command expr treats its command line argument as an expression to be evaluated and prints the resulting value. The Unix man page includes the following description. The expr utility will evaluate the expression and write the result to standard output. Arguments are taken as an expression. Terms of the expression must be separated by blanks. Characters special to the shell must be escapedThe Unix command supports a variety of operators with several precedence levels. Your application will support only the operators +, -, x, and d. 'x' is used in place of '*' for multiplication and 'd' in place of '/'for division to avoid conflicts with command interpreter special usage. Standard associativity and precedence apply. Operands must be integer constants and may optionally have a minus sign. .
1) Complete and fully-functional working program(s) in executable form as well as complete source code of all work done. 2) Installation package that will install the software (in ready-to-run condition) on the platform(s) specified in this bid request. 3) Complete ownership and distribution copyrights to all work purchased.
In addition, if the program is run with no parameters, it is to print the message shown in the example below, but with your name rather than mine.
Here are some simple examples.
$java Expr 1 + 2
$java Expr 1 + 2 x 3
$java Expr 1 + 6 d 3 x 2
Java Expr programmed by Roy LevowObserve that the Java interpreter takes the values following the class name and places them in an array of strings that is passed as the argument value for main. You just use those values, you do not have to read anything within your program. The assignmentCode a Java application program using a class named Expr to operate on expressions as described above. Grading considerationsHandling precedence is worth 15 points of the total. Suggestion: Develop the program first without precedence, evaluating operations strictly from left to right and then add precedence handling. A program that does not run at all will get at most 50 points. A program that does not compile will get a zero.
Design hintThe following algorithm is commonly given as an example of the use of a stack in data structures texts, though with more complex expressions.
A simple way to handle precedence is to use two stacks, one for the operators and one for the operands. Start by pushing some distinctive symbol onto the operator stack to mark the beginning of the expression. This symbol is considered to have very low precedence. Then push the first operand onto the operand stack. Then repeat the following process.
1. If the next operator in the input has precedence less than or equal to the one on the top of the operator stack, apply the operator on the top of the stack to the two operands on top of the operand stack and push the result back onto the operand stack. Pop the **operator** off the stack and repeat this step.
2. If the next operator has higher precedence than the one on top of the operator stack, push it onto the operator stack and push the next operand onto the operand stack.
3. When input is complete, repeatedly apply the evaluation process of step 1 until only the beginning-of-expression operand is left on the **operator** stack. The value on the operand stack is the result.
You can use arrays to implement the stacks or you can use the Java Stack class. You can assume that there are no more than 50 operators and 50 operands in an expression.
Error handlingThe program must run correctly on correct data. Behavior of the program on erroneous data is not defined and no special error handling is required
Java and Unix.