Ejemplo n.º 1
0
        /// <summary>
        /// x^2 + x + C = 0
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <param name="c"></param>
        /// <returns>Object with both values</returns>
        public Qaudratic SolveQuadratic(decimal a, decimal b, decimal c)
        {
            decimal derivitive     = (b * b) - (Convert.ToDecimal(4) * a * c);
            decimal rootDerivitive = Utils.SquareRoot(derivitive);

            Qaudratic quad = new Qaudratic();

            if (rootDerivitive <= 0)
            {
                quad.success = false;
            }
            else
            {
                decimal x1 = (-b + rootDerivitive) / (Convert.ToDecimal(2) * a);
                decimal x2 = (-b - rootDerivitive) / (Convert.ToDecimal(2) * a);
                quad.success = true;
                quad.answer1 = Math.Round(x1, 3);
                quad.answer2 = Math.Round(x2, 3);
            }
            return(quad);
        }
Ejemplo n.º 2
0
        public static string GetTime(string distance, string initialVelocity, string acceleration)
        {
            decimal s = -Convert.ToDecimal(distance);
            decimal U = Convert.ToDecimal(initialVelocity);
            decimal a = Convert.ToDecimal(acceleration);

            if (a == 0)
            {
                decimal answer = -s / U;
                return($"{answer} s");
            }
            else
            {
                UtilsNS   helper = new UtilsNS();
                Qaudratic quad   = helper.SolveQuadratic(a, U, s);
                if (quad.success == false)
                {
                    return("Result is Imaginary");
                }
                else
                {
                    if (quad.answer1 == quad.answer2)
                    {
                        return($"{quad.answer1} s");
                    }
                    else if (quad.answer1 <= 0)
                    {
                        return($"{quad.answer2} s");
                    }
                    else if (quad.answer2 <= 0)
                    {
                        return($"{quad.answer1} s");
                    }
                    else
                    {
                        return($"{quad.answer1} s OR {quad.answer2} s");
                    }
                }
            }
        }