Lab 3
Lab assignment: Write a basic Calculator
In this week's lab you must write a C (not C++) program to simulate a simple integer calculator. You will need to know simple input/output, declaring variables, if statements, arithmetic operations and a loop of some kind.
The execution of the finished program should look like what is shown below. (User input is shown in bold, though in your program it will not be bold.)
Welcome to the calculator.
Your current value is 0
Options are:
1. Add
2. Subtract
3. Multiply
4. Divide
9. Exit
Please enter an operation -> 1
Please enter the number to be added -> 4
Your current value is 4
Options are:
1. Add
2. Subtract
3. Multiply
4. Divide
9. Exit
Please enter an operation -> 4
Please enter the number to be divided -> 3
Your current value is 1
Options are:
1. Add
2. Subtract
3. Multiply
4. Divide
9. Exit
Please enter an operation -> 9
Your final value is 1. Exiting program...
Stage 1 (1 point):
Write a calculator which is able to perform any of the 4 operations a single time and then exit. That is, it should ask once what operation to perform on the initial value of 0, and then based on that input perform that operation, print out the result, and then exit the program. All of your code must be inside main(), both for this and subsequent stages. As a starting point use the sample file called lab3.c provided at the bottom of this page. You may also want to look at a sample program called planetWeights.c that illustrates the concepts you need to know.
Stage 2 (1 point):
Set up the calculator with a loop so it behaves as shown above, where after each operation it loops back to the "menu," printing out the current value and presenting the user with options for the next operation. If the user enters a menu option that is not listed, it should simply be ignored and then menu will again be displayed. Use the framework I've given you, and ask if you have any questions.
Stage 3 (Extra Credit) (1 point):
Add one more operation to your calculator: exponentiation, that is the first number to the power of the second number. This should be menu option 5. For example consider the case where the current value stored in the calculator is 3 and the user menu selection is 5 for exponentiation. If the user then enters the number 2, then the result would be 3 to the 2nd power, which is 9. You must do this by setting up a loop to perform the correct number of multiplications. (You may not use the pow method, in case you know what that is.)
Notes:
Keep in mind that this is a team effort so you should agree with your partner on what you are going to do before you start typing. The partner who is typing is the "driver" and the partner watching is the "navigator." Be sure to switch roles every 10 to 15 minutes, to foster a deep understanding of the code for both partners. The navigator should be watching for syntax errors and verifying the correctness of the code you're writing.
It will speed things up for you if you keep a window open for editing and have a separate window open for compiling and running your program. Remember that windows are resizeable!
Submission:
1. You should work with a partner for this (and all the remaining) lab(s). Only one of you need to submit the program to Blackboard, though you should be certain that both of your names be present in a comment at the top of the .c or .cpp source file.
2. You should turn in to Blackboard by the end of the lab (8:50 for the 8-9 lab session, 9:50 for the 9-10 lab session). I know it's tempting to keep working on it, but other classes come in, and it's not fair to the students who are limited to that particular time span if you go over. Which isn't to say that you can't work on it later, to check your solution against the one I post for your own understanding. But what you submit for a grade should be before the next hour begins.