Advertisement
a3f

Untitled

a3f
Dec 30th, 2014
496
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.27 KB | None | 0 0
  1. #include <cstdio>
  2. #include <cmath>
  3. #include <cstdlib>
  4.  
  5. using namespace std;
  6.  
  7. //#define CLEAR_STDIN do {} while ( (c = getchar()) != '\n' && c != EOF )
  8.  
  9. double _sum(double x, double y) { return x + y;}
  10. double _sub(double x, double y) { return x - y;}
  11. double _mul(double x, double y) { return x * y;}
  12. double _div(double x, double y) { return x / y;}
  13. double _rem(double x, double y) { return (long)x % (long)y;} //accuracy loss!
  14.  
  15.  
  16. struct
  17. {
  18.     const char *description;
  19.     char symbol;
  20.     double (*function)(double, double);
  21. } ops[] = {{"sum", '+', _sum}   ,{"sub", '-', _sub}
  22.       ,{"mul", '*', _mul}   ,{"div", '/', _div}
  23.       ,{"rem", '%', _rem}   ,{"pow", '^', pow}
  24. };
  25.  
  26.  
  27. int main ()
  28. {
  29.     int i, c;
  30.     cout << "****\n";
  31.     for (i = 0; i < sizeof ops/sizeof ops[0]; i++){
  32.         cout << i + 1 << "- " << ops[i].description << " ( " << ops[i].symbol<<")";
  33.        
  34.         if (i % 2)  cout << "\n";
  35.         else        cout << "\t\t\t\t\t";
  36.     }
  37.    
  38.     unsigned choice;
  39.     double x,y;
  40.     cout << "\n****\nSelect:  ";
  41.     cin >> choice; choice--;
  42.     //CLEAR_STDIN;
  43.     if (choice > i){
  44.         cout << "Invalid; exiting.";
  45.         return 1;
  46.     }
  47.     // function with 2 parameters
  48.     cout << "Enter Number 1: ";
  49.     cin >> x;
  50.     cout << "Enter Number 2: ";
  51.     cin >> y;
  52.     cout << x << ops[choice].symbol <<  y << " = " << ops[choice].function(x,y);
  53.  
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement