Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse Math Expression [duplicate]

Tags:

c#

.net

c#-4.0

Is there an easy way to parse a simple math expression represented as a string such as (x+(2*x)/(1-x)), provide a value for x, and get a result?

I looked at the VSAEngine per several online examples, however, I am getting a warning that this assembly has been deprecated and not to use it.

If it makes any differences, I am using .NET 4.0.

like image 280
Brandon Avatar asked Oct 19 '10 21:10

Brandon


People also ask

What does parsing an expression mean?

Parsing is the process of analysing a string of symbols, expressed in natural or computer languages that will accord formal grammar. Expression Parsing in Data Structure means the evaluation of arithmetic and logical expressions.

What is parsing expression C++?

Ternary Expression Parser in C++Each number will contain only one digit. The conditional expressions group right-to-left. The condition will always be either T or F. So the condition will never be a digit. The result of the expression will always evaluate to either a digit 0-9, T or F.

How do you parse an expression in Java?

Here's how an arithmetic expression is parsed. A pointer is started at the left and is iterated to look at each character. It can be either a number(always a single-digit character between 0 and 9) or an operator (the characters +, -, *, and /). If the character is a number, it is pushed onto the stack.


1 Answers

I urge caution against choosing an existing generic expression evaluator over a purpose-built math evaluator. The reason for this is expression evaluators are not limited to math. A clever individual could use this to create an instance of any type in the framework and call any method on the type, and that would allow him to do some decidedly unwelcome things. For example: new System.Net.WebClient().DownloadFile("illegalchildpornurl", "C:\openme.gif") will evaluate just fine in most of those, and do just what it sounds like it would (and make you a felon at the same time).

This doesn't mean don't look for something that's already written; it just means be careful. You want one that does math, and only math. Most of what's already out there isn't that picky.

like image 185
Joel Coehoorn Avatar answered Oct 13 '22 00:10

Joel Coehoorn