home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / lang / c / 19464 < prev    next >
Encoding:
Text File  |  1993-01-09  |  2.4 KB  |  83 lines

  1. Path: sparky!uunet!ontek!mikey
  2. From: mikey@ontek.com (euphausia superba)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: How can you evaluate an arbitrary function?
  5. Message-ID: <2213@ontek.com>
  6. Date: 8 Jan 93 21:43:47 GMT
  7. References: <1993Jan6.202453.8630@news.tufts.edu>
  8. Organization: Ontek Corporation -- Laguna Hills, California
  9. Lines: 72
  10.  
  11. In comp.lang.c, slahiri@jade.tufts.edu (Sandip Lahiri) writes:
  12. | Suppose you have to write a program which would accept any arbitrary 
  13. | function f(x) and calculate its values for x = ... . The program has 
  14. | to be written in C. How would you go about it ? Note, common lisp 
  15. | does have the eval primitive and this task is considerably easier
  16. | if the program is written in lisp. Should you write a parser first to
  17. | check the input ? Remember we can have a function nested to any 
  18. | arbitrary depth e.g. f(x) = tan(tanx + sin(cos(sinx + x^2)) + 5).
  19.  
  20. This is a bit cheeky, but if you'd be allowed to use LISP's eval, I see 
  21. no reason why the following shouldn't be fair play.
  22.  
  23. ---
  24. #include <stdio.h>
  25.  
  26. #define PART1  "#include <stdio.h>\n#include <math.h>\n"
  27. #define PART2  "main(argc, argv) int argc; char * argv;\n"
  28. #define PART3  "{ double x, f();\n  scanf(\"%lg\", &x);\n"
  29. #define PART4  "  while(!feof(stdin)) {\n"
  30. #define PART5  "    printf(\"f(%lg) = %lg\\n\",x,f(x));\n"
  31. #define PART6  "    scanf(\"%lg\",&x);\n  }\n}\n"
  32. #define PART7  "double f(x) double x; \n{ return ("
  33. #define PARTZ  "); }"
  34.  
  35. #define TEMP_SRC  "tempXXtemp.c"
  36. #define TEMP_RUN  "./tempXXtemp"
  37. #define CC_PATH   "cc"
  38.  
  39. #define FROM_X  0.00
  40. #define TO_X    2.05
  41. #define INC_X   0.10
  42.  
  43. main()
  44. {
  45.   char formula[1000];
  46.   double i;
  47.   int status;
  48.   FILE * fs;
  49.  
  50.   printf("please type in a formula in terms of x:\n");
  51.   gets(formula);
  52.  
  53.   fs = fopen(TEMP_SRC, "w");
  54.   fwrite(PART1, 1, strlen(PART1), fs);
  55.   fwrite(PART2, 1, strlen(PART2), fs);
  56.   fwrite(PART3, 1, strlen(PART3), fs);
  57.   fwrite(PART4, 1, strlen(PART4), fs);
  58.   fwrite(PART5, 1, strlen(PART5), fs);
  59.   fwrite(PART6, 1, strlen(PART6), fs);
  60.   fwrite(PART7, 1, strlen(PART7), fs);
  61.   fwrite(formula, 1, strlen(formula), fs);
  62.   fwrite(PARTZ, 1, strlen(PARTZ), fs);
  63.   fclose(fs);
  64.  
  65.   printf("compiling...\n");
  66.   sprintf(formula, "%s %s -o %s -lm", CC_PATH, TEMP_SRC, TEMP_RUN);
  67.   status = system(formula);
  68.   if (status != 0) exit(1);
  69.  
  70.   fs = popen(TEMP_RUN, "w");
  71.   printf("running...\n");
  72.   for (i = FROM_X; i < TO_X; i += INC_X)
  73.   {
  74.     fprintf(fs, "%lg\n", i);
  75.   }
  76.   pclose(fs);
  77.  
  78.   unlink(TEMP_SRC);
  79.   unlink(TEMP_RUN);
  80.  
  81.   return 0;
  82. }
  83.