fork(1) download
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5.  
  6. typedef double (*funcptr_t)(double);
  7.  
  8. struct FunctionTable {
  9. const char *s;
  10. const funcptr_t f;
  11. };
  12.  
  13. int main() {
  14. const static struct FunctionTable t[] = {{"sin", sin}, {"cos", cos}, {"exp", exp}, {"log", log}, {"tan", tan}, {"sqrt", sqrt}};
  15. char *s;
  16. double d;
  17. while (scanf("%ms%lf", &s, &d) != EOF) {
  18. int size = sizeof(t) / sizeof(struct FunctionTable);
  19. int found = 0;
  20. for (int i = 0; i < size; ++i) {
  21. if (strcmp(t[i].s, s) == 0) {
  22. found = 1;
  23. printf("%a\n", t[i].f(d));
  24. break;
  25. }
  26. }
  27. if (found == 0) {
  28. printf("nan\n");
  29. }
  30. }
  31. //free(fname);
  32. return 0;
  33. }
Success #stdin #stdout 0s 9424KB
stdin
sin 0.5
log 0
log10 10
sqrt 4
cos 0.00001
stdout
0x1.eaee8744b05fp-2
-inf
nan
0x1p+1
0x1.ffffffff920c8p-1