Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // TODO: Check return val of scanf()
- #include <stdio.h>
- #include <math.h>
- #include <stdlib.h>
- #define ARRAY_SIZE(x) (sizeof(x)/sizeof (x)[0])
- #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 _curt(double x) { return pow(x, 1.f / 3);}
- double _quit(void) { exit(0); return 0;}
- struct
- {
- const char *description;
- const char *symbol;
- int operands;
- double (*function)();
- } ops[] = {{"sum", "+", 2, _sum} ,{"sub", "-", 2, _sub}
- ,{"mul", "*", 2, _mul} ,{"div", "/", 2, _div}
- ,{"pow", "^", 2, pow} ,{"abs", " ", 1, fabs}
- ,{"sqrt", "√", 1, sqrt},{"curt", "∛", 1, _curt}
- {"exit", "⎋", 0, _quit}
- };
- int main (void)
- {
- int i, c;
- printf("****\n");
- for (i = 0; i < ARRAY_SIZE(ops); i++){
- printf("%d- %s (%s)", i + 1, ops[i].description, ops[i].symbol);
- if (i % 2) printf("\n");
- else printf("\t\t\t\t\t");
- }
- unsigned choice;
- double x,y;
- for (;;){
- printf("\n****\nSelect: ");
- scanf("%u", &choice); choice--;
- CLEAR_STDIN;
- if (choice > i){
- printf ("Invalid; try again.");
- continue;
- }
- switch (ops[choice].operands){
- case 0: // constant or x(void) function
- printf("=%lg", ops[choice].function());
- break;
- case 1: // function with 1 parameter
- printf("Enter Number: ");
- scanf("%lf", &x);
- printf("%s(%lg) = %lg", ops[choice].description, x, ops[choice].function(x));
- break;
- case 2: // function with 2 parameters
- printf("Enter Number 1: ");
- scanf("%lf", &x);
- printf("Enter Number 2: ");
- scanf("%lf", &y);
- printf("%lg %s %lg = %lg", x,ops[choice].symbol, y,ops[choice].function(x,y));
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement