Java Reference
In-Depth Information
7.
Consider arithmetic expressions like the ones described in the previous project. Allow operands to be either integer values
or variable names that are strings of letters. Design and implement an iterative algorithm that uses a stack to test whether
an expression is legal in Lisp. Write a program that reads potential expressions and demonstrates your algorithm.
Each expression that your program reads can be split across several lines, which is the style used by typical
Lisp programmers. For example, the following expression is legal in Lisp:
(+ (- height)
(* 3 3 4)
(/ 3 width length)
(* radius radius)
)
In contrast, the following expressions are illegal in Lisp:
(+ (-)
(* 3 3 4)
(/ 3 width length)
(* radius radius)
(+ (- height)
(* 3 3 4) )s
(* (/ 3 width length)
(* radius radius)
(+ (- height)
(* 3 3 4)
(/ 3 width length))
(* radius radius)
(+ (- height)
(* 3 3 4)
((/ 3 width length))
(* radius radius)
)
)
)
)
8.
Write a program that graphically displays a working calculator for simple infix expressions that consist of single-
digit operands; the operators + , - , * , and / ; and parentheses. Make the following assumptions:
Unary operators (as in - 2) are illegal.
All operations, including division, are integer operations.
The input expression contains no embedded spaces and no illegal characters, since it is entered by
using a keypad.
The input expression is a syntactically correct infix expression.
Division by zero will not occur. (Consider how you can remove this restriction.)
The calculator has a display and a keypad of 20 keys, which are arranged as follows:
C < Q /
7 8 9 *
4 5 6 -
1 2 3 +
0 ( ) =
As the user presses keys to enter an infix expression, the corresponding characters appear in the display. The C (Clear) key
erases all input entered so far; the < (Backspace) key erases the last character entered. When the user presses the = key, the
expression is evaluated and the result replaces the expression in the display window. The user can then press C and enter
another expression. If the user presses the Q (Quit) key, the calculator ceases operation and is erased from the screen.
A NSWERS TO S ELF -T EST Q UESTIONS
1.
Jill is at the top, and Jim is at the bottom.
2.
a. StackInterface<String> nameStack = new LinkedStack<String>();
while (!stringStack.isEmpty())
nameStack.push(stringStack.pop());
b. stringStack is empty, and nameStack contains the strings that were in stringStack but in reverse order ( Jim
is at the top, and Jill is at the bottom).
 
Search WWH ::




Custom Search