5,446,542 members and growing! (18,627 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, C#), .NET (.NET, .NET 3.5, .NET 3.0, .NET 2.0), Visual Studio (VS2005, VS2008, Visual Studio), Dev

Posted: 20 Jan 2008
Updated: 20 Jan 2008
Views: 5,036
Bookmarked: 20 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
14 votes for this Article.
Popularity: 3.68 Rating: 3.21 out of 5
5 votes, 38.5%
1
0 votes, 0.0%
2
2 votes, 15.4%
3
1 vote, 7.7%
4
5 votes, 38.5%
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


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
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 5 of 5 (Total in Forum: 5) (Refresh)FirstPrevNext
Subject  Author Date 
GeneralSome BugsmemberMember 285390011:47 16 Jul '08  
Generalthe download project doesn't workmemberbig_onion18:20 20 Jan '08  
GeneralRe: the download project doesn't workmemberTamer Oz18:32 20 Jan '08  
GeneralRe: the download project doesn't workmemberbig_onion19:20 20 Jan '08  
GeneralAlgorithm usedmemberImtiaz 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-2008
Web13 | Advertise on the Code Project