Click here to Skip to main content
15,881,380 members
Articles / Programming Languages / C#
Article

MathParser - Math Formula Parser

Rate me:
Please Sign up or sign in to vote.
3.82/5 (33 votes)
20 Jan 2008CPOL 132.8K   6.5K   80   24
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.
Image 1

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.

C#
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:

C#
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)


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

Comments and Discussions

 
GeneralThank you ! Pin
kHALEELus128-Apr-14 7:40
kHALEELus128-Apr-14 7:40 
QuestionThank you for ur sharing Pin
yongbam11-Aug-13 21:49
professionalyongbam11-Aug-13 21:49 
QuestionHello Pin
Member 92023892-Jul-12 21:57
Member 92023892-Jul-12 21:57 
AnswerRe: Hello Pin
Member 1002020530-Apr-13 6:41
Member 1002020530-Apr-13 6:41 
QuestionThank you Pin
dwmills27-Oct-11 14:49
dwmills27-Oct-11 14:49 
GeneralBug Pin
Mr.PoorEnglish2-Apr-11 7:16
Mr.PoorEnglish2-Apr-11 7:16 
GeneralRe: Bug Pin
Damir127-Jun-11 22:34
Damir127-Jun-11 22:34 
GeneralRe: Bug Pin
JWillmott1236-Jul-11 3:00
JWillmott1236-Jul-11 3:00 
GeneralTHANKS! Pin
mattan20002-Jan-11 9:46
mattan20002-Jan-11 9:46 
GeneralMy vote of 5 Pin
mattan20002-Jan-11 9:44
mattan20002-Jan-11 9:44 
General1/2 and 1.0/2 Pin
jiming liu26-Dec-10 8:02
jiming liu26-Dec-10 8:02 
Generalthanks Pin
va12310-Sep-10 6:33
va12310-Sep-10 6:33 
Generaldispite of correct term -> exception (negative numbers) Pin
xparet020923-Oct-09 12:12
xparet020923-Oct-09 12:12 
GeneralRe: dispite of correct term -> exception (negative numbers) Pin
JWillmott1236-Jul-11 3:03
JWillmott1236-Jul-11 3:03 
GeneralNOT WORK Pin
dataminers10-Jul-09 7:43
dataminers10-Jul-09 7:43 
GeneralRe: NOT WORK Pin
onurr30-Aug-09 12:50
onurr30-Aug-09 12:50 
GeneralThanks Pin
GoDeep11-May-09 4:09
GoDeep11-May-09 4:09 
Thanks Tamer. This really helped me with a project I was working. Thanks for sharing it.
GeneralThank you Pin
Tomas Hradec21-Jan-09 22:15
Tomas Hradec21-Jan-09 22:15 
GeneralSome Bugs Pin
Guido Tapia16-Jul-08 10:47
Guido Tapia16-Jul-08 10:47 
GeneralRe: Some Bugs Pin
xginox13-Nov-08 14:33
xginox13-Nov-08 14:33 
Generalthe download project doesn't work Pin
big_onion20-Jan-08 17:20
big_onion20-Jan-08 17:20 
GeneralRe: the download project doesn't work Pin
Tamer Oz20-Jan-08 17:32
Tamer Oz20-Jan-08 17:32 
GeneralRe: the download project doesn't work Pin
big_onion20-Jan-08 18:20
big_onion20-Jan-08 18:20 
GeneralAlgorithm used Pin
Imtiaz A20-Jan-08 11:09
Imtiaz A20-Jan-08 11:09 

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

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