473,320 Members | 2,006 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 formula to mathematical formula

VM
If I have a string variable with a formula:

string sMyformula = "3.3*5.2*5";

How can I convert this to a mathematical formula that the compiler can
calculate? For all purposes, the formula in the string will always be valid.

Thanks

Nov 16 '05 #1
5 26270
A quick and dirty solution is to use the JScript engine:

Microsoft.JScript.Vsa.VsaEngine myEngine =
Microsoft.JScript.Vsa.VsaEngine.CreateEngine();

return Microsoft.JScript.Eval.JScriptEvaluate(
"3.3*5.2*5", myEngine
);

See also http://www.odetocode.com/Code/80.aspx for a similar
technique.

--
Scott

On Mon, 5 Jul 2004 12:45:17 -0400, "VM" <vo******@yahoo.com> wrote:
If I have a string variable with a formula:

string sMyformula = "3.3*5.2*5";

How can I convert this to a mathematical formula that the compiler can
calculate? For all purposes, the formula in the string will always be valid.

Thanks


--
Scott
http://www.OdeToCode.com
Nov 16 '05 #2
VM
Thanks.

Would you know if this also evaluates correct formluas with parenthesis (ie.
"(3.3*(5.2*5))+3" ) ?

Thanks again.

"Scott Allen" <bitmask@[nospam].fred.net> wrote in message
news:kv********************************@4ax.com...
A quick and dirty solution is to use the JScript engine:

Microsoft.JScript.Vsa.VsaEngine myEngine =
Microsoft.JScript.Vsa.VsaEngine.CreateEngine();

return Microsoft.JScript.Eval.JScriptEvaluate(
"3.3*5.2*5", myEngine
);

See also http://www.odetocode.com/Code/80.aspx for a similar
technique.

--
Scott

On Mon, 5 Jul 2004 12:45:17 -0400, "VM" <vo******@yahoo.com> wrote:
If I have a string variable with a formula:

string sMyformula = "3.3*5.2*5";

How can I convert this to a mathematical formula that the compiler can
calculate? For all purposes, the formula in the string will always be valid.
Thanks


--
Scott
http://www.OdeToCode.com

Nov 16 '05 #3
I suggest you write a postfix / prefix converter, then evaluate using Reverse Polish Notation:

http://www.codeproject.com/csharp/RP...sionParser.asp

Don't let it scare you -- it is really quite easy,

Regards,

--
Matthew Cosier
Technology Support
Public Sector

Microsoft Australia Pty. Ltd.
*email:t-******@microsoft.com mob: 0401 932 250 ext:3638
"VM" wrote:
Thanks.

Would you know if this also evaluates correct formluas with parenthesis (ie.
"(3.3*(5.2*5))+3" ) ?

Thanks again.

"Scott Allen" <bitmask@[nospam].fred.net> wrote in message
news:kv********************************@4ax.com...
A quick and dirty solution is to use the JScript engine:

Microsoft.JScript.Vsa.VsaEngine myEngine =
Microsoft.JScript.Vsa.VsaEngine.CreateEngine();

return Microsoft.JScript.Eval.JScriptEvaluate(
"3.3*5.2*5", myEngine
);

See also http://www.odetocode.com/Code/80.aspx for a similar
technique.

--
Scott

On Mon, 5 Jul 2004 12:45:17 -0400, "VM" <vo******@yahoo.com> wrote:
If I have a string variable with a formula:

string sMyformula = "3.3*5.2*5";

How can I convert this to a mathematical formula that the compiler can
calculate? For all purposes, the formula in the string will always be valid.
Thanks


--
Scott
http://www.OdeToCode.com


Nov 16 '05 #4
Yes, I've tried parens and it works.

--s

On Mon, 5 Jul 2004 17:46:46 -0400, "VM" <vo******@yahoo.com> wrote:
Thanks.

Would you know if this also evaluates correct formluas with parenthesis (ie.
"(3.3*(5.2*5))+3" ) ?

Thanks again.


--
Scott
http://www.OdeToCode.com
Nov 16 '05 #5
On 06 Jul 2004 01:02, Matthew Cosier [MFST] wrote:
I suggest you write a postfix / prefix converter, then evaluate using Reverse
Polish Notation:

http://www.codeproject.com/csharp/RP...sionParser.asp

Don't let it scare you -- it is really quite easy,

Regards,


Another approach is to use the GoF Interpreter and State patterns to build
your own evaluator. I have articles about them both at http://www.ghytred.com/ShowArticle.a...rpreterPattern
and http://www.ghytred.com/ShowArticle.aspx?StatePattern which builds a
boolean expression interpreter.
--
Simon Smith
simon dot s at ghytred dot com
www.ghytred.com/NewsLook - NNTP Client for Outlook
Nov 16 '05 #6

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

Similar topics

4
by: Hal Vaughan | last post by:
If I have a byte and I convert it to string (String sData = new String(byte bData), then convert it back (byte bData = sData.getBytes()), will all data be intact, or do Strings have problems with...
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...
4
by: rainmaker1234 | last post by:
Its very simple in VC++. In the followeing code I have declared a String, and a double than I am taking the string and converting it into Double. getch() at the end is only to pause the screen so...
3
by: SiewSa | last post by:
I have come to a situation that I need to use the result of a string variable as a formula for performing calculation. As an example below: Dim X As Integer Dim Y As Integer Dim Result...
1
by: mdawoodk | last post by:
i am getting error "input string was not in correct format" when converting a string decimal into integer value. code is like this: string strVal = ""; int nVal = 0; strVal = "14.9"; nVal...
5
by: jeremito | last post by:
I am extending python with C++ and need some help. I would like to convert a string to a mathematical function and then make this a C++ function. My C++ code would then refer to this function to...
5
by: SMichal | last post by:
Hi, how can I parse string "? 20.000" to double ?
5
by: pankajit09 | last post by:
Hi I want some mechanism to validate a mathematical formula. For eg: (a+b is not valid since the closing bracket is missing. Please help.
7
by: clear1140 | last post by:
Good Day!!! Is it somehow possible to retrieve a string and then convert it into a mathematical expression? for example i have this string '14200 * 5' and then i want to convert this into a...
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
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
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...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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.