0

I have a List<String> that is full of values and operators.

["123", "+", "(", "890", "-", "15.00", ")"]

I know that I can make an algorithm that will push these numbers and and operators onto a stack and pop them off and evaluate as I go. But, is there a better way to do with without using a external library?

Cœur
  • 37,241
  • 25
  • 195
  • 267
J Lundberg
  • 2,313
  • 2
  • 20
  • 23
  • Are these broken up from user input or fed to you by some other system? – user7116 Aug 16 '11 at 17:37
  • Are you trying to do it without either using an existing library OR writing your own implementation? Or are you looking for a library included in the standard libraries? – corsiKa Aug 16 '11 at 17:38
  • I am looking for something that is included in the standard library or I will write my own algorithm. – J Lundberg Aug 16 '11 at 17:39
  • 3
    Use can use any implementation of the standard infix (inorder) expression evaluator... [Sample Code to Evaluate an Infix Expression](http://scriptasylum.com/tutorials/infix_postfix/algorithms/infix-postfix/index.htm) – Devendra D. Chavan Aug 16 '11 at 17:44
  • I have been very happy with ILCalc (http://ilcalc.codeplex.com/). It is an external library, but it is on codeplex, so you get source code if you need to see it. – Philipp Schmid Aug 16 '11 at 17:48

4 Answers4

4

Pushing the numbers and operators onto a stack would be an interpreter.

The obvious better way to do this (for some definition of "better") is to write a compiler!

You already have the input split into lexical tokens, so you can skip implementing a lexer and can dive right into building the AST. You can find suitable classes to transform your input to in the System.​Linq.​Expressions Namespace; have a look at the Expression Class. You can wrap the result in a lambda expression, compile it to IL and execute it on the CLR!

dtb
  • 213,145
  • 36
  • 401
  • 431
  • It seems kind of pointless to use a compiler to evaluate a constant, but if he had variables then it would be a good idea. – Gabe Aug 16 '11 at 18:30
2

You can join the List and then let the compiler evaluate it at runtime as Mehrdad stated.

Expression e = new Expression("5 * 2"); 
e.Evaluate();

I found a very similar question already asked here

update :

NCalc "NCalc - Mathematical Expressions Evaluator for .NET" even though this is an external library I think it is an open source project which means you can add the code directly to your project.

Update :

You can use the String.Join function to join the List.

string formula = String.Join("",listMathOperators);
Community
  • 1
  • 1
Jethro
  • 5,896
  • 3
  • 23
  • 24
0

You can always use the C# compiler classes in the .NET framework to evaluate it as a C# expression...

user541686
  • 205,094
  • 128
  • 528
  • 886
0

I have a complete class that calculates any expression.

First, you need to extract all strings of your list to a single string, like:

string sExpression = "123+(890-15.00)";

Second, you need to evaluate this expression in a parser, I have a complete class that makes this for you, but I can't attach files here.

gandarez
  • 2,609
  • 4
  • 34
  • 47