Created
January 4, 2022 16:01
-
-
Save Irwin1985/fb12933503fdab0c8acf89c453106028 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <stdlib.h> | |
| typedef double (*math)(double, double); | |
| double add(double a, double b) { return a + b; } | |
| double sub(double a, double b) { return a - b; } | |
| double mul(double a, double b) { return a * b; } | |
| double divi(double a, double b) { return a / b; } | |
| double operation(double a, double b, math func) { | |
| return func(a, b); | |
| } | |
| int main(int argc, char *argv[]) { | |
| double a, b; | |
| char ope; | |
| printf("Ingresa un número: "); | |
| scanf("%lf", &a); | |
| printf("Ingresa otro número: "); | |
| scanf("%lf", &b); | |
| printf("\nSelecciona una operacion: "); | |
| printf("'a' = Add, 's' = Sub, 'm' = Mul, 'd' = Div\n"); | |
| scanf(" %c", &ope); | |
| math function; | |
| switch (ope) { | |
| case 'a': | |
| function = add; | |
| break; | |
| case 's': | |
| function = sub; | |
| break; | |
| case 'm': | |
| function = mul; | |
| break; | |
| case 'd': | |
| function = divi; | |
| break; | |
| default: | |
| printf("invalid action.\n"); | |
| } | |
| printf("El resultado es: %6.2lf\n", operation(a, b, function)); | |
| return EXIT_SUCCESS; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment