The app parses math expressions and calculates results. It's easy to extend. Just create new operator which implements either IBinaryOperator
or IUnaryOperator
interface located in Calculator.Operators.Interfaces
.
public sealed class AddOperator : IBinaryOperator
{
public int Precedence { get; set; }
public OperatorAssociativity OperatorAssociativity { get; set; }
public string OperatorNotationInput { get; set; }
public string OperatorNotationOutput { get; set; }
public decimal Calculate(decimal operand1, decimal operand2)
{
return operand1 + operand2;
}
public AddOperator()
{
Precedence = 4;
OperatorNotationOutput = OperatorNotationInput = "+";
OperatorAssociativity = OperatorAssociativity.Left;
}
}
public sealed class DecadicLogarithmOperator : IUnaryOperator
{
public int Precedence { get; set; }
public OperatorAssociativity OperatorAssociativity { get; set; }
public string OperatorNotationInput { get; set; }
public string OperatorNotationOutput { get; set; }
public UnaryOperatorType UnaryOperatorType { get; set; }
public decimal Calculate(decimal operand)
{
return (decimal) Math.Log10((double) operand);
}
public DecadicLogarithmOperator()
{
Precedence = 64;
OperatorNotationOutput = OperatorNotationInput = "log10";
UnaryOperatorType = UnaryOperatorType.Function;
OperatorAssociativity = OperatorAssociativity.Left;
}
}