Click here to Skip to main content
Licence CPOL
First Posted 20 Jan 2008
Views 48,500
Downloads 2,488
Bookmarked 68 times

MathParser - Math Formula Parser

By | 20 Jan 2008 | Article
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)

About the Author

Tamer Oz

Team Leader

Turkey Turkey

Member

Follow on Twitter Follow on Twitter
Tamer Oz is a Microsoft MVP and works as Assistant Unit Manager.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionThank you Pinmemberdwmills14:49 27 Oct '11  
GeneralBug PinmemberMr.PoorEnglish7:16 2 Apr '11  
GeneralRe: Bug PinmemberDamir122:34 27 Jun '11  
GeneralRe: Bug PinmemberJWillmott1233:00 6 Jul '11  
GeneralTHANKS! Pinmembermattan20009:46 2 Jan '11  
GeneralMy vote of 5 Pinmembermattan20009:44 2 Jan '11  
General1/2 and 1.0/2 Pinmemberjiming liu8:02 26 Dec '10  
Generalthanks Pinmemberva1236:33 10 Sep '10  
Generaldispite of correct term -> exception (negative numbers) Pinmemberxparet020912:12 23 Oct '09  
GeneralRe: dispite of correct term -> exception (negative numbers) PinmemberJWillmott1233:03 6 Jul '11  
GeneralNOT WORK Pinmemberdataminers7:43 10 Jul '09  
GeneralRe: NOT WORK Pinmemberonurr12:50 30 Aug '09  
GeneralThanks PinmemberGoDeep4:09 11 May '09  
Thanks Tamer. This really helped me with a project I was working. Thanks for sharing it.
GeneralThank you PinmemberTomas Hradec22:15 21 Jan '09  
GeneralSome Bugs PinmemberMember 285390010:47 16 Jul '08  
GeneralRe: Some Bugs Pinmemberxginox14:33 13 Nov '08  
Generalthe download project doesn't work Pinmemberbig_onion17:20 20 Jan '08  
GeneralRe: the download project doesn't work PinmemberTamer Oz17:32 20 Jan '08  
GeneralRe: the download project doesn't work Pinmemberbig_onion18:20 20 Jan '08  
GeneralAlgorithm used PinmemberImtiaz A11:09 20 Jan '08  

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.

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