Example #1
0
        /// <summary>
        /// If obj is null or obj is not a Formula, returns false.  Otherwise, reports
        /// whether or not this Formula and obj are equal.
        ///
        /// Two Formulae are considered equal if they consist of the same tokens in the
        /// same order.  To determine token equality, all tokens are compared as strings
        /// except for numeric tokens, which are compared as doubles, and variable tokens,
        /// whose normalized forms are compared as strings.
        ///
        /// For example, if N is a method that converts all the letters in a string to upper case:
        ///
        /// new Formula("x1+y2", N, s => true).Equals(new Formula("X1  +  Y2")) is true
        /// new Formula("x1+y2").Equals(new Formula("X1+Y2")) is false
        /// new Formula("x1+y2").Equals(new Formula("y2+x1")) is false
        /// new Formula("2.0 + x7").Equals(new Formula("2.000 + x7")) is true
        /// </summary>
        public override bool Equals(object obj)
        {
            // If obj is null, return false.
            if (obj == null)
            {
                return(false);
            }
            // If obj cannot be cast to Formula, return false.
            Formula Formula2 = obj as Formula;

            if ((System.Object)Formula2 == null)
            {
                return(false);
            }
            // Retrieve each Formula object's tokens.
            List <string> Formula1Tokens = this.GetFormulaTokens();
            List <string> Formula2Tokens = Formula2.GetFormulaTokens();

            // If Formulas contain a non-equal amount of variables, then they are not equal.
            if (!(Formula1Tokens.Count == Formula2Tokens.Count))
            {
                return(false);
            }
            // Compare the variables from each formula object.
            List <string> Formula1Variables = this.GetFormulaVariables();
            List <string> Formula2Variables = Formula2.GetFormulaVariables();

            // Check that formulas have same number of variables
            if (!(Formula1Variables.Count == Formula2Variables.Count))
            {
                return(false);
            }
            // Check that all variables are equal.
            for (int i = 0; i < Formula1Variables.Count; i++)
            {
                if (!(Formula1Variables[i] == Formula2Variables[i]))
                {
                    return(false);
                }
            }

            // Variables to contain parsed doubles from tokens for comparison.
            double Formula1Double = 0;
            double Formula2Double = 0;

            for (int i = 0; i < Formula1Tokens.Count; i++)
            {
                // If current token is a double, attempt to parse the corresponding token from each list.
                if (Double.TryParse(Formula1Tokens[i], out Formula1Double))
                {
                    // If the other object's corresponding token is not also a parsable double, return false.
                    if (!Double.TryParse(Formula2Tokens[i], out Formula2Double))
                    {
                        return(false);
                    }
                    // If the parsed doubles are not equal, return false.
                    else if (!(Formula2Double == Formula1Double))
                    {
                        return(false);
                    }
                }
                // Skip over any variables. (They have already been compared)
                if (Formula1Tokens[i].IsValidVariable())
                {
                    continue;
                }


                // The rest of the tokens should be normalized variables and operators, so we simply compare them.
                else if (!(Formula1Tokens[i] == Formula2Tokens[i]))
                {
                    // If tokens are not equal, return false.
                    return(false);
                }
            }
            // If the for loop completes, the Formula objects are equal by the provided definition.
            return(true);
        }