wozniak@utkux1.utk.edu (Bryon Lape) (10/19/89)
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. -bryon-
mcdonald@aries.uiuc.edu (Doug McDonald) (10/19/89)
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. > > >-bryon- 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. There is a program called "gnuplot" that does this, and the graphing too. I have written a formula compiler for the 8086 and use that. I also have an interpreter (only) that I could send you. Doug McDonald (mcdonald@uxe.cso.uiuc.edu)
jnh@ecemwl.ncsu.edu (Joseph N. Hall) (10/19/89)
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 ... v v sssss|| joseph hall || 4116 Brewster Drive v v s s || jnh@ecemwl.ncsu.edu (Internet) || Raleigh, NC 27606 v sss || SP Software/CAD Tool Developer, Mac Hacker and Keyboardist -----------|| Disclaimer: NCSU may not share my views, but is welcome to.
usenet@cps3xx.UUCP (Usenet file owner) (10/19/89)
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. Sounds like you have to write an expression evaluation program. Then you could do this: scanf("%s",buffer); tree=parse(buffer); for(x=min;x<=max;x+=step) { y= eval(x,tree); plot(x,y); } You can find out how to turn the expression into a tree in books about compilers (maybe interpreters too). Example: y=x+3*x Tree: add / \ / \ x * / \ / \ 3 x Then the eval() function does an postorder left-then right traversal of tree. This means: float eval(struct tree) { float t1,t2, calc(); t1=eval(tree.left); t2=eval(tree.right); val = calc(t1,t2,tree.function); This function can be a big switch statement to perfrom the appropriate function. return val; } If you only have to evaulate once, you could do the computation while building the tree, instaed of makeing it a second pass. Joe Porkka porkka@frith.egr.msu.edu
mmengel@cuuxb.ATT.COM (Marc W. Mengel) (10/19/89)
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. The only neat, clean, portable way to do this is to write a string expression parser, and interpret the function string for each point on the graph. The parser can be pretty simple; just a recursive descent type deal. >-bryon- -- Marc Mengel mmengel@cuuxb.att.com attmail!mmengel ...!{lll-crg|att}!cuuxb!mmengel
mcdonald@uxe.cso.uiuc.edu (10/20/89)
>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