Advertisement
Guest User

Calculator

a guest
Apr 10th, 2010
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.32 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <iomanip>
  4.  
  5. using namespace std;
  6.  
  7. class Calc
  8. {
  9. private:
  10.    double m_nValue2;
  11.    double m_nValue;
  12.  
  13. public:
  14.    Calc(double nValue) { m_nValue = nValue; };
  15.  
  16.    double Add(double nValue) { m_nValue += nValue; return m_nValue;};
  17.    double Subtract(double nValue) { m_nValue -= nValue; return m_nValue;};
  18.    double Multiply(double nValue) { m_nValue *= nValue; return m_nValue;};
  19.    double Divide(double nValue) { m_nValue /= nValue; return m_nValue;};
  20.    double Modulus(double nValue) { m_nValue = fmod(m_nValue,nValue); return m_nValue;};
  21.    double Power(double nValue) { m_nValue = pow(m_nValue,nValue); return m_nValue;};
  22.    double Reciprocal() { m_nValue = 1/m_nValue; return m_nValue;};
  23.    double NthRoot(double index, double guess, double pc)
  24.    {
  25.        double result = guess;
  26.        double result_next;
  27.        do
  28.        {
  29.            result_next = (1/index)*((index-1)*result+(m_nValue)/(pow(result,(index-1))));
  30.            result = result_next;
  31.            pc--;
  32.        }while(pc>1);
  33.        m_nValue = result;
  34.        return m_nValue;
  35.    };
  36.    double Factorial()
  37.    {
  38.        double result = m_nValue;
  39.        double result_next;
  40.        double pc = m_nValue;
  41.        do
  42.        {
  43.            result_next = result*(pc-1);
  44.            result = result_next;
  45.            pc--;
  46.        }while(pc>2);
  47.        m_nValue2 = m_nValue;
  48.        m_nValue = result;
  49.        return m_nValue;
  50.    }
  51.    double Factorial2(int nValue)
  52.    {
  53.        double result = nValue;
  54.        double result_next;
  55.        double pc = nValue;
  56.        do
  57.        {
  58.            result_next = result*(pc-1);
  59.            result = result_next;
  60.            pc--;
  61.        }while(pc>2);
  62.        nValue = result;
  63.        return nValue;
  64.    }
  65.    double DoubleFactorial()
  66.    {
  67.        double result=m_nValue;
  68.        double result_next;
  69.        double pc = m_nValue;
  70.        do
  71.        {
  72.            result_next = result*(pc-2);
  73.            result = result_next;
  74.            pc--;
  75.            pc--;
  76.        }while(pc>2);
  77.        m_nValue2 = m_nValue;
  78.        m_nValue = result;
  79.        return m_nValue;
  80.    }
  81.    double Gamma()
  82.    {
  83.        double result = Factorial()/m_nValue2;
  84.        m_nValue = result;
  85.        return m_nValue;
  86.    }
  87.    double Tetrate(double pc)
  88.    {
  89.        double result = m_nValue;
  90.        double result_next;
  91.        do
  92.        {
  93.             result_next = pow(result,m_nValue);
  94.             result = result_next;
  95.             pc--;
  96.        }while(pc>1);
  97.        m_nValue = result;
  98.        return m_nValue;
  99.    }
  100.    double EvaluateBinomialCoefficient(double nValue)
  101.    {
  102.        double result;
  103.  
  104.        result = (Factorial2(m_nValue))/(Factorial2(nValue)*Factorial2((m_nValue - nValue)));
  105.        m_nValue = result;
  106.        return m_nValue;
  107.    }
  108.    double GetValue() { return m_nValue;};
  109.    double SetValue(double nValue) { m_nValue = nValue;};
  110. };
  111.  
  112. int main()
  113. {
  114.     double start;
  115.     unsigned int operation;
  116.     double operand;
  117.     double operand2;
  118.     double iterations;
  119.     cout<<"What shall be your starting value?";
  120.     cin>>start;
  121.     Calc cCalc(start);
  122.  
  123.     do
  124.     {
  125.         cout<<"Select an operation, please.\n\n1. Addition\n2. Subtraction\n3. Multiplication\n4. Division\n5. Modulus\n6. Exponentiation\n7. Tetration\n8. Reciprocal\n9. Nth Root\n10. Factorial\n11. Double Factorial\n12. Gamma Function\n13. Evaluate Binomal Coefficient\n14. Set Value\n15. Show Value\n\n";
  126.         cin>>operation;
  127.         cin.ignore();
  128.  
  129.         cout<<setprecision(15);
  130.  
  131.         switch(operation)
  132.         {
  133.                          case 1:
  134.                             cout<<"Select an addend, please.\n";
  135.                             cin>>operand;
  136.                             cin.ignore();
  137.                               cCalc.Add(operand);
  138.                               cout<<cCalc.GetValue()<<"\n";
  139.                               break;
  140.                          case 2:
  141.                             cout<<"Select an minuend, please.\n";
  142.                             cin>>operand;
  143.                             cin.ignore();
  144.                               cCalc.Subtract(operand);
  145.                               cout<<cCalc.GetValue()<<"\n";
  146.                               break;
  147.                          case 3:
  148.                             cout<<"Select an multiplicand, please.\n";
  149.                             cin>>operand;
  150.                             cin.ignore();
  151.                               cCalc.Multiply(operand);
  152.                               cout<<cCalc.GetValue()<<"\n";
  153.                               break;
  154.                          case 4:
  155.                             cout<<"Select a divisor, please.\n";
  156.                             cin>>operand;
  157.                             cin.ignore();
  158.                               cCalc.Divide(operand);
  159.                               cout<<cCalc.GetValue()<<"\n";
  160.                               break;
  161.                         case 5:
  162.                             cout<<"Select a divisor, please.\n";
  163.                             cin>>operand;
  164.                             cin.ignore();
  165.                               cCalc.Modulus(operand);
  166.                               cout<<cCalc.GetValue()<<"\n";
  167.                               break;
  168.                          case 6:
  169.                             cout<<"Select a exponent, please.\n";
  170.                             cin>>operand;
  171.                             cin.ignore();
  172.                             cCalc.Power(operand);
  173.                             cout<<cCalc.GetValue()<<"\n";
  174.                             break;
  175.                         case 7:
  176.                             cout<<"Select a number to tetrate to, please.\n";
  177.                             cin>>operand;
  178.                             cin.ignore();
  179.                             cCalc.Tetrate(operand);
  180.                             cout<<cCalc.GetValue()<<"\n";
  181.                             break;
  182.                         case 8:
  183.                             cout<<"Select a number to take the reciprocal of, please.\n";
  184.                             cin>>operand;
  185.                             cin.ignore();
  186.                             cCalc.Reciprocal();
  187.                             cout<<cCalc.GetValue()<<"\n";
  188.                             break;
  189.                         case 9:
  190.                             cout<<"Select an index, please.";
  191.                             cin>>operand;
  192.                             cin.ignore();
  193.                             cout<<"Select a guess for the root, please.";
  194.                             cin>>operand2;
  195.                             cin.ignore();
  196.                             cout<<"How many iterations of the algorithm?\n";
  197.                             cin>>iterations;
  198.                             cin.ignore();
  199.                             cCalc.NthRoot(operand,operand2,iterations);
  200.                             cout<<cCalc.GetValue()<<"\n";
  201.                             break;
  202.                         case 10:
  203.                             cCalc.Factorial();
  204.                             cout<<cCalc.GetValue()<<"\n";
  205.                             break;
  206.                         case 11:
  207.                             cCalc.DoubleFactorial();
  208.                             cout<<cCalc.GetValue()<<"\n";
  209.                             break;
  210.                         case 12:
  211.                             cCalc.Gamma();
  212.                             cout<<cCalc.GetValue()<<"\n";
  213.                             break;
  214.                         case 13:
  215.                             cout<<"Choose an operand, please.";
  216.                             cin>>operand;
  217.                             cCalc.EvaluateBinomialCoefficient(operand);
  218.                             cout<<cCalc.GetValue()<<"\n";
  219.                             break;
  220.                         case 14:
  221.                             cout<<"Which number would you like to set the calculator to?\n";
  222.                             cin>>operand;
  223.                             cin.ignore();
  224.                             cCalc.SetValue(operand);
  225.                             cout<<cCalc.GetValue()<<"\n";
  226.                             break;
  227.                         case 15:
  228.                             cout<<cCalc.GetValue()<<"\n";
  229.                             break;
  230.                         default:
  231.                             cout<<"Sorry, but that isn't a feature yet.";
  232.                             break;
  233.         }
  234.     }while(1==1);
  235. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement