Package cs2110
Class RpnCalc
java.lang.Object
cs2110.RpnCalc
An interactive calculator for evaluating expressions represented in Reverse Polish Notation.
-
Constructor Summary
ConstructorsConstructorDescriptionRpnCalc()
Create a new calculator object with no variables set and an initial expression of "0" that understands the core math functions defined in `UnaryFunction`. -
Method Summary
Modifier and TypeMethodDescriptionvoid
dispatchCommand
(Scanner scanner) Consume the first token in `scanner`, interpret it as a command name, and execute the appropriate command handler.void
Parse remaining arguments in `scanner` and perform the "clear" command to clear all variable assignments in this calculator instance.void
Parse remaining arguments in `scanner` and perform the "def" command to define a new function equivalent to the current expression.void
Parse remaining arguments in `scanner` and perform the "defs" command to print all function definitions in this calculator instance.void
Perform the "deps" command to print the names of variables that the current expression depends on.void
Perform the "eval" command to evaluate the current expression and print the result.void
Print the commands understood by this calculator, their arguments (in angle brackets, with additional square brackets around optional arguments), and a brief description of the commands' effects.void
Perform the "infix" command to print the current expression in infix notation.void
Perform the "opcount" command to print the number of operations required to evaluate the current expression.void
doOptimize
(Scanner scanner) Perform the "optimize" command to replace the current expression with its optimized form, propagating constant subexpressions based on the current variable assignments.void
Perform the "postifx" command to print the current expression in postfix (RPN) notation.void
Parse remaining arguments in `scanner` and perform the "set" command to assign a value to a variable in this calculator instance.void
doTabulate
(Scanner scanner) Parse remaining arguments in `scanner` and perform the "tabulate" command to evaluate the current expression at a range of abscissa and print each abscissa and ordinate value.void
Parse remaining arguments in `scanner` and perform the "unset" command to unassign a value from a variable in this calculator instance.void
Parse remaining arguments in `scanner` and perform the "vars" command to print all variable assignments in this calculator instance.boolean
Return whether the user has issued an 'exit' command.static void
Run an interactive calculator application.
-
Constructor Details
-
Method Details
-
dispatchCommand
Consume the first token in `scanner`, interpret it as a command name, and execute the appropriate command handler. Do nothing if `scanner` has no tokens. If the command name is not recognized, print an error message. -
exitRequested
public boolean exitRequested()Return whether the user has issued an 'exit' command. -
doHelp
Print the commands understood by this calculator, their arguments (in angle brackets, with additional square brackets around optional arguments), and a brief description of the commands' effects. Arguments passed in `scanner` are currently ignored. -
doSet
Parse remaining arguments in `scanner` and perform the "set" command to assign a value to a variable in this calculator instance. Prints usage to `System.err` and returns if improper arguments are passed. -
doUnset
Parse remaining arguments in `scanner` and perform the "unset" command to unassign a value from a variable in this calculator instance. Prints usage to `System.err` and returns if improper arguments are passed. -
doClear
Parse remaining arguments in `scanner` and perform the "clear" command to clear all variable assignments in this calculator instance. Prints usage to `System.err` and returns if improper arguments are passed. -
doVars
Parse remaining arguments in `scanner` and perform the "vars" command to print all variable assignments in this calculator instance. Prints usage to `System.err` and returns if improper arguments are passed. -
doDefs
Parse remaining arguments in `scanner` and perform the "defs" command to print all function definitions in this calculator instance. Prints usage to `System.err` and returns if improper arguments are passed. -
doEval
Perform the "eval" command to evaluate the current expression and print the result. If arguments are provided in `scanner`, parse them as an RPN expression and update the current expression. Prints to `System.err` and returns if expression cannot be parsed or depends on variables that have not been assigned a value. -
doInfix
Perform the "infix" command to print the current expression in infix notation. If arguments are provided in `scanner`, parse them as an RPN expression and update the current expression. Prints to `System.err` and returns if expression cannot be parsed. -
doPostfix
Perform the "postifx" command to print the current expression in postfix (RPN) notation. If arguments are provided in `scanner`, parse them as an RPN expression and update the current expression. Prints to `System.err` and returns if expression cannot be parsed. -
doDeps
Perform the "deps" command to print the names of variables that the current expression depends on. If arguments are provided in `scanner`, parse them as an RPN expression and update the current expression. Prints to `System.err` and returns if expression cannot be parsed. -
doOpcount
Perform the "opcount" command to print the number of operations required to evaluate the current expression. If arguments are provided in `scanner`, parse them as an RPN expression and update the current expression. Prints to `System.err` and returns if expression cannot be parsed. -
doOptimize
Perform the "optimize" command to replace the current expression with its optimized form, propagating constant subexpressions based on the current variable assignments. If arguments are provided in `scanner`, parse them as an RPN expression and update the current expression. Prints to `System.err` and returns if expression cannot be parsed. -
doTabulate
Parse remaining arguments in `scanner` and perform the "tabulate" command to evaluate the current expression at a range of abscissa and print each abscissa and ordinate value. The required arguments are:- var: The name of the abscissa variable
- lo: The minimum value of the abscissa (floating-point number)
- hi: The maximum value of the abscissa (floating-point number)
- n: The number of abscissa to sample at (integer)
The `n` sampled abscissa are evenly spaced between `lo` and `hi`. After executing this command,`var` should be assigned the value `hi`. Prints to `System.err` and returns if improper arguments are passed, if expression cannot be parsed, or if expression depends on variables that have not been assigned a value.
-
doDef
Parse remaining arguments in `scanner` and perform the "def" command to define a new function equivalent to the current expression. The required arguments are:- name: The name of the function to define
- var: The name of the variable to serve as the function's argument
Prints to `System.err` and returns if improper arguments are passed, if expression cannot be parsed, if expression depends on variables other than var, or if a function named `name` has already been defined (in the latter two cases, the current expression is still updated).
-
main
Run an interactive calculator application. If a program argument is provided, commands are read from a file rather than `System.in`.
-