Click here to Skip to main content
15,898,222 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
I created a API to evaluate an expression. It takes expression like "100+200+Var1" and return as "300+var1" without evaluating variables since variable value not known.

But when i tried to evaluate expression like "100+var1+200" i can only return as "100+var1+200" but not "300+var1".

Is there any example which evaluates arithmetic expression without evaluating
variables.

What I have tried:

I searched google and found "NCalc - Mathematical Expressions Evaluator for .NET" to evaluate arithmetic expression with variables but it expects values for those variables
before running it.
Posted
Updated 6-Apr-16 2:40am
Comments
phil.o 6-Apr-16 8:15am    
Maybe if you let us have a look at your evaluator we could try to help. Please show your actual code.

1 solution

You need to start looking at Operator associativity - Wikipedia, the free encyclopedia[^]
"+" is both left- and right- associative, so a + b + c can be evaluated in any order, so your evaluator would be free to assign a "fixed/variable" value to each part of the expression and rearrange it to "group" the fixed and variable parts:
100 + var1 + 200
 F  +  V   +  F
therefore:
100 + 200 + var1
 F  +  F  +  V
is equivalent

Exactly how you do that in your code I can't say: I have no idea of how it works. But consider adding an associativity property to your operators, and it should give you the beginnings of what you want.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900