A 2 - Strange Calculator, due Tues, Apr. 2
in CS 102 on C++, Programming Basics
Write your own strange calculator that meets the following requirements:
- at least five ‘strange’ calculations. Instead of the traditional calculator that adds, subtracts, multiplies, and divides numbers, think creatively about functions that aren’t usually done by a calculator; for example, adding
strings
instead of numbers, performingmodulus
, or calculating the force of an object given its mass and acceleration. - an overloaded function name (that is, at least two functions with the same name)
- generally follow the ‘pretty code’ guidelines
Be prepared to present and explain your code in class.
Examples of strange functions are:
- a function that computes the nth number in the fibonacci sequence
- a function that determines if a given number is part of the fibonacci sequence
- a function that determines if a given number is prime or composite
- a function that determines if a given number is triangular
- a function that determines the time in a different country
- a function that prints the calendar of a month given the first day of the week
Submission Portal
Submit your responses here: https://forms.gle/9ejy5Mxw1Sqph7d37
Example
Below is the calculator we wrote together in class. It’s a calculator, but not a strange calculator (‘cause it does the traditional operations–add, subtract, multiply, divide).
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
int addNumbers(int a, int b) {
int sum = a + b;
return sum;
}
int addNumbers(int c, int d, int f) { // overloading addNumbers() -- two functions have the same name, but different parameters
int sum = c + d + f;
return sum;
}
int multiplyNumbers(int a, int b) {
int product = a * b;
return product;
}
int subtractNumbers(int a, int b) {
int result = a - b;
return result;
}
int divideNumbers(int a, int b) {
if (b != 0) { //cannot divide by 0
cout << "Divide by 0 error." << endl;
return -21312; //you have to return an integer.
} else {
int quotient = a / b;
return quotient;
}
}
int showMenu(){
cout << "Choose which option you'd like to perform: " << endl;
cout << "1. Add 2 integers" << endl;
cout << "2. Add 3 integers" << endl;
cout << "3. Subtract 2 integers" << endl;
cout << "4. Multiply 2 integers" << endl;
cout << "5. Divide 2 integers" << endl;
int choice;
cin >> choice;
return choice;
}
int enterNumber(){
cout << "Enter a number: ";
int num;
cin >> num;
return num;
}
int main(){
cout << "----------------------------" << endl;
cout << " CALCULATOR " << endl;
cout << "----------------------------" << endl;
int x, y, z; //declare 3 variables
int operation = showMenu();
switch(operation){
case 1: // add 2 numbers (x and y)
x = enterNumber();
y = enterNumber();
cout << x << " + " << y << " = " << addNumbers(x, y) << endl;
break;
case 2: // add 3 numbers (x, y, and z)
x = enterNumber();
y = enterNumber();
z = enterNumber();
cout << x << " + " << y << " + " << z << " = " << addNumbers(x, y, z) << endl;
break;
case 3: // subtract 2 numbers
x = enterNumber();
y = enterNumber();
cout << x << " - " << y << " = " << subtractNumbers(x, y) << endl;
break;
case 4: // multiply 2 numbers
x = enterNumber();
y = enterNumber();
cout << x << " * " << y << " = " << multiplyNumbers(x, y) << endl;
break;
case 5: // divide 2 numbers
x = enterNumber();
y = enterNumber();
cout << x << " / " << y << " = " << divideNumbers(x, y) << endl;
break;
default:
cout << "You did not enter a valid menu choice. Try again." << endl;
showMenu();
}
return 0;
/* Example output:
Choose which option you'd like to perform:
1. Add 2 integers
2. Add 3 integers
3. Subtract 2 integers
4. Multiply 2 integers
5. Divide 2 integers
2
Enter a number: 4
Enter a number: 5
Enter a number: 4
4 + 5 + 4 = 13
*/
}