Click here to Skip to main content
11,719,903 members (84,551 online)
Click here to Skip to main content

MathParser - Math Formula Parser

, 20 Jan 2008 CPOL 84.3K 5.2K 77
Rate this:
Please Sign up or sign in to vote.
MathParser is a class that can calculate the result of mathematic operations given as string such as "3+5/2-3*(1+3)". It can also calculate the result of parametric formulas such as 3A+5B/2C. Calculation is made by operation order(/,*,-,+) and parenthesis rule.

Introduction

MathParser is a class that can calculate the result of mathematic operations given as string such as "3+5/2-3*(1+3)". It can also calculate the result of parametric formulas such as 3A+5B/2C. Calculation is made by operation order(/,*,-,+) and parenthesis rule. In some projects, users want to create a formula dynamically. This is a useful method for calculating the values of these types of formulas.

Background

The basic idea of this code is parsing the string correctly, converting values to decimal and calculating them with the operator.

Using the Code

Using the code is pretty simple.

For basic use, developers should call the calculate method by passing the formula.

MathFunctions.MathParser mp = new MathFunctions.MathParser();
decimal result = mp.Calculate("5/(1-2)+(3+(2/1))");

For parametric use, developers should declare parameters before calling the calculate method, then call the method passing the formula as seen below:

MathFunctions.MathParser mp = new MathFunctions.MathParser();
mp.Parameters.Add(MathFunctions.Parameters.A, 5);
mp.Parameters.Add(MathFunctions.Parameters.B, 2);
mp.Parameters.Add(MathFunctions.Parameters.C, 1);
mp.Parameters.Add(MathFunctions.Parameters.D, 3);
decimal result = mp.Calculate("3D-2B/C+(A-B)");

History

  • 20.01.2007: Initial release
    For bug reports and suggestions, feel free to contact me at oztamer@hotmail.com.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Tamer Oz
Team Leader
Turkey Turkey
Tamer Oz is a Microsoft MVP and works as Assistant Unit Manager.

You may also be interested in...

Comments and Discussions

 
GeneralThank you ! Pin
kHALEELus128-Apr-14 7:40
memberkHALEELus128-Apr-14 7:40 
QuestionThank you for ur sharing Pin
yongbam11-Aug-13 21:49
memberyongbam11-Aug-13 21:49 
QuestionHello Pin
Member 92023892-Jul-12 21:57
memberMember 92023892-Jul-12 21:57 
AnswerRe: Hello Pin
Member 1002020530-Apr-13 6:41
memberMember 1002020530-Apr-13 6:41 
QuestionThank you Pin
dwmills27-Oct-11 14:49
memberdwmills27-Oct-11 14:49 
GeneralBug Pin
Mr.PoorEnglish2-Apr-11 7:16
memberMr.PoorEnglish2-Apr-11 7:16 
GeneralRe: Bug Pin
Damir127-Jun-11 22:34
memberDamir127-Jun-11 22:34 
GeneralRe: Bug Pin
JWillmott1236-Jul-11 3:00
memberJWillmott1236-Jul-11 3:00 
GeneralTHANKS! Pin
mattan20002-Jan-11 9:46
membermattan20002-Jan-11 9:46 
GeneralMy vote of 5 Pin
mattan20002-Jan-11 9:44
membermattan20002-Jan-11 9:44 
General1/2 and 1.0/2 Pin
jiming liu26-Dec-10 8:02
memberjiming liu26-Dec-10 8:02 
Generalthanks Pin
va12310-Sep-10 6:33
memberva12310-Sep-10 6:33 
Generaldispite of correct term -> exception (negative numbers) Pin
xparet020923-Oct-09 12:12
memberxparet020923-Oct-09 12:12 
GeneralRe: dispite of correct term -> exception (negative numbers) Pin
JWillmott1236-Jul-11 3:03
memberJWillmott1236-Jul-11 3:03 
GeneralNOT WORK Pin
dataminers10-Jul-09 7:43
memberdataminers10-Jul-09 7:43 
GeneralRe: NOT WORK Pin
onurr30-Aug-09 12:50
memberonurr30-Aug-09 12:50 
GeneralThanks Pin
GoDeep11-May-09 4:09
memberGoDeep11-May-09 4:09 
GeneralThank you Pin
Tomas Hradec21-Jan-09 22:15
memberTomas Hradec21-Jan-09 22:15 
GeneralSome Bugs Pin
Member 285390016-Jul-08 10:47
memberMember 285390016-Jul-08 10:47 
GeneralRe: Some Bugs Pin
xginox13-Nov-08 14:33
memberxginox13-Nov-08 14:33 
Generalthe download project doesn't work Pin
big_onion20-Jan-08 17:20
memberbig_onion20-Jan-08 17:20 
GeneralRe: the download project doesn't work Pin
Tamer Oz20-Jan-08 17:32
memberTamer Oz20-Jan-08 17:32 
GeneralRe: the download project doesn't work Pin
big_onion20-Jan-08 18:20
memberbig_onion20-Jan-08 18:20 
GeneralAlgorithm used Pin
Imtiaz A20-Jan-08 11:09
memberImtiaz A20-Jan-08 11:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150901.1 | Last Updated 20 Jan 2008
Article Copyright 2008 by Tamer Oz
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid