Exemplo n.º 1
0
        public void Solve()
        {
            double a, b, c;

            if (!DegreeCheck())
            {
                return;
            }
            if (Degree == 0)
            {
                SolutionType = SolutionTypes.Any;
                return;
            }

            EquationParser.SetCoefficients(out a, out b, out c, ref _equation);
            Steps.Add($"[Reading coefficients]\t\ta = {a}, b = {b}, c = {c}");

            if (a == 0)
            {
                if (c == 0)
                {
                    Roots.Add("0");
                }
                else
                {
                    SolveLinear(b, c);
                }

                return;
            }

            Discriminant = b * b - 4 * a * c;
            Steps.Add($"[Calculating discriminant]\tD = b^2 - 4ac = {b}^2 - 4 * {a} * {c} = {Discriminant}");
            if (Discriminant >= 0)
            {
                SolveQuadratic(a, b);
            }
            else
            {
                SolveComplex(a, b);
            }
        }
Exemplo n.º 2
0
        private bool DegreeCheck()
        {
            var match = Regex.Match(_equation, @"x\^\-");

            if (match.Success)
            {
                throw new Exception("reduced form should not contain negative powers of x.\n" + _equation + "\n" +
                                    "^".PadLeft(match.Index + 1));
            }

            Degree = EquationParser.GetPolynomialDegree(_equation);

            if (Degree < 0 || Degree > 2)
            {
                throw new Exception("polynomial degree should be in range of [0, 2].");
            }
            if (Degree == 0 && (_equation.Length != 3 || _equation[0] != _equation[2]))
            {
                SolutionType = SolutionTypes.None;
                return(false);
            }

            return(true);
        }