Board index » delphi » Input an equation

Input an equation

Could somebody kindly help me.

I would like to readln an equation, for example: 5*9-2/3, or whatever,
including reserved pascal words for example: "sqr","sqrt","cos","sin", ect.
Then I would like to use it in my program in calculating the answer.

If there is a(n) (easy) way of doing this, please let me know...

Thanks in advance,
Anthony la Grange, South Africa

 

Re:Input an equation


Somehow I don't think there is an easy way to do this.
You'll have to make up some sort of parser to handle the expression,
check out www.iecc.com/compilers/crenshaw/
that is a tutorial on compiler construction, but it has several
pages about expression parsing.
Good luck.

Re:Input an equation


Quote
ya...@ravine.binary.net wrote:

> Somehow I don't think there is an easy way to do this.
> You'll have to make up some sort of parser to handle the expression,
> check out www.iecc.com/compilers/crenshaw/
> that is a tutorial on compiler construction, but it has several
> pages about expression parsing.
> Good luck.

No, it is not easy to write it. But it is easy to find
a parser (formula expression evaluator) on the TP-links site
in the numerics chapter and in the sources chapter.

Look for Pedt Scragg's parser and some more. Even a very old
TP 3 example is there, the MicroCalc spreadsheet program,
which has such a parser.

http://www.geocities.com/SiliconValley/2926/tp.html

Franz Glaser

Re:Input an equation


 27) How to evaluate a function given as a string to the program?

 149061 Jul 19 1998 ftp://garbo.uwasa.fi/pc/link/tsfaqp.zip
 tsfaqp.zip Common Turbo Pascal Questions and Timo's answers, linked

   All the best, Timo

....................................................................
Prof. Timo Salmi   Co-moderator of news:comp.archives.msdos.announce
Moderating at ftp:// & http://garbo.uwasa.fi/ archives 193.166.120.5
Department of Accounting and Business Finance  ; University of Vaasa
mailto:t...@uwasa.fi <http://www.uwasa.fi/~ts/>  ; FIN-65101,  Finland

Spam foiling in effect.  My email filter autoresponder will return a
required email password to users not yet in the privileges database.

Re:Input an equation


In article <6pvcn0$lf...@news2.saix.net>,
  "AM la Grange" <alagra...@intekom.co.za> wrote:

Quote
> I would like to readln an equation, for example: 5*9-2/3, or whatever,
> including reserved pascal words for example: "sqr","sqrt","cos","sin", ect.
> Then I would like to use it in my program in calculating the answer.

If you are using a compiler which can use ActiveX DLL components
(such as Delphi), then download a copy of UCalc Fast Math Parser
from http://www.ucalc.com/dll .

Daniel Corbier, UCALC
http://www.ucalc.com

-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/rg_mkgrp.xp   Create Your Own Free Member Forum

Re:Input an equation


JRS:  In article <6pvcn0$lf...@news2.saix.net> of Sat, 1 Aug 1998
17:36:10 in comp.lang.pascal.borland, AM la Grange

Quote
<alagra...@intekom.co.za> wrote:
>I would like to readln an equation, for example: 5*9-2/3, or whatever,
>including reserved pascal words for example: "sqr","sqrt","cos","sin", ect.
>Then I would like to use it in my program in calculating the answer.

Those words are not reserved; they are merely the names of library
routines.

If you have BP7, there is the possibility of reading the line(s),
writing a file with suitable head and tail and your lines in between,
then using EXEC on BPC to compile the file into a DLL, then loading the
DLL dynamically.  It /should/ work; and, once it works at all, it gives
immense expressive power.

Alternatively, http://www.merlyn.demon.co.uk/programs/longcalc.pas,
which is still in development, already can read in expressions in RPN
and evaluate them.  Transcendentals are not included : it is a long
integer program, capable of thousand-digit arithmetic.  By entering #f9
one executes what is in effect a pre-written RPM program.  You could
alter the storage from kilodigits to extended and add new routines.

NOTE : In it, #uf, #sd, #ds, whilst plausible, are not yet correct.

--
John Stockton, Surrey, UK.    j...@merlyn.demon.co.uk    Turnpike v1.12    MIME.
  Web <URL:http://www.merlyn.demon.co.uk/> -- FAQqish topics, acronyms & links.
  PAS & EXE in <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.txt.
  Do not Mail News to me.    Before a reply, quote with ">" or "> " (SoRFC1036)

Re:Input an equation


In article <35c8c2af.5474...@nntp.ftc-i.net>, Higgi...@ftc-i.SpAmZaP.net
writes

Quote
>On 1 Aug 1998 16:00:42 GMT, ya...@ravine.binary.net wrote:

>>Somehow I don't think there is an easy way to do this.
>>You'll have to make up some sort of parser to handle the expression,
>>check out www.iecc.com/compilers/crenshaw/
>>that is a tutorial on compiler construction, but it has several
>>pages about expression parsing.
>>Good luck.

>Actually if the problem can be reduced to entering only single digit
>numbers 0 - 9, using postfix notation, and substituting single
>character tokens for operators like "sqr," "sqrt," etc., you can code
>the whole thing in 3 printed pages including generous multiline
>comments and white space between routines.

IMHO, it requires users of the software to remember the tokens, far
easier to allow the standard operators such as sqrt etc.
Quote

