0

I have a list of type decimal containing 5 numbers: {10, 9, 100,73,3457}. I have another list of 4 operators of type string between these numbers: * multiplication - subtraction / division + adding.

How can I get the result of the string? I do not know how to impose operator precedence.

10 * 9 - 100 / 73 + 3457 = ???

Here is the code I have, I'm approaching the problem in entirely the wrong way, how should it be done?

static List<decimal> numbers = new List<decimal>();
static List<string> operators = new List<string>();

foreach (decimal number in numbers)
{
    foreach (string operatorz in operators)
    {
        resultnumber = 
    }
}
  • Do you really need a result computed 20 times, or is the looping optional? – HABO Mar 22 '13 at 13:10
  • Google "skeet misc utility" - buried in there is a fantastic mathematical operator library that plays reasonably well with generic concepts. May not solve your problem, but definitely "utile". – JerKimball Mar 22 '13 at 13:12
  • Programmers ought to use a library or ready made tool like this: http://stackoverflow.com/questions/4620437/evaluate-an-arithmetic-expression-stored-in-a-string-c If you want to write the guts yourself, you'll have to research some algorithms as defined here: http://stackoverflow.com/questions/1437964/best-and-shortest-way-to-evaluate-mathematical-expressions, Asking for stack overflow to tell you what letters to type defeats the purpose of the exercise, you might as well not do it at all. The result will be the same. – Eric Leschinski Mar 22 '13 at 13:15
  • What's the point of this exercise? Are you trying to learn how to build a parser? – Eric Lippert Mar 22 '13 at 15:07

3 Answers3

4

Hope I've got what you want, This will make a cross-join of all numbers into them-self and then into operators. You've listed binary operators, so I suppose you need two operands for each operator. Number of resulting rows would be = numbers.Length * numbers.Length * operators.Length

void Main()
{
   var numbers = new[] { 10, 9, 100, 73, 3457 };
   var operators = new [] { "*", "+", "/", "-" };


    var r = from n1 in numbers
            from n2 in numbers
            from op in operators
            select string.Format("{0} {1} {2} = {3}",
                                  n1, op, n2,   Perform(n1, op, n2));

}

public int Perform(int val1, string @operator, int val2)
{
    //main core if your question, consider to extract operators to outer level
    var operators = new Dictionary<string, Func<int, int, int>>
    {
        {"+", (v1, v2) => v1 + v2},
        {"/", (v1, v2) => v1 / v2},
        {"*", (v1, v2) => v1 * v2},
        {"-", (v1, v2) => v1 - v2},
    };

    return operators[@operator](val1, val2);
}

result would be

10 * 10 = 100 
10 + 10 = 20 
10 / 10 = 1 
10 - 10 = 0 
10 * 9 = 90 
....
Ilya Ivanov
  • 23,148
  • 4
  • 64
  • 90
1

Not entirely clear what you wanted. But assumed those operators in-between the numbers, and resolving to the native operator precedence: Simple way for that is to script it up. Your operator precedence will be native.

Scripting

private static void Main(string[] args)
{
    var numbers = new[]{ 10, 9, 100, 73, 3457 };
    var ops = new[] { "*","-","/","+" };

    if (numbers.Length != ops.Length + 1)
        throw new Exception("TODO");

    string statement = numbers[0].ToString();

    for (int i = 0; i < ops.Length; i++)
    {
        statement += string.Format(" {0} {1}", ops[i], numbers[i + 1]);
    }

    Console.WriteLine("Statement: " + statement);

    var method = String.Format(@"int Product() {{ return {0}; }} ", statement);

    Console.WriteLine("Method: " + method);


    var Product = CSScript.Evaluator.CreateDelegate(method);
    int result = (int)Product();

    Console.WriteLine(result);
}

Result:

Statement: 10 * 9 - 100 / 73 + 3457
Method: int Product() { return 10 * 9 - 100 / 73 + 3457; }
3546
Press any key to continue . . .
Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
Meirion Hughes
  • 24,994
  • 12
  • 71
  • 122
-1

http://msdn.microsoft.com/fr-fr/library/s53ehcz3(v=VS.80).aspx

you can use your own class for processing calculations

Joan Caron
  • 1,969
  • 18
  • 21