Typically when we write arithmetic expressions we use what is called infix notation: For example, we write ``1 + 2'' to indicate that we should use an ADDITION algorithm to add the two parameters 1 and 2. This is infix notation because we place the operator (`+') in between the two parameters (1 and 2).
Prefix notation is an alternative way of writing expressions, where the operator comes before the parameters. Here, we would write ``+ 1 2''. If we wrote ``* + 1 2 3'', this would indicate that we are to multiply two parameters. The first parameter is the sum of 1 and 2, and the second parameter is 3. In infix notation, we would write this as ``(1 + 2) * 3''. Here are some other prefix expressions and their infix equivalents:
prefix notation infix equivalent + - + 1 2 9 8 ((1 + 2) - 9) + 8 / 9 - 7 4 9 / (7 - 4) * + 8 6 - + 1 2 3 (8 + 6) * ((1 + 2) - 3)
(Postfix notation is yet another alternative, where the operator is written after the two parameters: ``1 2 +'' is ``1 + 2'' in infix. But this isn't relevant to this exercise.)
Write a program to evaluate prefix expressions involving addition, subtraction, multiplication, and division. Here is a sample run.
+ 1 2 3.0 / 9 - 7 4 3.0 * + 8 6 - + 1 2 3 0.0(This program does not stop until you abort it by closing the MS-DOS window.) Note that the user consistently separates all numbers and operators by spaces.
To write this program you will want to read strings
using the IOWindow class's readString instance
method.
This method reads a single word at a time.
Suppose the user types ``+ 1 2'' on a line.
The first time you call readString,
the method would return the string ``+'';
the next time it would return the string ``1'';
and the next time it would return the string ``2.''
When you have a String object representing a number, and
you want to convert it into a double, you can use the
Double class's parseDouble method.
Hint: There is a simple way to do this, and there are many more complicated ways. If the simple way isn't obvious, it's probably worthwhile thinking about it for a while before beginning to write the program. It is not necessary to store everything the user types (but of course you may if you like).
Bonus (!) Hint: The simplest way doesn't read in the entire expression and then evaluate it - it reads as it goes. (Ok, maybe that hint is more confusing than helpful.)
Double Bonus (!!) Hint: Think recursion.
This was originally the last exercise.
If you reach this point, you're making lots of progress.
Congratulations!
On to Exercise 6: Breakout, Part I.
I really prefer that you do the above exercise. But if you're running into problems, you can do the following instead. It uses a different kind of trick from the above. The answer turns out to be slightly more complicated.
Write a program to evaluate postfix expressions.
1 2 + = 3 9 7 4 - / = 3 8 6 + 1 2 + 3 - * = 0(This program does not stop until you abort it by closing the window.) Note that the user consistently separates all numbers and operators by spaces.
You'll still want to use the readString and
parseDouble methods described above.