Click here to Skip to main content
Licence CPOL
First Posted 20 Jan 2008
Views 44,465
Downloads 2,008
Bookmarked 63 times

MathParser - Math Formula Parser

By Tamer Oz | 20 Jan 2008
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.
5 votes, 20.0%
1

2
2 votes, 8.0%
3
3 votes, 12.0%
4
15 votes, 60.0%
5
3.61/5 - 26 votes
μ 3.63, σa 2.84 [?]

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 Pinmemberdwmills15:49 27 Oct '11  
GeneralBug PinmemberMr.PoorEnglish8:16 2 Apr '11  
GeneralRe: Bug PinmemberDamir123:34 27 Jun '11  
GeneralRe: Bug PinmemberJWillmott1234:00 6 Jul '11  
GeneralTHANKS! Pinmembermattan200010:46 2 Jan '11  
GeneralMy vote of 5 Pinmembermattan200010:44 2 Jan '11  
General1/2 and 1.0/2 Pinmemberjiming liu9:02 26 Dec '10  
Generalthanks Pinmemberva1237:33 10 Sep '10  
Generaldispite of correct term -> exception (negative numbers) Pinmemberxparet020913:12 23 Oct '09  
GeneralRe: dispite of correct term -> exception (negative numbers) PinmemberJWillmott1234:03 6 Jul '11  
GeneralNOT WORK Pinmemberdataminers8:43 10 Jul '09  
GeneralRe: NOT WORK Pinmemberonurr13:50 30 Aug '09  
GeneralThanks PinmemberGoDeep5:09 11 May '09  
GeneralThank you PinmemberTomas Hradec23:15 21 Jan '09  
GeneralSome Bugs PinmemberMember 285390011:47 16 Jul '08  
GeneralRe: Some Bugs Pinmemberxginox15:33 13 Nov '08  
Generalthe download project doesn't work Pinmemberbig_onion18:20 20 Jan '08  
GeneralRe: the download project doesn't work PinmemberTamer Oz18:32 20 Jan '08  
GeneralRe: the download project doesn't work Pinmemberbig_onion19:20 20 Jan '08  
GeneralAlgorithm used PinmemberImtiaz A12: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
Web04 | 2.5.120206.1 | Last Updated 20 Jan 2008
Article Copyright 2008 by Tamer Oz
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid