/************************************* ** proj3.c ** ** Dennis L. Frey ** 11/20/98 ** ** This program simulates a simple calculator ** ** Possible input operations are ** + add ** - subtract ** * multiply ** / divide ** ^ exponentiation ** V or v absolute value ** R or r square root ** C or c clear running total ** E of e Exit ** ** Note that the main program does only "basic" or ** "high level" stuff. The details are hidden in ** the functions ** ******************************************/ #include #include /* function prototypes ** these functions take the current total as the parameter ** and return the updated total */ double add (double a); double subtract (double a); double multiply (double a); double divide (double a); double exponent (double a); main () { double total = 0; /* the running total */ int value; char command = ' '; /* holds user input -- initialized */ char dummyc; /* to accept the NEWLINES */ int i; printf ("starting total is %0.2f\n\n", total); while (command != 'E' && command != 'e') { printf ("Input operation: "); command = getchar(); /* the real command character */ dummyc = getchar(); /* eat the NewLine */ switch (command) { case '+': /* addition */ total = add (total); break; case '-': /* subtraction */ total = subtract (total); break; case '*': /* multiplication */ total = multiply (total); break; case '/': /* division */ total = divide (total); break; case 'C': case 'c': /* clear total back to 0 */ printf ("clearing the total\n"); total = 0; break; case 'E': case 'e': /* exit */ printf ("Exiting\n"); break; case 'R': /* square root */ case 'r': if (total < 0) { printf ("Can't take sqrt of negative number\n"); } else { printf ("Taking Square Root of %0.2f\n", total); total = sqrt (total); } break; case '^': /* raise total to power */ total = exponent (total); break; case 'V': case 'v': /* take the absolute value of total */ printf ("Getting absolute value of %0.2f\n", total); total = fabs(total); break; default: /* error for any other command */ printf ("invalid command: %c\n", command); break; } printf ("subtotal = %0.2f\n\n", total); } printf ("total is %0.2f\n", total); } /************************************************ ** function add ** gets value from user and adds to parameter ** returns new total to caller ****************************************************/ double add (double a) { double value; printf ("Input value to add: "); scanf ("%lf", &value); /* eat the NEWLINE after the value */ getchar(); printf ("adding %0.2f to %0.2f\n", value, a); return value + a; } /************************************************ ** function subtract ** gets value from user and subtracts it from parameter ** returns new total to caller ****************************************************/ double subtract (double a) { double value; printf ("Input value to subtract: "); scanf ("%lf", &value); /* number to subtract */ getchar(); /* eat the NEWLINE */ printf ("subtracting %0.2f from %0.2f\n", value, a); return a - value; } /************************************************ ** function multiply ** gets value from user and multiplies times the parameter ** returns new total to caller ****************************************************/ double multiply (double a) { double value; printf ("Input multiplier: "); scanf ("%lf", &value); getchar(); /* eat the NEWLINE */ printf ("multiplying %0.2f by %0.2f\n", a, value); return a * value; } /************************************************ ** function divide ** gets value from user and divides parameter by it ** returns new total to caller ** ** checks for divide by 0 ****************************************************/ double divide (double a) { double value; do { printf ("Input divisor: "); scanf ("%lf", &value); getchar(); /* eat the NEWLINE */ if (value == 0) { printf ("Can't divide by 0\n"); } } while (value == 0); printf ("dividing %0.2f by %0.2f\n", a, value); return a / value; } /************************************************ ** function exponent ** gets value from user and raises parameter to ** that power using pow function from math library ** returns new total to caller ****************************************************/ double exponent (double a) { double value; printf ("Input exponent: "); scanf ("%lf", &value); getchar(); /* eat the NEWLINE */ return pow (a, value); }