Türkçe   |   English
Calendar
ArticleCategories
Article Archive
Most Read Articles
Links
Blogroll
Files

MathParser - Math Formula Parser
Publish Date 20.01.2008
Categories C#,.Net Framework
Views 2750
Summary
MathParser is a class that can calculate the result of mathematic operations given as string such as "3+5/2-3*(1+3)". Also it can calculate the result of parametric formulas such as 3A+5B/2C. Calculation is made by operation order(/,*,-,+) and paranthesis 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)". Also it can calculate the result of parametric formulas such as 3A+5B/2C. Calculation is made by operation order(/,*,-,+) and paranthesis rule. In some projects users wants to create a formula dynamically. This is a useful method for calculating the values of this type 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 with 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 with 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)");

Download Source
CodeProject

30.06.2009 - Adnan
Great Code. Is it possible If you have the similar code for PHP. Many Many Thx

18.04.2008 - Alok Jadhav
math formula calculation
Add Comment
First Name Last Name
Web Site
E-Mail
Comment
Security Picture

Photos

Bob Marley - me
Show All
Me in MSDN Forums
-   Answered the question Close some open windows by name in the Visual C# General forum
-   Answered the question Close some open windows by name in the Visual C# General forum
-   Answered the question Datagridview Click Event in C#.net in the Visual C# General forum
-   Contributed a proposed answer to the question Close some open windows by name in the Visual C# General forum
-   Replied to the question Datagridview Click Event in C#.net in the Visual C# General forum
-   Replied to the question Close some open windows by name in the Visual C# General forum
-   Contributed a helpful post (total votes:2) to the forums thread Array of events? in the Visual C# General forum
-   Answered the question Displaying random images from resources in the project in the Visual C# Language forum
-   Contributed a helpful post (total votes:1) to the forums thread sorted dictionary (Can i get key from value) in the Visual C# General forum
-   Contributed a proposed answer to the question Non-existing Argument in format string in the Visual C# General forum
Entries
News
Articles