home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / formu220.zip / test.c < prev    next >
C/C++ Source or Header  |  1995-05-17  |  4KB  |  124 lines

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