English 中文(简体)
C# 解决神学根源问题
原标题:C# application solving for quadratic imaginary roots

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,nmcBnmc/code>均为NumericUpDown控制,用于终端用户的a、b和c价值链投入。

因此,我希望进一步采取这一步骤,并可能解决想象价值问题。 考虑 我已经设定了条件,但我只需要说明原始价值,因为歧视者等于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上提供了复杂的数量支持。 我现在要检查这一情况。

问题回答

e.g. you are trying to calculate

(-b + sqrt(inside)) / (2*a)

<代码>Math.Sqrt >tn tnow about imaginary number, so it croaks if inside < 0. 但是,如果不改变答案,我们总是能够乘以1。 注 i2 页: 1

(-b + sqrt(inside * -1 * i**2)) / (2*a)
(-b + sqrt(-inside) * sqrt(i**2)) / (2*a)
(-b + sqrt(-inside) * i) / (2*a)
-b/(2*a) + sqrt(-inside)/(2*a) * i

因此,以下C#代码:

solution1String = (-b/(2*a)).ToString() +
                      " + " + (Math.Sqrt(-inside)/(2*a)).ToString() + " i";

So what is the problem that you think you might have? You are already checking for imaginery results. Just calculate accordingly - eg. perform the square root but of a positive value, and keep track of the real and imaginery parts.





相关问题
Bring window to foreground after Mutex fails

I was wondering if someone can tell me what would be the best way to bring my application to the foreground if a mutex was not able to be created for a new instance. E.g.: Application X is running ...

How to start WinForm app minimized to tray?

I ve successfully created an app that minimizes to the tray using a NotifyIcon. When the form is manually closed it is successfully hidden from the desktop, taskbar, and alt-tab. The problem occurs ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

Handle DataTable.DataRow cell change event

I have a DataTable that has several DataColumns and DataRow. Now i would like to handle an event when cell of this DataRow is changed. How to do this in c#?

Apparent Memory Leak in DataGridView

How do you force a DataGridView to release its reference to a bound DataSet? We have a rather large dataset being displayed in a DataGridView and noticed that resources were not being freed after the ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

WPF-XAML window in Winforms Application

I have a Winforms application coded in VS C# 2008 and want to insert a WPF window into the window pane of Winforms application. Could you explain me how this is done.

热门标签