internal void CheckAnswer(Equation eqn, List <int> coefs)
        {
            if (coefs.Count != eqn.LeftSide.Count + eqn.RightSide.Count)
            {
                throw new BalancerException("Assertion error: Mismatched length");
            }

            var allzero = true;

            foreach (var coef in coefs)
            {
                allzero = allzero && coef == 0;
            }
            if (allzero)
            {
                throw new BalancerException("Assertion error: All-zero solution");
            }

            foreach (var elem in eqn.GetElements())
            {
                var sum = 0;
                var j   = 0;
                foreach (var term in eqn.LeftSide)
                {
                    sum = CheckedAdd(sum, CheckedMultiply(term.CountElement(elem), coefs[j]));
                    j++;
                }
                foreach (var term in eqn.RightSide)
                {
                    sum = CheckedAdd(sum, CheckedMultiply(term.CountElement(elem), -coefs[j]));
                    j++;
                }
                if (sum != 0)
                {
                    throw new BalancerException("Assertion error: Incorrect balance");
                }
            }
        }
Exemple #2
0
        public void Solve(string input)
        {
            // Clear output
            ErrorMessage   = "";
            EquationHtml   = new HtmlTag("div");
            CodeOutputHtml = new HtmlTag("div");
            var utils = new Utils();

            // Parse equation
            Equation eq = null;

            try
            {
                eq = new Parser(input).ParseEquation();
            }
            catch (BalancerException bex)
            {
                ErrorMessage = "Syntax error: " + bex.Message;

                var start      = bex.Start;
                var end        = bex.End.HasValue ? bex.End.Value : bex.Start;
                var separators = new List <char> {
                    ' ', '\t'
                };

                while (end > start && separators.IndexOf(input[end - 1]) != -1)
                {
                    end--;  // Adjust position to eliminate whitespace
                }
                if (start == end)
                {
                    end++;
                }

                CodeOutputHtml.AppendText(input.Substring(0, start));
                if (end <= input.Length)
                {
                    CodeOutputHtml.Append(utils.CreateElement("u", input.Substring(start, end)));
                    CodeOutputHtml.AppendText(input.Substring(end, input.Length));
                }
                else
                {
                    CodeOutputHtml.Append(utils.CreateElement("u", " "));
                }
            }
            catch (Exception ex)
            {
                ErrorMessage = "Syntax error: " + ex.Message;
                return;
            }

            try
            {
                var matrix = utils.BuildMatrix(eq);
                utils.Solve(matrix);
                var coefs = utils.ExtractCoefficients(matrix);
                utils.CheckAnswer(eq, coefs);
                EquationHtml.Append(eq.ToHtml(coefs));
                EquationText = eq.ToString(coefs);
            }
            catch (Exception ex)
            {
                ErrorMessage = ex.ToString();
            }
        }