-1

I have to calculate method which takes as parameters the first number, second number and the operation.

public static double Calculate(double firstNumber, double secondNumber, string operation)
{
    var result = default(double);
    switch (operation)
    {
        case "+":
            result = firstNumber + secondNumber;
            break;
        case "-":
            result = firstNumber - secondNumber;
            break;
        case "*":
            result = firstNumber * secondNumber;
            break;
        case "/":
            result = firstNumber / secondNumber;
            break;
    }

    return result;
}

Right now i am using a switch. When the operation is a "+", i add the numbers.

How could make the calculate method take any arithmetic operator as a parameter without having to add a specific case for it?

My idea would by that in the end it looks like this:

public static string Calculate(double firstNumber, double secondNumber, string operation)
{
     return firstNumber operation secondNumber;
}

I know from this question that there is a way, for example using

new DataTable().Compute("1 + 2 * 7", null)

this would require me to convert my numbers to string what i don't want.

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
Nightscape
  • 464
  • 6
  • 19

2 Answers2

5

Let's extract model in a form of dictionary for all these operators:

 private static Dictionary<string, Func<double, double, double>> s_Operators = new
   Dictionary<string, Func<double, double, double>>() {
     { "+", (x, y) => x + y },
     { "-", (x, y) => x - y },
     { "*", (x, y) => x * y },
     { "/", (x, y) => x / y },

     //TODO: Add new operations if you want, e.g.
     // { "^", (x, y) => Math.Pow(x, y) }, 
   };

Then you can easily implement Calculate:

public static double Calculate(double firstNumber, double secondNumber, string operation) 
{
  return s_Operators.TryGetValue(operation, out var func)
    ? func(firstNumber, secondNumber)
    : throw new ArgumentException($"Unknown {operation} operation", nameof(operation));
}

Please, note, that you have to Dispose DataTable instance, that's why computing with a help DataTable will be something like this:

private static double TableCompute(string formula) {
  using (DataTable table = new DataTable()) {
    return Convert.ToDouble(table.Compute(formula, null));
  }
}
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
1

For DataTable().Compute() to work, there is no need to convert numbers to string,

using System;
using System.Data;

public class Program
{
    public static void Main()
    {
        Console.WriteLine("Addition -> " + Calculate(6, 3, "+").ToString());
        Console.WriteLine("Substraction -> " + Calculate(6, 3, "-").ToString());
        Console.WriteLine("Multiplication -> " + Calculate(6, 3, "*").ToString());
        Console.WriteLine("Division -> " + Calculate(6, 3, "/").ToString());
    }

    public static double Calculate(double firstNumber, double secondNumber, string operation)
    {       
        return Convert.ToDouble(new DataTable().Compute(firstNumber + operation + secondNumber, null));
    }
}

Output for input numbers 6 and 3

Addition -> 9
Substraction -> 3
Multiplication -> 18
Division -> 2
as-if-i-code
  • 2,103
  • 1
  • 10
  • 19