473,597 Members | 2,749 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 10692
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******@discu ssions.microsof t.com> wrote in message
news:D8******** *************** ***********@mic rosoft.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" <wwegner23NOEMA ILhotmail.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.d e/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
2676
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 column1 in DataTable has a string (for example AAA, ABC or BCA, etc) and i also have lookup table in excel with formulas which contains a business rule for one of the columns (column35) on how to calculate: for example for AAA the calculation would...
5
26344
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 the string will always be valid. Thanks
1
1071
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
1497
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 string Dim decEval as Decimal
6
1710
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 5 6 serialport.write('!SC'+'\x01'+'\x05'+'\xFA'+'\x00'+'\r') 1=Get devices attention 2=Select channel on device 3=Rate for movement
7
9084
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 - 5 ) * (10200 - 9000) else 0 End If" How do I evaluate/execute the string as code and return the answer, in this case: 1200?
0
1424
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 advance, Humberto.
6
3927
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 always returns a 'TRUE' Eg: $str = '6.0 >= 8.5' ;
4
1622
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 8*50. Is there a command for vb2008 to calculate the answer of this formula inputed by a string ? Thanks in advance.
0
7969
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8272
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8381
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8035
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8258
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6688
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5847
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3886
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3927
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.