// Program that utilises function pointer to perform mathematical operations. // // Written by YOUR-NAME (YOUR-ZID), DD-MM-YYYY // #include #include double (*get_operation_function(char operation))(double, double); void perform_operation( double first, double second, double (*operation_function)(double, double) ); // DO NOT CHANGE THIS MAIN FUNCTION int main(void) { char operation; double first; double second; double (*operation_function)(double, double); printf("Please enter an operation: "); while (scanf(" %c", &operation) == 1) { operation_function = get_operation_function(operation); if (operation_function == NULL) { printf("Invalid operation '%c'\n", operation); } else { printf("Please enter 2 numbers to perform this operation on: "); scanf("%lf %lf", &first, &second); perform_operation(first, second, operation_function); } printf("Please enter an operation: "); } } // Given two numbers `a` and `b`, return the result of adding them double addition(double a, double b) { return a + b; } // Given two numbers `a` and `b`, return the result of subtracting `b` from `a` double subtraction(double a, double b) { return a - b; } // Given two numbers `a` and `b`, return the result of multiplying them double multiplication(double a, double b) { return a * b; } // Given two numbers `a` and `b`, return the result of dividing `a` by `b` double division(double a, double b) { return a / b; } // Given an `operation`, return a pointer to the function that can perform // that operation. // // E.g. if addition is the operation, a pointer to the `addition` function // should be returned. double (*get_operation_function(char operation))(double, double) { // TODO: COMPLETE THIS FUNCTION & CHANGE THIS RETURN return NULL; } // Performs an operation given various amounts of information. void perform_operation( double first, double second, double (*operation_function)(double, double) ) { // TODO: COMPLETE THIS FUNCTION }