static void Main(string[] args) { Console.WriteLine("ax^2 + bx + c = 0"); Console.Write("Enter a: "); var aString = Console.ReadLine(); double a = InputValidation.CheckValidity(aString); Console.Write("Enter b: "); var bString = Console.ReadLine(); double b = InputValidation.CheckValidity(bString); Console.Write("Enter c: "); var cString = Console.ReadLine(); double c = InputValidation.CheckValidity(cString); var discriminant = SquareEquationMath.Discriminant(a, b, c); Console.WriteLine($"Discriminant = {discriminant}"); if (discriminant == 0) { var firstRoot = SquareEquationMath.FirstRoot(discriminant, a, b); Console.WriteLine("Equation has 1 root"); Console.WriteLine($"x1 = {firstRoot:F2}"); Console.WriteLine($"{a}x^2 + {b}x + {c} = {a}*(x - {firstRoot:F2})*(x - {firstRoot:F2})"); } if (discriminant > 0) { var firstRoot = SquareEquationMath.FirstRoot(discriminant, a, b); var secondRoot = SquareEquationMath.SecondRoot(discriminant, a, b); Console.WriteLine("Equation has 2 roots"); Console.WriteLine($"x1 = {firstRoot:F2}"); Console.WriteLine($"x2 = {secondRoot:F2}"); Console.WriteLine($"{a}x^2 + {b}x + {c} = {a}*(x - {firstRoot:F2})*(x - {secondRoot:F2})"); } if (discriminant < 0) { Console.WriteLine("Equation has no roots"); } Console.ReadKey(); }
private void btnCalculate_Click(object sender, EventArgs e) { var stringA = txtA.Text; var stringB = txtB.Text; var stringC = txtC.Text; double a; if (double.TryParse(stringA, out a) == false) { MessageBox.Show("Supplied value 'a' is not a number, please try again"); } double b; if (double.TryParse(stringB, out b) == false) { MessageBox.Show("Supplied value 'b' is not a number, please try again"); } double c; if (double.TryParse(stringC, out c) == false) { MessageBox.Show("Supplied value 'c' is not a number, please try again"); } var discriminant = SquareEquationMath.Discriminant(a, b, c); if (discriminant < 0) { MessageBox.Show("D < 0, equation has no roots!"); } var firstRoot = SquareEquationMath.FirstRoot(discriminant, a, b); var secondRoot = SquareEquationMath.SecondRoot(discriminant, a, b); lblDiscriminant.Text = discriminant.ToString(); lblFirstRoot.Text = firstRoot.ToString(); lblSecondRoot.Text = secondRoot.ToString(); lblPolyFactor.Text = $"{a}x² + {b}x + {c} = {a}*(x - {firstRoot:F2})*(x - {secondRoot:F2})"; }