Instructions:
- Download the form for this project from the
website: Rainbow Calculator
- Write a program completing the four function calculator.
Working from the keypad you have already completed, add code for the
command buttons: Add (+), Subtract (-), Multiply (X), Divide (/) and
Equal (=)
- Your calculator must be able to handle "running
totals". After the equal key is pressed you can then perform another
arithmetic operation using the result of the last one. For example:
Suppose you want to add 5 plus 3 then multiply the result by 7. You
press the "5" key, followed by the "+" key, followed
by the "3" key, followed by the "=" key. This produces
a sum of 8 in the picture box. Now you can press the "X" key
followed by the "7" key followed by the "=" key
to get the result 56 in the picture box. From here you can continue
with any other sequence of operations.
- As you have seen already, you create a number in
the picture box by clicking the keys on the keypad.
- Now you will add the code to perform the arithmetic
operations. Here is the sequence of things that you must do:
- Whenever any one of the arithemetic buttons
(+, -, X, or /) is clicked.
- convert the string held in global variable
num_str to a numeric value and save it in global variable op_one
- set a flag (global variable fun_code) to
remember what operation was performed. (Let fun_code = 1 for
addition, fun_code = 2 for subtraction etc.)
- reset the variable num_str such that it
contains no characters.
- When the equal (=) button is pressed you
will have two values on which to work. The first one is already
converted to a number and stored in global variable op_one. The
second value is still in string form and held in global variable
num_str. To complete the calculation you must do the following:
- check the global variable fun_code to determine
what operation to perform (+, -, X, or /), then perform the
operation saving the answer in a local variable such as result.
The general form for the computation will be:
Let result = op_one + Val(num_str) if the fun_code represents
addition.
- print the answer to the picture box
- A critical final part will be to save the
answer so that it can be used in successive calculations. To
do this convert the result to a string and save it in variable
num_str. e.g. Let num_str = str(result)
|