Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!uwm.edu!ux1.cso.uiuc.edu!ux1.cso.uiuc.edu!uxe.cso.uiuc.edu!mcdonald From: mcdonald@uxe.cso.uiuc.edu Newsgroups: comp.lang.c Subject: Re: Reading in Functions Message-ID: <225800226@uxe.cso.uiuc.edu> Date: 20 Oct 89 00:40:31 GMT References: <1197@utkcs2.cs.utk.edu> Lines: 43 Nf-ID: #R:utkcs2.cs.utk.edu:1197:uxe.cso.uiuc.edu:225800226:000:2246 Nf-From: uxe.cso.uiuc.edu!mcdonald Oct 19 10:07:00 1989 >In article <1989Oct18.202238.22792@ux1.cso.uiuc.edu> mcdonald@aries.scs.uiuc.edu (Doug McDonald) writes: >>In article <1197@utkcs2.cs.utk.edu> wozniak@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