Exercise 3: Prefix calculator

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 >>, which reads a string until the first space, will work), compare strings (using ==), and convert strings to numbers. The following fragment of code assigns a double variable x to hold the number represented in a string named token.

x = atof(token.c_str());
(What this does is convert string to a C-style character-array string and then use the built-in C function atof() to convert this to a double.)

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).

Alternative exercise

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 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 >>, which reads a string until the first space, will work), compare strings (using ==), and convert strings to numbers. The following fragment of code assigns a double variable x to hold the number represented in a string named token.

x = atof(token.c_str());
(What this does is convert string to a C-style character-array string and then use the built-in C function atof() to convert this to a double.)

After you finish this exercise, try a project.