Skip to content

Instantly share code, notes, and snippets.

@Irwin1985
Created January 4, 2022 16:01
Show Gist options
  • Select an option

  • Save Irwin1985/fb12933503fdab0c8acf89c453106028 to your computer and use it in GitHub Desktop.

Select an option

Save Irwin1985/fb12933503fdab0c8acf89c453106028 to your computer and use it in GitHub Desktop.
#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