abazgiriabazgiri
abazgiriabazgiri

Reputation: 19

how to do basic math calculations with a list of numbers and operators

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 = 
    }
}

Upvotes: 0

Views: 2025

Answers (3)

Meirion Hughes
Meirion Hughes

Reputation: 26428

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

Upvotes: 1

Ilya Ivanov
Ilya Ivanov

Reputation: 23636

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

Upvotes: 4

Joan Caron
Joan Caron

Reputation: 1970

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

you can use your own class for processing calculations

Upvotes: -1

Related Questions