[previous_group] * [up] [next] [next_group]

One More Look at the Calculator

The calculator program was a pretty big program - maybe around 600 lines of code! But when it came time to put it all together, it should have gone together pretty quickly. This is the advantage of the modular apporach we took - once we wrote and tested the components of the overall project in all the drivers, we could concentrate on using them to implement the overall algorithm for the project without having to worry about the details of how each module worked.

Suppose we want to improve the calculator?

We can use same modular approach (and the "throw-away" drivers) to concentrate on just the module that needs to be modified.

For example, if we want to be able to accept negative operands, the place to look is the operand module - get_opnd().

The algorithm we might have for get_opnd() is:

     Given:  the first character of the operand
     Return: the next operand typed as an integer

     skip leading white space

     while character is a digit character
          display it
          convert and add it in to the number
          get the next character

     while character is an exponent character
          display it
          convert and add it in to the exponent
          get the next character

     if there was an exponent
          compute the base to exponent

     if character is a space
          display it
          return the result
     otherwise do error handling


To handle negative operands, we would expect a '-' before the first digit of the base. So we could modify the algorithm to be:
     Given:  the first character of the operand
     Return: the next operand typed as an integer

     skip leading white space

     if the character is '-'
          display it
          remember the number is negative

     while character is a digit character
          display it
          convert and add it in to the number
          get the next character

     if the number is supposed to be negative
          negate the number

     while character is an exponent character
          display it
          convert and add it in to the exponent
          get the next character

     if there was an exponent
          compute the base to exponent

     if character is a space
          display it
          return the result
     otherwise do error handling


Similarly, we could add steps to handle float values for the operand. In this case we add code to detect the '.' after the digits, and accumulate the fractional part. We would also need to modify the print results module to print float values.

Or if we wanted negative exponents as well. We could modifiy get_opnd() to detect the '-' exponent character and negate the exponent. We would also need to modify the exponent module to handle negative exponenets, not just pos_power().

Any other suggestions?


[up] to Overview.

[next]