Reading in Functions

mcdonald at uxe.cso.uiuc.edu mcdonald at uxe.cso.uiuc.edu
Fri Oct 20 10:40:31 AEST 1989


>In article <1989Oct18.202238.22792 at ux1.cso.uiuc.edu> mcdonald at aries.scs.uiuc.edu (Doug McDonald) writes:
>>In article <1197 at utkcs2.cs.utk.edu> wozniak at utkux1.utk.edu (Bryon Lape) writes:
>>>
>>>	How does one write a procedure in C so that the user can type in
>>>a formula from the keyboard and the programme will graph it?  I can
>>>handle the graphing part, but what I want to be able to do is is have a
>>>programme that will read in a function and graph the result.

>>Well, you read the formula into a char array and then either 
>>interpret or compile it to get a y value at each x point....

>When you say "compile," what exactly do you mean?  As a sort of quick-
>and-dirty kludge (for UNIX users only) you could read the user's input,
>wrap it in some braces and declarations, write it to a file, cc it,
>link it into the user's program on-the-fly (with ld) and then ... voila
>... plot it.  Like I said, quick and dirty ...

Well, what I actually use is an expression compiler written in C,
as part of my program. This particular one takes single
expressions (of constants and a single variable x), all assumed
to be of type double, in standard C syntax (except that I added
the basic ^ operator as in x^5 for x to the 5th power), using 
/*+- and the standard ANSI C double functions, and converts it to
machine code (for an 8087 or a PDP-11/45 or 70 ). Then I call this
code for various pruposes, including graphing it, but more
importantly I use it repeatedly (tens of thousands of times) for
solutions of integral equations involving the function. 

The compilation is actually done inside my program - not externally
as you describe. This would be impossible for my application, 
which involves giving (or selling!) the program to hundreds of
students - who can't afford to buy full language compilers.
On the AT's they use it is sluggish enough as-is. Running it 
as an interpreter takes several times as long. (Please note that
it compiles to 808*7* code, not 808*6*, as it is float). 
Writing this is really quite easy - I use the compilation logic
that came with a demo program "focal.c" that comes with the Decus
C compiler for the PDP-11.

For making a single graph an interpreter is the likely the way to go.

Doug McDonald



More information about the Comp.lang.c mailing list