Click here to Skip to main content
6,630,901 members and growing! (19,414 online)
Email Password   helpLost your password?
Languages » C# » Applications     Intermediate License: The Code Project Open License (CPOL)

MathParser - Math Formula Parser

By Tamer Oz

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.
C# (C# 1.0, C# 2.0, C# 3.0), .NET (.NET 2.0, .NET 3.0, .NET 3.5), Visual Studio (VS2005, VS2008), Dev
Posted:20 Jan 2008
Views:16,770
Bookmarked:34 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
18 votes for this article.
Popularity: 4.23 Rating: 3.37 out of 5
5 votes, 29.4%
1

2
2 votes, 11.8%
3
2 votes, 11.8%
4
8 votes, 47.1%
5

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


Member
Tamer Oz is a Microsoft MVP and works as Assistant Unit Manager.
Occupation: Team Leader
Company: BilgeAdam
Location: Turkey Turkey

Other popular C# articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 11 of 11 (Total in Forum: 11) (Refresh)FirstPrevNext
Generaldispite of correct term -> exception (negative numbers) Pinmemberxparet020913:12 23 Oct '09  
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    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 20 Jan 2008
Editor: Deeksha Shenoy
Copyright 2008 by Tamer Oz
Everything else Copyright © CodeProject, 1999-2009
Web11 | Advertise on the Code Project