home *** CD-ROM | disk | FTP | other *** search
/ Troubleshooting Netware Systems / CSTRIAL0196.BIN / attach / pcc / v08n03 / math.exe / PARSER21.ZIP / TEST.C < prev    next >
C/C++ Source or Header  |  1994-08-23  |  4KB  |  108 lines

  1. /* TEST.C  Demonstration program for FORMULC -- link with FORMULC.C */
  2. /* Copyright (c) 1994 by Harald Helfgott */
  3.  
  4. /*Copyright (c) 1994 by Harald Helfgott        */
  5. /* This program must be distributed with its corresponding README.DOC */
  6. /* The full copyright and availability notice is in README.DOC          */
  7. /*     This program is provided "as is", without any explicit or */
  8. /* implicit warranty. */
  9.  
  10.  
  11. /* Programmer's Address:
  12.         Harald Helfgott
  13.         MB 1807, Brandeis University
  14.         P.O. Box 9110
  15.         Waltham, MA 02254-9110
  16.            OR
  17.          (during the summer)
  18.         2606 Willett Apt. 427
  19.         Laramie, Wyoming 82070
  20.         seiere@uwyo.edu */
  21.  
  22. #include <stdlib.h>
  23. #include <errno.h>
  24. #include <stdio.h>
  25. #include <ctype.h>
  26. #include "formulc.h"
  27. #include <math.h>
  28. #include <time.h>
  29. int main()
  30. {
  31.  int length,error;
  32.  char source[200];
  33.  UCHAR *code;
  34.  double x,result;
  35.  char answer;
  36.  int i;
  37.  
  38.  puts("FORMULC v2.1 (c) 1994 by Harald Helfgott");
  39.  puts("        Demonstration program");
  40.  puts("");
  41.  puts("    FORMULC is a set of  routines  that  enables  you  to  enter  a");
  42.  puts("mathematical function by keyboard and lets your C or  C++  programs");
  43.  puts("understand it. It is flexible, can run in almost any  computer  and");
  44.  puts("OS, understands many different types of expressions and is extremely");
  45.  puts("fast. The code interpreted by   FORMULC  is  not  much  slower  than");
  46.  puts("compiled C code.");
  47.  puts("         First of all, let's see how much time it takes for FORMULC");
  48.  puts("to calculate the standard normal pdf ( exp(-x*x/2)/sqrt(2*pi) ) at ");
  49.  puts("1000 different points. Press c and ENTER or RETURN to continue.      ");
  50.  getchar();
  51.  code=translate("exp(-x*x/2)/sqrt(2*pi())", "x", &length, &error);
  52.  if(!code) puts("Bug!");
  53.  answer=0;
  54.  for(x=0.0; x<5.0; x += 0.005)
  55.   answer+=f_x_val(code,x);
  56.  puts(" DONE. Now, C compiled code will do it. Press c and ENTER.");
  57.  getchar();
  58.  getchar();
  59.  answer=0;
  60.  for(x=0.0; x<5.0; x+= 0.005)
  61.   answer+=exp(-x*x/2)/sqrt(2*3.14159265358979323846264);
  62.  puts(" DONE. The difference was not too large, was it? Other interpreters");
  63.  puts("are often several times slower than  the  C  compiler! Of course,");
  64.  puts("the slowest process is to change your program every time you want to");
  65.  puts("change a function which should be determined by the user.");
  66.  puts("");
  67.  puts("    This time, you will enter a function and FORMULC will parse it.");
  68.  puts("Multivariate  functions  are supported,  but,  for  the   sake   of");
  69.  puts("simplicity, you should only use x this time. The notation  must  be");
  70.  puts("similar to C's: don't forget the multiplication sign  (*)  and  use");
  71.  puts("exp(x) instead of e(x) . You MAY use ^ (the power operator) as well");
  72.  puts("as pi() (without  parameters).  Uppercase  scientific  notation  is");
  73.  puts("allowed.");
  74.  do {
  75.   printf("\nEnter your function: ");
  76.   scanf("%s",source);
  77.   getchar();
  78.   code=translate(source, "x", &length, &error);
  79.   if(!code)
  80.    printf("Error at character #%d of the function\n",error);
  81.   else
  82.   {
  83.    printf("      The function has been parsed. Now, enter the value of x: ");
  84.    scanf("%lg",&x);
  85.    getchar();
  86.    result=f_x_val(code,x);
  87.    if(errno==ERANGE) puts("Range error.");
  88.    else if(errno==EDOM) puts("Domain error.");
  89.    else printf("The result is %g\n",result);
  90.    puts("    FORMULC will now evaluate your function for 1000 random");
  91.    puts("values of x. Press Enter to continue.");
  92.    getchar();
  93.  
  94.    result=0;
  95.    for(i=0; i<1000; i++)
  96.     result +=
  97.      f_x_val(code,-10.0+20.0*((double) rand())/RAND_MAX);
  98.  
  99.    puts("DONE");
  100.   }
  101.   printf("Do you want to enter another function (y/n) ? ");
  102.   answer=getchar();
  103.  } while(tolower(answer) == 'y');
  104.  puts("Good luck with FORMULC! Read the copyright and availability notice");
  105.  puts("(README.DOC) and the manual (FORMULC.DOC).");
  106.  return(0);
  107. }
  108.