>I wouldn't care to guess what it would take to handle reals as input,
>use algebraic or parenthesis notation, and handle operators like
>"sqrt" spelled out rather than single character tokens.

You just need to set the whole thing up to be recursive and thus parse
the input line sequentially. Sqrt and suchlike just need to have their
arguments parsed, parantheses are a bit harder to work with but can be
dealt with. Reals can be dealt with by looking for the decimal point.

One thing to remember with this is that you should look for "sqrt"
before "sqr" or untoward effects happen with your calculation.

There's an example of a parser that will do this available via
http://www.pedt.demon.co.uk/

--
Pedt Scragg                    <newsmas...@pedt.demon.co.uk>

Never curse the Crocodile's mother before crossing the river

Re:Input an equation


Re:Input an equation


Quote
AM la Grange wrote:

> I would like to readln an equation, for example: 5*9-2/3,
> or whatever, including reserved pascal words for example:
> "sqr","sqrt","cos","sin", etc.

> Then I would like to use it in my program in calculating the answer.

Please compile my file MATH.PAS I wrote several years ago:

[Expression-Compiler (OOP/TP55-TP70)]
<http://home.t-online.de/home/Antivivisektion/math.pas>

You may use it like in my example below:

|  Uses Math;
|  Var E: Expression; Term: String; Result: Float;
|  Begin
|    Write('Enter expression: ');
|    ReadLn(Term);
|    Result := E.EvaluateTerm(Term);
|    Write('Result: ',Result:0:20);
|  End.

Quote
> If there is a(n) (easy) way of doing this, please let me know...

It's really that easy ;-)

You may also use one-char variables to evaluate complex expressions:

|  Uses Math;
|  Var E: Expression; X: Word;
|  Begin
|    E.Init;
|    E.Parse := '2*X+SIN(X)*TAN(X/8)';
|    E.FormatTerm;
|    E.Compile;
|    For X := 1 To 100 Do
|    Begin
|      E.Data['X'] := X;
|      E.PrintResult; { same as WriteLn(E.FormFloat(E.Evaluate)); }
|    End;
|  End.

The expression may be as complex as you wish,
with 25 build-in var's A-Z (like in basic).

It's even possible to add your own functions at runtime, e.g. RANDOM:

|  Uses Math;
|  Var E: Expression; X: Word;
|  
|  {$F+} Function MyFunc (x: Float): Float; {$F-}
|  Begin
|    MyFunc := Random(Round(x));
|  End;
|  
|  Begin
|    E.Init; { Insert ADDFUNC after this line }
|    E.AddFunc('RANDOM',MyFunc);
|    { Now you can use RANDOM(...) in your expressions, example: }
|    E.Parse := 'RANDOM(100)';
|    E.FormatTerm;
|    E.Compile;
|    E.PrintResult;
|  End.

The error-handling (simple HALT) has to be worked out...

--
              ---===Coders Against Vivisection===---

A.E.Neumann fr die Antivivisektion e.V., PO-Box 201, D-53569 Unkel
mailto:Antivivisekt...@t-online.de http://Antivivisektion.base.org/

[Animal research is wasteful and misleading]
<http://www.werple.net.au/~antiviv/issue12.htm>

Re:Input an equation


Re:Input an equation


AM la Grange (alagra...@intekom.co.za) wrote:

Quote
> Could somebody kindly help me.

> I would like to readln an equation, for example: 5*9-2/3, or whatever,
> including reserved pascal words for example: "sqr","sqrt","cos","sin", ect.
> Then I would like to use it in my program in calculating the answer.

> If there is a(n) (easy) way of doing this, please let me know...

You need to write your own parser.   About 10 years ago I wrote
a program that did exactlyt what you need.  If there's an interest
I cab post the source sometimes.   Having fully parenthesized
expressions make the parser somewhat simpler.

Re:Input an equation


If I can find it, quite a long time ago I wrote a pascal program for doing
automatic differentiation. It worked quite well, and included a parser just
like the one you seem to need. However, it is probably true to say that the
bulk of the program is a bit difficult to understand, as it includes the
differentiation part, and a reducer/factorer (which never got finished to
my satisfaction). Hardly surprising - this is a very hard problem, much
harder than the differentiator!

Interested? (I hope to goodness I've still got it somewhere!)

Re:Input an equation


Hi there...

I only arrived home just now (I'm at boarding school). But yes, please,
could you post your source code for me to work from... It would really be a
great help. If not, you could simply e-mail it to me.

Thanks so much...

A la Grange, Aliwal North, South Africa

Quote
Virtual wrote in message <35c6606...@news.pacifier.com>...
>AM la Grange (alagra...@intekom.co.za) wrote:
>> Could somebody kindly help me.

>> I would like to readln an equation, for example: 5*9-2/3, or whatever,
>> including reserved pascal words for example: "sqr","sqrt","cos","sin",
ect.
>> Then I would like to use it in my program in calculating the answer.

>> If there is a(n) (easy) way of doing this, please let me know...

>You need to write your own parser.   About 10 years ago I wrote
>a program that did exactlyt what you need.  If there's an interest
>I cab post the source sometimes.   Having fully parenthesized
>expressions make the parser somewhat simpler.

Other Threads