// Mar10.cpp : Defines the entry point for the console application. // #include "stdafx.h" void print_m(void); float get_sum(float x, float y); float get_difference(float x, float y); float get_product(float x, float y); float get_quotient(float x, float y); float get_square(float x); float get_cube(float x); int get_factorial(int n); int _tmain(int argc, _TCHAR* argv[]) { int option, continue_key; float x, y; int n, j; char c, cc; do{ print_m(); scanf("%d", &option); switch(option){ case 1: printf("Enter the values of x and y, with a space between them, for sum x + y:\n "); scanf("%f%f", &x, &y); printf("\n\n%f + %f = %f\n\n", x, y, get_sum(x, y)); printf("Press to continue:\n\n"); c = getchar(); while( ( c = getchar() ) != '\n') cc = c; break; case 2: printf("Enter the values of x and y, with a space between them, for difference x + y:\n "); scanf("%f %f", &x, &y); printf("\n\n%f - %f = %f\n\n", x, y, get_difference(x, y)); printf("Press to continue:\n\n"); c = getchar(); while( ( c = getchar() ) != '\n') cc = c; break; case 3: printf("Enter the values of x and y, with a space between them, for product x * y:\n "); scanf("%f %f", &x, &y); printf("\n\n%f * %f = %f\n\n", x, y, get_product(x, y)); printf("Press to continue:\n\n"); c = getchar(); while( ( c = getchar() ) != '\n') cc = c; break; case 4: printf("Enter the values of x and y, with a space between them, for quotient x / y:\n "); scanf("%f %f", &x, &y); if (y == 0) { printf("\n\nYou fool ... you can't divide by 0.\n\n"); for(j = 1; j<=50; j++) printf("\a"); } else printf("\n\n%f / %f = %f\n\n", x, y, get_quotient(x, y)); printf("Press to continue:\n\n"); c = getchar(); while( ( c = getchar() ) != '\n') cc = c; break; case 5: printf("Enter the value of x to square:\n "); scanf("%f", &x); printf("\n\n%f squared = %f\n\n", x, get_square(x)); printf("Press to continue:\n\n"); c = getchar(); while( ( c = getchar() ) != '\n') cc = c; break; case 6: printf("Enter the value of x to cube:\n "); scanf("%f", &x); printf("\n\n%f cubed = %f\n\n", x, get_cube(x)); printf("Press to continue:\n\n"); c = getchar(); while( ( c = getchar() ) != '\n') cc = c; break; case 7: printf("Enter the value of n (must be an integer) to compute n!:\n "); scanf("%d", &n); printf("\n\n%d ! = %d\n\n", n, get_factorial(n)); printf("Press to continue:\n\n"); c = getchar(); while( ( c = getchar() ) != '\n') cc = c; break; case 0: break; default: break; } } while (option != 0); return (0); } void print_m(void) { int i; for(i=1; i<40; i++) printf("\n"); printf("Menu of a lot of math functions...\n\n"); printf("Option Action\n"); printf("------ ------\n"); printf(" 1 Compute Sum: x + y \n"); printf(" 2 Compute Difference: x - y \n"); printf(" 3 Compute Product: x * y \n"); printf(" 4 Compute Quotient: x / y \n"); printf(" 5 Compute x^2 \n"); printf(" 6 Compute x^3 \n"); printf(" 7 Compute n! \n"); printf(" 0 End This Program \n\n\n"); printf("Enter option 1 ... 7, or 0 to STOP\n\n"); return; } float get_sum(float x, float y) { return(x + y); } float get_difference(float x, float y) { return(x - y); } float get_product(float x, float y) { return(x * y); } float get_quotient(float x, float y) { return(x / y); } float get_square(float x) { return(x*x); } float get_cube(float x) { return(x*x*x); } int get_factorial(int n) { int i, fac = 1; if(n == 0) return(1); else if( (n > 12) || (n < 0) ) return(-1); else for(i=1; i<=n; i++) fac *= i; return fac; }