473,421 Members | 1,579 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,421 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 26319
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.