Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- class Calculate
- {
- private:
- float calcResult;
- public:
- float add(float, float);
- float subtract(float, float);
- float multiply(float, float);
- float divide(float, float);
- };
- float Calculate::add(float x, float y)
- {
- calcResult = x + y;
- return calcResult;
- }
- float Calculate::subtract(float x, float y)
- {
- calcResult = x - y;
- return calcResult;
- }
- float Calculate::multiply(float x, float y)
- {
- calcResult = x * y;
- return calcResult;
- }
- float Calculate::divide(float x, float y)
- {
- calcResult = x / y;
- return calcResult;
- }
- int main()
- {
- Calculate calc;
- Calculate *cPtr;
- cPtr = &calc;
- float num1, num2;
- char again;
- int menu;
- do
- {
- cout << "You may choose from any of the following options:\n\n";
- cout << "1. Add\n";
- cout << "2. Subtract\n";
- cout << "3. Multiply\n";
- cout << "4. Divide\n";
- cout << "Please choose one...\n";
- cin >> menu;
- switch(menu)
- {
- case 1:
- cout << "Please enter the first summand:" << endl;
- cin >> num1;
- cout << "Please enter the second summand:" << endl;
- cin >> num2;
- cout << num1 << " plus " << num2 << " equals " << cPtr->add(num1, num2) << endl;
- break;
- case 2:
- cout << "Please enter the minuend:" << endl;
- cin >> num1;
- cout << "Please enter the subtrahend:" << endl;
- cin >> num2;
- cout << num1 << " minus " << num2 << " equals " << cPtr->subtract(num1, num2) << endl;
- break;
- case 3:
- cout << "Please enter the multiplicand:" << endl;
- cin >> num1;
- cout << "Please enter the multiplier:" << endl;
- cin >> num2;
- cout << num1 << " times " << num2 << " equals " << cPtr->multiply(num1, num2) << endl;
- break;
- case 4:
- cout << "Please enter the dividend:" << endl;
- cin >> num1;
- cout << "Please enter the divisor:" << endl;
- cin >> num2;
- if (num2)
- {
- cout << num1 << " divided by " << num2 << " equals " << cPtr->divide(num1, num2) << endl;
- }
- else
- {
- cout << "Can't divide by zero!\n";
- }
- break;
- default:
- cout << "You did not enter 1, 2, 3 or 4!\n";
- }
- cout << "Do you want the program to run again? y/n" << endl;
- cin >> again;
- }
- while (again == 'Y' || again == 'y');
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment