Skip to content

Commit 9abc467

Browse files
committed
Update README.md
1 parent c4b771f commit 9abc467

File tree

1 file changed

+118
-2
lines changed

1 file changed

+118
-2
lines changed

README.md

Lines changed: 118 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ Mathos Parser
22
=============
33
Mathos Parser (along with Mathos Parser Portable, and Expression Compiler) are currently transferred from CodePlex to GitHub. The old page can be found below:
44
* http://mathosparser.codeplex.com/
5-
*
65

76
**Mathos Parser** is a mathematical expression parser, built on top of the .NET Framework, which allows you to parse all kinds of mathematical expressions, and in addition, add your own custom functions, operators, and variables (see the online demo).
87

@@ -22,6 +21,123 @@ NEW: We have launched a new support site, Q&A style: http://support.mathosprojec
2221
* And much more!
2322

2423
##Introduction
25-
Introduction
2624

2725
Mathos Parser is a part of Mathos Project, a project that provides useful methods, structures, etc, to make the life a little bit easier! This math parser is fully independent of Mathos project, so you can just use this library to archive a powerful math parsing experience.
26+
27+
##How to use
28+
29+
It's really easy to use and understand this math parser. In this topic I will try to show you some (not all) key features of this library.
30+
31+
* Adding a custom operator.
32+
* Adding a custom function.
33+
* Functions with more than one argument.
34+
* Programatically add variables.
35+
36+
###Adding custom operator
37+
38+
````
39+
// declaring the parser
40+
MathParser parser = new MathParser();
41+
42+
//customize the operator list
43+
parser.OperatorList = new List<string>() { "^", "%", "*", ":", "/", "+", "-", ">", "<", "=" };
44+
45+
// adding sqrt to the OperatorAction list
46+
parser.OperatorAction.Add("^", delegate(decimal numA, decimal numB)
47+
{
48+
return (decimal)Math.Pow((double)numA, (double)numB);
49+
});
50+
51+
// parsing and comparing
52+
Assert.IsTrue(parser.Parse("3^2") == (decimal)Math.Pow (3,2));
53+
````
54+
###Adding custom function
55+
````
56+
public void CustomFunctions()
57+
{
58+
/*
59+
* This test demonstrates three ways of adding a function
60+
* to the Math Parser
61+
*
62+
* 1) directly pointing to the function
63+
* 2) lambda expression
64+
* 3) anonymous method
65+
*/
66+
67+
MathParser parser = new MathParser();
68+
69+
//for long functions
70+
parser.LocalFunctions.Add("numberTimesTwo", NumberTimesTwoCustomFunction); // adding the function
71+
decimal resultA = parser.Parse("numberTimesTwo(3)");
72+
73+
//for short functions, use lambda expression, or anonymous method
74+
// 1) using lambda epxression (recommended)
75+
parser.LocalFunctions.Add("square", x => x[0] * x[0]);
76+
decimal resultB = parser.Parse("square(4)");
77+
78+
// 2) using anonymous method
79+
parser.LocalFunctions.Add("cube", delegate(decimal[] x)
80+
{
81+
return x[0] * x[0] * x[0];
82+
});
83+
decimal resultC = parser.Parse("cube(2)");
84+
85+
}
86+
public decimal NumberTimesTwoCustomFunction(decimal[] input)
87+
{
88+
return input[0] * 2;
89+
}
90+
````
91+
###Functions with more than one operator
92+
````
93+
/*
94+
* This example demonstrates the "anonymous method" way of adding
95+
* a function that can take more than one agument.
96+
*/
97+
98+
MathParser parser = new MathParser();
99+
100+
//for long functions
101+
parser.LocalFunctions.Add("log", delegate(decimal[] input) // adding the function
102+
{
103+
// input[0] is the number
104+
// input[1] is the base
105+
106+
if (input.Length == 1)
107+
{
108+
return (decimal)Math.Log((double)input[0]);
109+
}
110+
else if (input.Length == 2)
111+
{
112+
return (decimal)Math.Log((double)input[0], (double)input[1]);
113+
}
114+
else
115+
{
116+
return 0; // false
117+
}
118+
});
119+
120+
decimal resultA = parser.Parse("log(2)");
121+
decimal resultB = parser.Parse("log(2,3)");
122+
````
123+
###Programatically add variables
124+
```
125+
/*
126+
* when parsing an expression that requires
127+
* for instance a variable name declaration
128+
* or change, use ProgramaticallyParse().
129+
*/
130+
MathParser parser = new MathParser();
131+
132+
// first way, using let varname = value
133+
decimal resultA = parser.ProgrammaticallyParse("let a = 2pi");
134+
Assert.IsTrue (parser.Parse ("a") == (decimal)Math.PI*2);
135+
136+
// second way, using varname := value
137+
decimal resultC = parser.ProgrammaticallyParse("b := 20");
138+
Assert.IsTrue(parser.Parse("b") == 20);
139+
140+
// third way, using let varname be value
141+
decimal resultD = parser.ProgrammaticallyParse("let c be 25");
142+
Assert.IsTrue(resultD == 25);
143+
```

0 commit comments

Comments
 (0)