public AlphameticEquation(string input)
        {
            input = input.ToUpper();

            Match matchEquation = Regex.Match(input, @"^([A-Z]+[+\-/*])*[A-Z]+=[A-Z]+$"); // Check equation format.

            if (!matchEquation.Success)
            {
                throw new FormatException("Invalid math equation format.");
            }

            string[] equationSides = input.Split('='); // Split into left and right parts.
            EqualsPart = equationSides[1];

            MatchCollection termMatches = Regex.Matches(equationSides[0], @"[A-Z]+"); // Get all letter terms.

            foreach (Match termMatch in termMatches)
            {
                Terms.Add(termMatch.Value);
            }

            MatchCollection operatorMatches = Regex.Matches(equationSides[0], @"[+\-/*]"); // Get all operators.

            foreach (Match operatorMatch in operatorMatches)
            {
                Operators.Add(MathOperatorsMethods.GetOperatorFromString(operatorMatch.Value));
            }
        }
Example #2
0
        private bool IsSolutionCorrect()
        {
            double result = int.Parse(TranslateAlphametic(Equation.Terms[0], LetterNumberPairs));

            for (int i = 0; i < Equation.Operators.Count; i++)
            {
                result = MathOperatorsMethods.PerformOperation(result, int.Parse(TranslateAlphametic(Equation.Terms[i + 1], LetterNumberPairs)), Equation.Operators[i]);
            }
            return(result == int.Parse(TranslateAlphametic(Equation.EqualsPart, LetterNumberPairs)));
        }
        public override string ToString()
        {
            string output = "";

            for (int i = 0; i < Terms.Count - 1; i++)
            {
                output += Terms[i] + MathOperatorsMethods.OperatorToString(Operators[i]);
            }
            return(output + Terms[Terms.Count - 1] + "=" + EqualsPart);
        }