I created an extremely simple, but fully functional and very useful WinForms C # application that solves the real roots of the quadratic equation.
Here is my current programming logic:
string noDivideByZero = "Enter an a value that isn't 0";
txtSolution1.Text = noDivideByZero;
txtSolution2.Text = noDivideByZero;
decimal aValue = nmcA.Value;
decimal bValue = nmcB.Value;
decimal cValue = nmcC.Value;
decimal solution1, solution2;
string solution1String, solution2String;
decimal insideSquareRoot = (bValue * bValue) - 4 * aValue * cValue;
if (insideSquareRoot < 0)
{
solution1String = "No real solutions!";
solution2String = "No real solutions!";
txtSolution1.Text = solution1String;
txtSolution2.Text = solution2String;
}
else if (insideSquareRoot == 0)
{
decimal sqrtOneSolution = (decimal)Math.Sqrt((double)insideSquareRoot);
solution1 = (-bValue + sqrtOneSolution) / (2 * aValue);
solution2String = "No real solution!";
txtSolution1.Text = solution1.ToString();
txtSolution2.Text = solution2String;
}
else if (insideSquareRoot > 0)
{
decimal sqrtTwoSolutions = (decimal)Math.Sqrt((double)insideSquareRoot);
solution1 = (-bValue + sqrtTwoSolutions) / (2 * aValue);
solution2 = (-bValue - sqrtTwoSolutions) / (2 * aValue);
txtSolution1.Text = solution1.ToString();
txtSolution2.Text = solution2.ToString();
}
txtSolution1and txtSolution2- these are text fields that cannot accept input, but display calculation results
nmcA, nmcBand nmcCare the NumericUpDown elements that are used to enter the values a, b, and c by the end user
Well, that’s why I was hoping to take another step and possibly solve the imaginary values. Given that I have already established conditional conditions, I will need to consider imaginary values only when the discriminant is equal to 0or less 0.
, . , , i. i = sqroot(-1) i^2 = -1.
- , , ?
Googling, , # 4.0 ( .NET 4.0, , ) System.Numerics.Complex. .