449,364 Members | 1,716 Online
Need help? Post your question and get tips & solutions from a community of 449,364 IT Pros & Developers. It's quick & easy.

# Converting string formula to mathematical formula

 P: n/a 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 Replies

 P: n/a 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" 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 cancalculate? For all purposes, the formula in the string will always be valid.Thanks -- Scott http://www.OdeToCode.com Nov 16 '05 #2

 P: n/a Thanks. Would you know if this also evaluates correct formluas with parenthesis (ie. "(3.3*(5.2*5))+3" ) ? Thanks again. "Scott Allen" 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" 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 cancalculate? For all purposes, the formula in the string will always be valid.Thanks -- Scott http://www.OdeToCode.com Nov 16 '05 #3

 P: n/a 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" 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" 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 cancalculate? For all purposes, the formula in the string will always be valid.Thanks -- Scott http://www.OdeToCode.com Nov 16 '05 #4

 P: n/a Yes, I've tried parens and it works. --s On Mon, 5 Jul 2004 17:46:46 -0400, "VM" 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

 P: n/a On 06 Jul 2004 01:02, Matthew Cosier [MFST] wrote:I suggest you write a postfix / prefix converter, then evaluate using ReversePolish Notation:http://www.codeproject.com/csharp/RP...sionParser.aspDon'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 discussion thread is closed

Replies have been disabled for this discussion.