473,408 Members | 2,734 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,408 software developers and data experts.

Evaluating a mathematical string formula.

Hi guys,

I am building a string in code that is a mathematical formula, based on a
lot of criteria specified by the user. Once the formula is complete, I want
to evaluate it to get a result. For example:

dim strFormula as String

strFormula = "((2 + 2) * 3) / 4) - 5"

Once I have this formula, I want to evaluate it and get the result: -2.

Any tips on how I can do this?

Also, is there a way to check if the formula is valid (i.e. I didn't miss
any parenthesis or anything)? I guess I could try to evaluate it, and stick
it in a try - catch block.

Thanks for your time and help!

Wade
Nov 21 '05 #1
4 10677
Wade,

Only a link

By Peter Bromberg
http://www.eggheadcafe.com/articles/20030908.asp

I hope it helps

Cor
Nov 21 '05 #2
Three basic options:
1. Write your own postfix/prefix based expressions evaluator
2. Use Reflection.Emit to generate a function to evaluate this formula and
then execute that
3. Use the javascript eval function. There was a example posted in response
to a similar query a week or two back in this group on how to do the exact
same thing.

Rgds,
Anand M
VB.NET MVP
http://www.dotnetindia.com

"Wade" wrote:
Hi guys,

I am building a string in code that is a mathematical formula, based on a
lot of criteria specified by the user. Once the formula is complete, I want
to evaluate it to get a result. For example:

dim strFormula as String

strFormula = "((2 + 2) * 3) / 4) - 5"

Once I have this formula, I want to evaluate it and get the result: -2.

Any tips on how I can do this?

Also, is there a way to check if the formula is valid (i.e. I didn't miss
any parenthesis or anything)? I guess I could try to evaluate it, and stick
it in a try - catch block.

Thanks for your time and help!

Wade

Nov 21 '05 #3
The solution from the following website proved to be the perfect solution to
my problem:

http://www.codeproject.com/vb/net/ma..._evaluator.asp

Thanks!

"Anand[MVP]" <An******@discussions.microsoft.com> wrote in message
news:D8**********************************@microsof t.com...
Three basic options:
1. Write your own postfix/prefix based expressions evaluator
2. Use Reflection.Emit to generate a function to evaluate this formula and
then execute that
3. Use the javascript eval function. There was a example posted in response to a similar query a week or two back in this group on how to do the exact
same thing.

Rgds,
Anand M
VB.NET MVP
http://www.dotnetindia.com

"Wade" wrote:
Hi guys,

I am building a string in code that is a mathematical formula, based on a lot of criteria specified by the user. Once the formula is complete, I want to evaluate it to get a result. For example:

dim strFormula as String

strFormula = "((2 + 2) * 3) / 4) - 5"

Once I have this formula, I want to evaluate it and get the result: -2.

Any tips on how I can do this?

Also, is there a way to check if the formula is valid (i.e. I didn't miss any parenthesis or anything)? I guess I could try to evaluate it, and stick it in a try - catch block.

Thanks for your time and help!

Wade

Nov 21 '05 #4
"Wade" <wwegner23NOEMAILhotmail.com> schrieb:
I am building a string in code that is a mathematical formula, based on a
lot of criteria specified by the user. Once the formula is complete, I
want
to evaluate it to get a result. For example:

dim strFormula as String

strFormula = "((2 + 2) * 3) / 4) - 5"

Once I have this formula, I want to evaluate it and get the result: -2.


MathLib
<URL:http://www.palmbytes.de/content/dotnet/mathlib.htm>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #5

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

Similar topics

1
by: Dim | last post by:
Hi, need some help/idea... I have data table in Excel(about 30 columns) and it's getting too big (over 65K) so I need to move it to DB (MS Access is the choice) one of the columns (for example...
5
by: VM | last post by:
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...
1
by: Stefan | last post by:
Is it possible to let an user enter a formula/expression in a text box and let the app evaluate it? /Stefan
1
by: kris Palmby | last post by:
hi, I'm trying to do something which I assume is fairly simple but I jsut cannot seem to do it. I have a string which i am trying to evaluate Mathematically for Example Dim strEval as...
6
by: bugnthecode | last post by:
I'm writing a program to send data over the serial port. I'm using pyserial, and I'm on WindowsXP. When I use literals I can get the data accross how I want it for example: 1 2 3 4...
7
by: david | last post by:
I have searched existing posts and have not found an answer to this variation of an old question. I have the following string stored in a variable Dim str as String = "If 9000 < 10200 Then (6 -...
0
by: humberto.bortolossi | last post by:
Greetings! I would like to know if there is a C library (free or not) that converts a string formula to TeX code. For instance, "(a + b)/2" is converted to "\frac{a + b}{2}". Thanks in...
6
by: eureka2050 | last post by:
Hi, I am a PHP coder, recently ran into a bit of a problem trying to evaluate expressions in PHP. I have an expression which is stored in a string variable and when I try to evaluate it, it...
4
by: Alfredo73 | last post by:
Hi, In the user input of a textbox I would like that the user can input a formula to make it easier for him. So instead of inputing 400 I would like the user to have the possibility to input as...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.