Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

declare math function

delphi declare

  • Please log in to reply
4 replies to this topic

#1 Saeed7007

Saeed7007

    CC Regular

  • Member
  • PipPipPip
  • 43 posts

Posted 30 March 2012 - 01:42 PM

hi
as u know, we can declare a math function inside delphi like:

funcion myfun(x:real):real;
begin
myfun:=x*sin(x)+4-x;
end

if i want user to declare it, what can i do?
thanks
  • 0

#2 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 30 March 2012 - 06:19 PM

You would probably need to write a parser.
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#3 Saeed7007

Saeed7007

    CC Regular

  • Member
  • PipPipPip
  • 43 posts

Posted 31 March 2012 - 12:27 PM

could you give an example.. small one...
thanks.
  • 0

#4 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 31 March 2012 - 05:47 PM

A small example would be several pages long.
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#5 Luthfi

Luthfi

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1320 posts
  • Programming Language:PHP, Delphi/Object Pascal, Pascal, Transact-SQL
  • Learning:C, Java, PHP

Posted 02 April 2012 - 09:44 PM

You can parse the inputted string formula into tokens and store them to a TStrings. Then you translate the tokens (with taking into account their sequence order) into corresponding values in your framework (you might want to use an array of records here). Then with these values you do the real calculation.

Example for what I meant by value in your framework.

type
  TTokenType = (ttUnknown, ttVariable, ttOpenBracket, ttCloseBracket, ttFunction, ttAddOperator, ttSubstractOperator, ttMultiplyOperator);

  PValue = ^TValue;
  TValue=record
    Type_: TTokenType;
    Name: string;              // obtained directly from the formula, if applicable (e.g. for operators this has no meaning).
    Value: Variant;           // the real value, if applicable (e.g. operators of course do not have real value)
  end;

  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download