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. This application will support only the operators +, -, *, /, and parentheses for grouping. Expressions that do not contain parentheses will be evaluated strictly from left to right. Operands must be integer constants and may optionally have a minus sign.
Note that when expressions with the above syntax are typed on the command line, *, (, and ), must be typed with escapes as \*, \(, and \), but the program sees only the single operand character. If you are using a development environment like Visual J++, you do not use the escapes since the arguments are passed directly to the program and are not seen by the command interpreter.
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. The '$' at the beginning of some lines is the prompt symbol.
$ expr 1 + 2The equivalent of the first command for your program would look like
3
$ expr 1 + 2 \* 3
9
$ expr 1 + \( 2 \* 3 \)
7
$ expr
Programmed by Roy Levow
$java Expr 1 + 2Observe 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.
3
Use a recursive function to do the actual evaluation of the expression.
When you come to a left paren, call the function recursively and return
the value of the subexpression when you come to a right paren or the end
of the expression. The main function will print the returned value.
Note that there is a slight technical problem here because a program designed
this way will operate correctly even if some right parens at the end are
omitted.
Last Update: 11 Feb1999 by R.Levow