Bukz

Simple console calc... u___u

Mar 23rd, 2011
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.90 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class Calculate
  5.   {
  6.     private:
  7.       float calcResult;
  8.     public:
  9.       float add(float, float);
  10.       float subtract(float, float);
  11.       float multiply(float, float);
  12.       float divide(float, float);
  13.   };
  14.  
  15. float Calculate::add(float x, float y)
  16.   {
  17.     calcResult = x + y;
  18.     return calcResult;
  19.   }
  20.  
  21. float Calculate::subtract(float x, float y)
  22.   {
  23.     calcResult = x - y;
  24.     return calcResult;
  25.   }
  26.  
  27. float Calculate::multiply(float x, float y)
  28.   {
  29.     calcResult = x * y;
  30.     return calcResult;
  31.   }
  32.  
  33. float Calculate::divide(float x, float y)
  34.   {
  35.     calcResult = x / y;
  36.     return calcResult;
  37.   }
  38.  
  39. int main()
  40.   {
  41.     Calculate calc;
  42.     Calculate *cPtr;
  43.     cPtr = &calc;
  44.     float num1, num2;
  45.     char again;
  46.     int menu;
  47.    
  48.     do
  49.       {
  50.         cout << "You may choose from any of the following options:\n\n";
  51.         cout << "1. Add\n";
  52.         cout << "2. Subtract\n";
  53.         cout << "3. Multiply\n";
  54.         cout << "4. Divide\n";
  55.         cout << "Please choose one...\n";
  56.         cin >> menu;
  57.        
  58.         switch(menu)
  59.           {
  60.             case 1:
  61.               cout << "Please enter the first summand:" << endl;
  62.               cin >> num1;
  63.               cout << "Please enter the second summand:" << endl;
  64.               cin >> num2;
  65.               cout << num1 << " plus " << num2 << " equals " << cPtr->add(num1, num2) << endl;
  66.               break;
  67.             case 2:
  68.               cout << "Please enter the minuend:" << endl;
  69.               cin >> num1;
  70.               cout << "Please enter the subtrahend:" << endl;
  71.               cin >> num2;
  72.               cout << num1 << " minus " << num2 << " equals " << cPtr->subtract(num1, num2) << endl;
  73.               break;
  74.             case 3:
  75.               cout << "Please enter the multiplicand:" << endl;
  76.               cin >> num1;
  77.               cout << "Please enter the multiplier:" << endl;
  78.               cin >> num2;
  79.               cout << num1 << " times " << num2 << " equals " << cPtr->multiply(num1, num2) << endl;
  80.               break;
  81.             case 4:
  82.               cout << "Please enter the dividend:" << endl;
  83.               cin >> num1;
  84.               cout << "Please enter the divisor:" << endl;
  85.               cin >> num2;
  86.              
  87.               if (num2 == 0)
  88.                 {
  89.                   cout << "Can't divide by zero!\n";
  90.                   break;
  91.                 }
  92.               else
  93.                 {
  94.                   cout << num1 << " divided by " << num2 << " equals " << cPtr->divide(num1, num2) << endl;
  95.                   break;
  96.                 }
  97.             default:
  98.               cout << "You did not enter 1, 2, 3 or 4!\n";
  99.           }
  100.         cout << "Do you want the program to run again? y/n" << endl;
  101.         cin >> again;
  102.       }
  103.     while (again == 'Y' || again == 'y');
  104.    
  105.     return 0;
  106.   }
Advertisement
Add Comment
Please, Sign In to add comment