-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.

2
  • 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. Dec 18, 2015 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, 2015 at 10:43

2 Answers 2

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.

5
  • 1
    Not quite, .NET actually has some, just what the OP is asking for. Dec 18, 2015 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, 2015 at 12:33
  • Found this one which is 195 USD :D blueanalysis.com/acidlibrary.php
    – Vlad
    Dec 20, 2015 at 12:37
  • 2
    @Vlad DataTable.Compute is there from .NET 1.1. It's there in .NET 2 too. Dec 20, 2015 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, 2015 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

Not the answer you're looking for? Browse other questions tagged or ask your own question.