Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #include <cmath>
- #include <cstdlib>
- using namespace std;
- //#define CLEAR_STDIN do {} while ( (c = getchar()) != '\n' && c != EOF )
- double _sum(double x, double y) { return x + y;}
- double _sub(double x, double y) { return x - y;}
- double _mul(double x, double y) { return x * y;}
- double _div(double x, double y) { return x / y;}
- double _rem(double x, double y) { return (long)x % (long)y;} //accuracy loss!
- struct
- {
- const char *description;
- char symbol;
- double (*function)(double, double);
- } ops[] = {{"sum", '+', _sum} ,{"sub", '-', _sub}
- ,{"mul", '*', _mul} ,{"div", '/', _div}
- ,{"rem", '%', _rem} ,{"pow", '^', pow}
- };
- int main ()
- {
- int i, c;
- cout << "****\n";
- for (i = 0; i < sizeof ops/sizeof ops[0]; i++){
- cout << i + 1 << "- " << ops[i].description << " ( " << ops[i].symbol<<")";
- if (i % 2) cout << "\n";
- else cout << "\t\t\t\t\t";
- }
- unsigned choice;
- double x,y;
- cout << "\n****\nSelect: ";
- cin >> choice; choice--;
- //CLEAR_STDIN;
- if (choice > i){
- cout << "Invalid; exiting.";
- return 1;
- }
- // function with 2 parameters
- cout << "Enter Number 1: ";
- cin >> x;
- cout << "Enter Number 2: ";
- cin >> y;
- cout << x << ops[choice].symbol << y << " = " << ops[choice].function(x,y);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement