I need a function that will take operands/operators as parameters and provide the evaluation result. The problem that I am facing is how to elegantly parse an operator.
Sample code is as below
internal static bool Evaluator(double operand1, double operand2, string operation)
{
bool evaluation = false;
switch (operation)
{
case "<":
evaluation = operand1 < operand2;
break;
case ">":
evaluation = operand1 > operand2;
break;
case "<=":
evaluation = operand1 <= operand2;
break;
default:
break;
}
return evaluation;
}
I can put operators in enum (or extendable enum) and use strategy pattern to remove the switch case. The issue remains, I cannot parse operators. example
op1="<";
var operation = Operation.Parse(op1);
var result = operand1 <-- operation should come here --> operand2.
Please suggest how to refactor above code (Evaluator function) elegantly.