473,320 Members | 1,939 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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!
Jul 19 '05 #1
2 2332
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.Reflection).
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" <pr*******@hotmail.com> wrote in message
news:0b****************************@phx.gbl...
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!

Jul 19 '05 #2
Jonas,

Comments inline.

Ron
"Jonas Prismesen" <pr*******@hotmail.com> wrote in message
news:02****************************@phx.gbl...
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.
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.

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..
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).

Any thoughts??

Best regards
Jonas Prismesen

-----Original Message-----
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.Reflection).
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" <pr*******@hotmail.com> wrote in

message
news:0b****************************@phx.gbl...
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!

.

Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Asbjørn Ulsberg | last post by:
Hi. I'm trying to convert Brady Hegberg's great RTF2HTML VB 6.0 module to C#. I've managed to convert the VB code to VB.NET, which gave me the following code: Option Strict On Option...
9
by: Edward Diener | last post by:
I received no answers about this the first time I posted, so I will try again. My inability to decipher an MSDN topic may find others who have the same inability and someone who can decipher and...
2
by: Jonas Prismesen | last post by:
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...
3
by: Wallace | last post by:
Hai, Can anyone tell how to convert an object into string? Please help me.... urgent.. Thanx in advance...
4
by: gg9h0st | last post by:
i'm a newbie studying php. i was into array part on tutorial and it says i'll get an array having keys that from member variable's name by converting an object to array. i guessed "i can...
9
by: Terry | last post by:
I am converting (attempting) some vb6 code that makes vast use of interfaces. One of the major uses is to be able to split out Read-only access to an obect. Let me give you a simple (contrived)...
3
by: nvx | last post by:
Hi, I'm looking for a simple way to convert a Double to a String exactly the .ToString() does, except these two differences: - the number of decimals is given (rounding is applied if necessary),...
2
by: CoreyWhite | last post by:
Problem: You have numbers in string format, but you need to convert them to a numeric type, such as an int or float. Solution: You can do this with the standard library functions. The...
10
by: Hank Stalica | last post by:
I'm having this weird problem where my code does the following conversion from string to float: 27000000.0 -27000000.00 2973999.99 -29740000.00 2989999.13 -2989999.25 The number on the left...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.