/////////////////////////////////// // // program_4 project // // created 8/12/21 by tj // rev 0 // /////////////////////////////////// // // Program to provide various calculations w/ functions w/pointers // // Reads in a values for val1 and val2 // Reads in the desired operation // Provides the result // // inputs: 2 values for val1 and val2, a character for the operation // outputs: prints result of val1 operation val2 // /////////////////////////////////// #include "mbed.h" #include // only needed when printing #include // needed for NaN // function prototypes void get_vals(float * value_l, float * value_r); void get_op(char * op); uint8_t check_op(char op); void calc_result(float val_l, float val_r, char oper, float * result); void print_results(float val_l, float val_r, char oper, float result); int main(void){ setbuf(stdout, NULL); // disable buffering when printing // splash printf("\n\nprogram_4\n"); printf("Using Mbed OS version %d.%d.%d\n\n", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION); printf("Welcome to my calculator program (using functions and pointers)\n\n"); float val1; float val2; char operation; uint8_t legal; float answer; // infinte loop while(1){ // ask for and read in values and operation get_vals(&val1, &val2); get_op(&operation); // check if operation is legal legal = check_op(operation); // control printing if(legal){ // perform the calculation calc_result(val1, val2, operation, &answer); // print the eqn and results print_results(val1, val2, operation, answer); } else { printf("illegal operation\n\n"); }// end if }// end while return 0; }// end main void get_vals(float * value_l, float * value_r){ // ask for and read in values printf("Please enter a value for the left side of the operation "); scanf("%f", value_l); printf("Please enter a value for the right side of the operation "); scanf("%f", value_r); return ; }// end get_val void get_op(char * op){ // ask for and read in operation printf("Please enter the operation requested \(+ - * /): "); scanf(" %c", op); return; }// end get_op uint8_t check_op(char op){ // return 1 if legal, 0 if not if(op == '+' || op == '-' || op == '*' || op == '/') return 1; else return 0; }// end check_op void calc_result(float val_l, float val_r, char oper, float * result){ // select the calculation and execute switch(oper){ case '+': *result = val_l + val_r; break; case '-': *result = val_l - val_r; break; case '*': *result = val_l * val_r; break; case '/': *result = val_l / val_r; break; default: *result = 0; break; } // end switch return; }// end calc_results void print_results(float val_l, float val_r, char oper, float result){ printf("%f %c %f = %f\n\n", val_l, oper, val_r, result); return; }// end print_results