using System; using LoreSoft.MathExpressions.Properties; using System.Diagnostics.CodeAnalysis; namespace LoreSoft.MathExpressions { /// /// Class representing a math operator expression. /// public class OperatorExpression : ExpressionBase { /// The supported math operators by this class. private static readonly char[] operatorSymbols = new char[] {'+', '-', '*', '/', '%', '^'}; /// Initializes a new instance of the class. /// The operator to use for this class. /// When the operator is null or empty. /// When the operator is invalid. public OperatorExpression(string @operator) { if (string.IsNullOrEmpty(@operator)) throw new ArgumentNullException("operator"); switch (@operator) { case "+": base.Evaluate = new MathEvaluate(Add); _mathOperator = MathOperators.Add; break; case "-": base.Evaluate = new MathEvaluate(Subtract); _mathOperator = MathOperators.Subtract; break; case "*": base.Evaluate = new MathEvaluate(Multiple); _mathOperator = MathOperators.Multiple; break; case "/": base.Evaluate = new MathEvaluate(Divide); _mathOperator = MathOperators.Divide; break; case "%": base.Evaluate = new MathEvaluate(Modulo); _mathOperator = MathOperators.Modulo; break; case "^": base.Evaluate = new MathEvaluate(Power); _mathOperator = MathOperators.Power; break; default: throw new ArgumentException(Resources.InvalidOperator + @operator, "operator"); } } private MathOperators _mathOperator; /// Gets the math operator. /// The math operator. public MathOperators MathOperator { get { return _mathOperator; } } /// Gets the number of arguments this expression uses. /// The argument count. public override int ArgumentCount { get { return 2; } } /// Adds the specified numbers. /// The numbers. /// The result of the operation. /// When numbers is null. /// When the length of numbers do not equal . public double Add(double[] numbers) { base.Validate(numbers); double result = 0; foreach (double n in numbers) result += n; return result; } /// Subtracts the specified numbers. /// The numbers. /// The result of the operation. /// When numbers is null. /// When the length of numbers do not equal . public double Subtract(double[] numbers) { base.Validate(numbers); double? result = null; foreach (double n in numbers) if (result.HasValue) result -= n; else result = n; return result ?? 0; } /// Multiples the specified numbers. /// The numbers. /// The result of the operation. /// When numbers is null. /// When the length of numbers do not equal . public double Multiple(double[] numbers) { base.Validate(numbers); double? result = null; foreach (double n in numbers) if (result.HasValue) result *= n; else result = n; return result ?? 0; } /// Divides the specified numbers. /// The numbers. /// The result of the operation. /// When numbers is null. /// When the length of numbers do not equal . public double Divide(double[] numbers) { base.Validate(numbers); double? result = null; foreach (double n in numbers) if (result.HasValue) result /= n; else result = n; return result ?? 0; } /// Modulo the specified numbers. /// The numbers. /// The result of the operation. /// When numbers is null. /// When the length of numbers do not equal . public double Modulo(double[] numbers) { base.Validate(numbers); double? result = null; foreach (double n in numbers) if (result.HasValue) result %= n; else result = n; return result ?? 0; } /// Power for the specified numbers. /// The numbers. /// The result of the operation. /// When numbers is null. /// When the length of numbers do not equal . public double Power(double[] numbers) { base.Validate(numbers); return Math.Pow(numbers[0], numbers[1]); } /// Determines whether the specified string is a math symbol. /// The string to check. /// true if the specified string is a math symbol; otherwise, false. [SuppressMessage("Microsoft.Design", "CA1062:ValidateArgumentsOfPublicMethods")] public static bool IsSymbol(string s) { if (s == null || s.Length != 1) return false; char c = s[0]; return IsSymbol(c); } /// Determines whether the specified char is a math symbol. /// The char to check. /// true if the specified char is a math symbol; otherwise, false. public static bool IsSymbol(char c) { return Array.Exists(operatorSymbols, delegate(char s) { return s == c; }); } /// /// Returns a that represents the current . /// /// /// A that represents the current . /// /// 2 public override string ToString() { return _mathOperator.ToString(); } } }