I have constructed an extremely simple, yet fully-functioning and quite helpful, WinForms C# application that solves for the real roots of a quadratic equation.
我目前的方案拟订逻辑是:
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;
//Quadratic Formula: x = (-b +- sqrt(b^2 - 4ac)) / 2a
//Calculate discriminant
decimal insideSquareRoot = (bValue * bValue) - 4 * aValue * cValue;
if (insideSquareRoot < 0)
{
//No real solution
solution1String = "No real solutions!";
solution2String = "No real solutions!";
txtSolution1.Text = solution1String;
txtSolution2.Text = solution2String;
}
else if (insideSquareRoot == 0)
{
//One real solution
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)
{
//Two real solutions
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();
}
<代码>txtSolution1和txtSolution2
为允许接收投入的正本箱,但产出计算结果
<代码>nmcA,nmcB
和nmc
因此,我希望进一步采取这一步骤,并可能解决想象价值问题。 考虑 我已经设定了条件,但我只需要说明原始价值,因为歧视者等于0
。 或小于<代码>0。
然而,我不能认为这样做有好办法。 复杂的解决办法是在有人试图把负数的方括号扎根,导致出现在各地的<代码>i。 i = sqroot(-1)
and i^2 = -1
。
Does anyone know how to approach this problem, or if it is just not worth the time?
http://www.ohchr.org。
略微 最后,我发现,C# 4.0(或 NET 4.0 Im 不能确定)在<代码>System.Numerics.Complex上提供了复杂的数量支持。 我现在要检查这一情况。