Converting string to mathematicalexpression

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jonas Prismesen

    #1

    Converting string to mathematicalexpression

    Hi!

    I have string like this:
    string expr = "123/(12*3)";

    And I want to actually calculate the mathematical
    expression in the string. Is there an easy way do it? Or
    do I have to extract all the parts from the string, which
    will be difficult since the expression will change in my
    app..

    I'm using VS.NET 2003 and c#.

    In advance thank you!
  • Ron Bullman

    #2
    Re: Converting string to mathematicalexp ression

    Jonas,

    You have at least three alternatives to proceed:
    A) Create your own parser to parse the expressions to some intermediate
    representation and then compile and execute (or interpret) that. (keywords
    for google, recursive descent parser, abstract stack machine).
    B) If the expressions are valid C# (VB) you could utilize the inbuild
    features of .NET. Create CodeDom compile unit from the expression, compile
    it to assembly and execute that. (namespaces to study System,
    System.CodeDom, System.CodeDom. Compiler, System.Reflecti on).
    C) Incorporate scripting capabilities to your application via VSA (Visual
    Studio for Applications). (namespace to study Microsoft.Vsa and newsgroup
    ....dotnet.vsa) .

    You might wanna specify more detailed manner your requirements inorder to
    have better advice which route to take.


    Ron
    "Jonas Prismesen" <prismejon@hotm ail.com> wrote in message
    news:0bd601c35c c9$c658dc00$a30 1280a@phx.gbl.. .[color=blue]
    > Hi!
    >
    > I have string like this:
    > string expr = "123/(12*3)";
    >
    > And I want to actually calculate the mathematical
    > expression in the string. Is there an easy way do it? Or
    > do I have to extract all the parts from the string, which
    > will be difficult since the expression will change in my
    > app..
    >
    > I'm using VS.NET 2003 and c#.
    >
    > In advance thank you![/color]


    Comment

    • Ron Bullman

      #3
      Re: Converting string to mathematicalexp ression

      Jonas,

      Comments inline.

      Ron
      "Jonas Prismesen" <prismejon@hotm ail.com> wrote in message
      news:02b201c35d 2a$4aedbd90$a50 1280a@phx.gbl.. .[color=blue]
      > Thanks for your reply Ron!
      >
      > What I want to do in my app is have a textbox where the
      > user can enter formulas with a variable (eg. y) like
      > this: y/122 or (y*34)-12+(2*y)
      >
      > Then the user will be able to enter enter a value in
      > another textbox which will replace the y in all the
      > formulas.[/color]

      It seems that you only need to have a simple expression evaluator. Most
      probably someone has implemented it already. Try to google. However if you
      can't find any such you still might want implement your own one.
      [color=blue]
      >
      > I managed the replacing by simply using the replace-
      > method of string. With y=12 this gives me the two
      > string: "12/122" and "(12*34)-12+(2*y)"
      >
      > Now the problem is calculating the expressions. I think
      > creating a parser would be quite complex as the
      > expressions can be very different..[/color]

      Quite the opposite parsing simple mathematical expressions is pretty
      straightforward . The key thing is to have a grammar and use recursive
      descent parser. Don't reinvent the wheel here ;-), there are lot of examples
      how to implement it straightforward manner. (for example
      http://pages.cpsc.ucalgary.ca/~giles...calculator.pdf and
      plenty of similar ones).
      [color=blue]
      >
      > Any thoughts??
      >
      > Best regards
      > Jonas Prismesen
      >
      >[color=green]
      > >-----Original Message-----
      > >Jonas,
      > >
      > >You have at least three alternatives to proceed:
      > >A) Create your own parser to parse the expressions to[/color]
      > some intermediate[color=green]
      > >representati on and then compile and execute (or[/color]
      > interpret) that. (keywords[color=green]
      > >for google, recursive descent parser, abstract stack[/color]
      > machine).[color=green]
      > >B) If the expressions are valid C# (VB) you could[/color]
      > utilize the inbuild[color=green]
      > >features of .NET. Create CodeDom compile unit from the[/color]
      > expression, compile[color=green]
      > >it to assembly and execute that. (namespaces to study[/color]
      > System,[color=green]
      > >System.CodeDom , System.CodeDom. Compiler,[/color]
      > System.Reflecti on).[color=green]
      > >C) Incorporate scripting capabilities to your[/color]
      > application via VSA (Visual[color=green]
      > >Studio for Applications). (namespace to study[/color]
      > Microsoft.Vsa and newsgroup[color=green]
      > >....dotnet.vsa ).
      > >
      > >You might wanna specify more detailed manner your[/color]
      > requirements inorder to[color=green]
      > >have better advice which route to take.
      > >
      > >
      > >Ron
      > >"Jonas Prismesen" <prismejon@hotm ail.com> wrote in[/color]
      > message[color=green]
      > >news:0bd601c35 cc9$c658dc00$a3 01280a@phx.gbl. ..[color=darkred]
      > >> Hi!
      > >>
      > >> I have string like this:
      > >> string expr = "123/(12*3)";
      > >>
      > >> And I want to actually calculate the mathematical
      > >> expression in the string. Is there an easy way do it?[/color][/color]
      > Or[color=green][color=darkred]
      > >> do I have to extract all the parts from the string,[/color][/color]
      > which[color=green][color=darkred]
      > >> will be difficult since the expression will change in[/color][/color]
      > my[color=green][color=darkred]
      > >> app..
      > >>
      > >> I'm using VS.NET 2003 and c#.
      > >>
      > >> In advance thank you![/color]
      > >
      > >
      > >.
      > >[/color][/color]


      Comment

      Working...