-2

I plan to write a program or rather function which will be able to analyze a string parameter which in turn will be math expression. Only the 4 basic operations are allowed(addition, subtraction, multiplication and division) and the numbers are all whole numbers from -100 to 100. The result is allowed to be float. I know the registries work in the same way I.e calculate result of two numbers and store it, than calculate result of stored value and the next operant and store. And so forth until there are no operands left. The number of operands will usually be 2 but I will have a need of 3 or even more so yes, more operands is a requirement.

I was wondering how would you structure this in C#? What tools helper functions you would use in this scenario?

Note: I am working on Unity 5.1.4 project and I want to use a math parser in it. Unity is .NET 2.0

Note: This seems most promising: http://mono.1490590.n4.nabble.com/Javascript-eval-function-in-c-td1490783.html It uses a variant of eval() function.

Vlad
  • 2,739
  • 7
  • 49
  • 100
  • 1
    Please define what “to analyze a string parameter” means, and why you would want to write one from scratch instead of using one of the many existing arithmetic parsers already available. – Dour High Arch Dec 18 '15 at 23:39
  • Well I was searching for math engines and found Wolfram Alpha but never thought to search for aritmetic parsers. BTW Wolfram alpha enables 2000 queries for free. Will close the question once I find enough info. Thanks – Vlad Dec 19 '15 at 10:43

2 Answers2

1

In .NET there are no some high level helper functions to help you with this. You would have to parse and tokenize the string in your code. There are however third party libraries that do what you need, for instance Expression Compiler, Simple Math Parser, Mathos Parser, and many other. Search for math expression parser.

If you want to make one from scratch you could look the code of existing ones.

Hans Passant mentions a simple solution, maybe just what you need. You get the result of the expression, so if you need just that, and not the actual expression tokens, then .NET got you covered.

Community
  • 1
  • 1
Dialecticus
  • 16,400
  • 7
  • 43
  • 103
  • 1
    Not quite, .NET [actually has some](http://stackoverflow.com/a/2607976/17034), just what the OP is asking for. – Hans Passant Dec 18 '15 at 23:27
  • How about .NET 2.0 and I can import in Unity 5.1.4 project? :) DataTable.Compute works in .NET 4.5 and 4.6. Tried some of the parsers, they seem incompatible with .NET 2.0. Tried NCalc it is just evaluator of math expressions. I need the value of expression as well. – Vlad Dec 20 '15 at 12:33
  • Found this one which is 195 USD :D http://www.blueanalysis.com/acidlibrary.php – Vlad Dec 20 '15 at 12:37
  • 2
    @Vlad DataTable.Compute is there from .NET 1.1. It's there [in .NET 2 too](https://msdn.microsoft.com/en-us/library/system.data.datatable.compute%28v=vs.80%29.aspx). – Dialecticus Dec 20 '15 at 19:16
  • I tried to add System.Data from .NET assembly folder in C:\Windows\Microsoft.NET\Framework\v2.0.50727 but Unity was complaining :) – Vlad Dec 21 '15 at 10:26
0

This tool finished the job with no adding external references, dlls or what not: http://mono.1490590.n4.nabble.com/Javascript-eval-function-in-c-td1490783.html

Vlad
  • 2,739
  • 7
  • 49
  • 100