macrofish

simple calculator

Oct 22nd, 2012
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.85 KB | None | 0 0
  1. #include<iostream>
  2. #include<cmath>
  3. using namespace std;
  4.  
  5. void summation(double result, double x, double y)
  6. {
  7.     result = x+y;
  8.     cout << "result of summation is: " << result << endl;
  9. }
  10.  
  11. void subtraction(double result, double x, double y)
  12. {
  13.     result = x-y;
  14.     cout << "result subtraction is: " << result << endl;
  15. }
  16.  
  17. void multiplication(double result, double x, double y)
  18. {
  19.     result = x*y;
  20.     cout << "result multiplication je: " << result << endl;
  21. }
  22.  
  23. void division(double result, double x, double y)
  24. {
  25.     if (y==0)
  26.     {
  27.         cout << "you cannot divide by zero" << endl;
  28.     }  
  29.     else
  30.     {
  31.         result = x/y;
  32.         cout << "result division je: " << result << endl;
  33.     }
  34. }
  35.  
  36. void power(double result, double x, double y)
  37. {
  38. double p = 1;
  39. if (y==0)
  40.     {
  41.         result = 1;
  42.         cout << "result of powering: " << result << endl;
  43.     }
  44. else if (y<0)
  45.     {
  46.         y=abs(y);
  47.     for (unsigned int i=1;i<=y;i++)
  48.         {
  49.         x=1/x;
  50.         result = x;
  51.         }
  52.         cout << "result of powering: " << result << endl;
  53.     }
  54.  
  55. else
  56.     {
  57.     for (unsigned int i=1;i<=y;i++)
  58.         {
  59.         p=p*x;
  60.         result = p;
  61.         }
  62.     cout << "result of powering: " << result << endl;
  63.     }
  64. }  
  65.  
  66. void faktorial(double result, double x)
  67. {
  68.     if (x==0)
  69.     {
  70.         x=1;
  71.     }
  72.     else
  73.     {
  74.         for (unsigned int i = x-1; i>0; i--)
  75.         {
  76.             x = x * i;
  77.         }
  78.     }
  79.     cout << "faktorial is: " <<x << endl;
  80. }
  81.  
  82. int main ()
  83. {  
  84. double x,y,result = 0;
  85.     char a;
  86.     cout << "insert number, operator and number:" << endl;
  87.     cin >> x >> a;
  88.     if (a !='f')
  89.         {
  90.             cin>> y;
  91.         }
  92.         switch (a)
  93.         {  
  94.         case '+':
  95.             summation(result, x,y);
  96.             break;
  97.         case '-':
  98.             subtraction(result, x, y);
  99.             break;
  100.         case '*':
  101.             multiplication(result, x, y);
  102.             break;
  103.         case '/':
  104.             division(result, x, y);
  105.             break;
  106.         case 'm':
  107.             power(result, x, y);
  108.             break;
  109.         case 'f':
  110.             faktorial(result,x);
  111.             break;
  112.         default:
  113.             cout << "incorrect input" << endl;
  114.  
  115.         }
  116.     return 0;
  117. }
Advertisement
Add Comment
Please, Sign In to add comment