11,927,156 members (49,481 online)
Tip/Trick
Add your own
alternative version

61.1K views
6.3K downloads
62 bookmarked
Posted

# Math Parser .NET C#

, 28 Feb 2014 GPL3
 Rate this:
Please Sign up or sign in to vote.
This is light, fast and simple to understand mathematical parser designed in one class, which receives as input a mathematical expression (System.String) and returns the output value (System.Double)

## Introduction

This is light, fast and simple to understand mathematical parser designed in one class, which receives as input a mathematical expression (System.String) and returns the output value (System.Double). For example, if your input string is "√(625)+25*(3/3)" then parser returns double value — 50.

## Background

The idea was to create a string calculator for educational goals.

## How it work

For more please look at the code, I tried to explain the work of the parser in the comments and how it can be changed and extended.

Convert to RPN:

* Input operators are replaced by the corresponding token (its necessary to distinguish unary from binary).

## Using the code

Example:

```public static void Main()
{
MathParser parser = new MathParser();
string s1 = "pi+5*5+5*3-5*5-5*3+1E1";
string s2 = "sin(cos(tg(sh(ch(th(100))))))";
bool isRadians = false;
double d1 = parser.Parse(s1, isRadians);
double d2 = parser.Parse(s2, isRadians);

Console.WriteLine(d1); // 13.141592...
Console.WriteLine(d2); // 0.0174524023974442
Console.ReadKey(true);
}
```

## Features

Ariphmetical operators :

• () — parentheses;
• + —  plus (a + b);
• - — minus (a - b);
• * — multiplycation symbol (a * b);
• / — divide symbol (a / b);
• ^ — degree symbol (a ^ b).

Functions :

Trigonometric functions:

• sin(x);
• cos(x);
• tg(x);
• ctg(x).

Hyperbolic functions:

• sh(x);
• ch(x);
• th(x).

Other functions:
• √(a), sqrt(a) — square root of a number;
• exp(x)  — exponential function (or just use e^x);
• (a)log(b) — logarithm;
• ln(x) — natural logarithm;
• abs(x) — absolute value.

Constants:

• pi — 3.14159265...;
• e — 2.71828183....

Arguments of the trigonometric functions can be expressed as radians (true) or degrees (false) by sending appropriate bool value to parse method. Example (as radians):

`parser.Parse("sin(90)", true);`

Work with any char decimal separator in real number (regional settings).

New operators, functions and constants can be easily added to the code.

This parser is simple (special designed in single class), convenient for the further implementation, expansion and modification.

## Points of Interest

I better understand how parsers work and learned about the reverse-polish notation.

## History

• 2012/05/09: released the source code (1_0);
• 2012/06/07: optimized parser (1_2);
• 2014/02/28: rewritten version (1_4).

## License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)

## About the Author

 Kazakstan
My full name is Yerzhan Kalzhani. I’m a pragmatic software developer who loves tasks that are interesting and challenging. My motto in software developing: "Quality, Performance and Agile".

I prefer systematic approach when solving problem, so I think that every good developer first of all should be able to manage the complexity of a software system and produce the well-structured solution for a poor defined problem.

## Comments and Discussions

 First Prev Next
 NewFeatures: ConditionalSupport, CommaSeparatedFunctionArgList, Variables, RPNCaching, PostfixFunctions and more Member 1096039610-Oct-15 8:30 Member 10960396 10-Oct-15 8:30
 RPN export compsystems6-Sep-15 4:23 compsystems 6-Sep-15 4:23
 it's good Member 108375751-Sep-15 2:04 Member 10837575 1-Sep-15 2:04
 Multi-argument functions lianaent15-Mar-15 6:04 lianaent 15-Mar-15 6:04
 Re: Multi-argument functions lianaent15-Mar-15 9:29 lianaent 15-Mar-15 9:29
 Re: Multi-argument functions lianaent15-Mar-15 12:04 lianaent 15-Mar-15 12:04
 Re: Multi-argument functions Member 1096039612-Oct-15 22:42 Member 10960396 12-Oct-15 22:42
 Conditional Expressions possible? mbs-systems14-Feb-15 13:54 mbs-systems 14-Feb-15 13:54
 Re: Conditional Expressions possible? Member 1096039618-Oct-15 7:44 Member 10960396 18-Oct-15 7:44
 Re: Conditional Expressions possible? Member 1162037220-Nov-15 4:31 Member 11620372 20-Nov-15 4:31
 Tests with Log function seem wrong DEGT5-Feb-15 11:00 DEGT 5-Feb-15 11:00
 Now supporting Complex Numbers DEGT2-Feb-15 9:39 DEGT 2-Feb-15 9:39
 Symbol transformation DEGT29-Jan-15 9:31 DEGT 29-Jan-15 9:31
 Re: Symbol transformation Member 1096039618-Oct-15 7:51 Member 10960396 18-Oct-15 7:51
 Excellent job jigoch19-Dec-14 6:28 jigoch 19-Dec-14 6:28
 Great Job! miro125-Mar-14 4:03 miro12 5-Mar-14 4:03
 Re: Great Job! kirnbas24-Mar-14 6:35 kirnbas 24-Mar-14 6:35
 My vote of 4 Florian Rappl29-Aug-12 9:50 Florian Rappl 29-Aug-12 9:50
 Re: My vote of 4 kirnbas29-Aug-12 20:34 kirnbas 29-Aug-12 20:34
 Re: My vote of 4 Florian Rappl29-Aug-12 22:27 Florian Rappl 29-Aug-12 22:27
 Re: My vote of 4 Andreas Gieriet27-Jan-13 9:19 Andreas Gieriet 27-Jan-13 9:19
 Code Clifford Nelson24-Aug-12 8:07 Clifford Nelson 24-Aug-12 8:07
 Re: Code kirnbas24-Aug-12 8:46 kirnbas 24-Aug-12 8:46
 Sym Warren Harding16-Aug-12 21:32 Warren Harding 16-Aug-12 21:32
 Re: Sym kirnbas24-Aug-12 7:19 kirnbas 24-Aug-12 7:19
 Broken Image links Marcus Kramer7-Jun-12 5:53 Marcus Kramer 7-Jun-12 5:53
 Re: Broken Image links kirnbas7-Jun-12 6:32 kirnbas 7-Jun-12 6:32
 Please show and explain the code PIEBALDconsult14-May-12 14:51 PIEBALDconsult 14-May-12 14:51
 Re: Please show and explain the code kirnbas15-May-12 3:22 kirnbas 15-May-12 3:22
 Re: Please show and explain the code PIEBALDconsult15-May-12 8:03 PIEBALDconsult 15-May-12 8:03
 Quite complicated expression evaluator... Andreas Gieriet14-May-12 13:18 Andreas Gieriet 14-May-12 13:18
 You might want to elaborate on some of the inner workings, etc. ednrg14-May-12 9:07 ednrg 14-May-12 9:07
 Last Visit: 31-Dec-99 19:00     Last Update: 27-Nov-15 13:00 Refresh 1

General    News    Suggestion    Question    Bug    Answer    Joke    Praise    Rant    Admin

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.151126.1 | Last Updated 28 Feb 2014
Article Copyright 2012 by kirnbas
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid