|
mikrom (Programmer) |
18 Oct 11 15:36 |
this works CODE#include <stdio.h> #include <stdlib.h>
void nacitaj(double *x, double *y) { scanf("%lf %lf", x, y); }
double obsah(double x, double y) { return (x*y); }
double obvod(double x, double y) { return (2*x+2*y); }
double (*p_f)(double x, double y);
int main() { char c; double *x,*y;
x=(double *) malloc(1*sizeof(double)); y=(double *) malloc(1*sizeof(double)); nacitaj(x, y); printf("*x = %lf, *y = %lf\n", *x, *y);
printf("Which operation (o, s)?: "); // in gcc (MinGW) we have to use _flushall() or fflush(stdin) // before getchar() _flushall(); c=getchar(); printf("operation = '%c'\n", c); if (c=='o' || c=='O') p_f=obsah; else if (c=='S' || c=='s') p_f=obvod; else { printf("fail\n"); return 1; } printf("%.3lf\n", p_f(*x,*y));
free(x); free(y);
return 0; } Output: CODE3.5 4.75 *x = 3.500000, *y = 4.750000 Which operation (o, s)?: o operation = 'o' 16.625 You can do it a little bit simpler: CODE#include <stdio.h> #include <stdlib.h>
void nacitaj(double *x, double *y) { scanf("%lf %lf", x, y); }
double obsah(double x, double y) { return (x*y); }
double obvod(double x, double y) { return (2*x+2*y); }
double (*p_f)(double x, double y);
int main() { char c; double x, y;
nacitaj(&x, &y); printf("x = %lf, y = %lf\n", x, y);
printf("Which operation (o, s)?: "); // in gcc (MinGW) we have to use _flushall() or fflush(stdin) // before getchar() fflush(stdin); c=getchar(); printf("operation = '%c'\n", c); if (c=='o' || c=='O') p_f=obsah; else if (c=='S' || c=='s') p_f=obvod; else { printf("fail\n"); return 1; } printf("%.3lf\n", p_f(x, y));
return 0; } Output: CODE3.5 4.75 x = 3.500000, y = 4.750000 Which operation (o, s)?: o operation = 'o' 16.625 Tested with gcc (MinGW) |
|