C# 二次方程公式

C# 二次方程公式,c#,math,C#,Math,我试图制作一个程序,用一般公式计算二次方程的答案,但我遇到了一些错误。 我设置Windows窗体应用程序的方式要求a、b和c,并在通用公式中替换它们。 我有3个文本框,一个用于a、b和c的每个值,另一个用于答案,它应该与一个我称为“计算”的按钮一起工作。 我的问题是,当我尝试一个完美的三项式正方形以外的东西时,答案是NaN。 以下是我的一些代码: private void textBox1_TextChanged(object sender, EventArgs e) {

我试图制作一个程序,用一般公式计算二次方程的答案,但我遇到了一些错误。 我设置Windows窗体应用程序的方式要求a、b和c,并在通用公式中替换它们。 我有3个文本框,一个用于a、b和c的每个值,另一个用于答案,它应该与一个我称为“计算”的按钮一起工作。 我的问题是,当我尝试一个完美的三项式正方形以外的东西时,答案是NaN。 以下是我的一些代码:

private void textBox1_TextChanged(object sender, EventArgs e)
    {
        a = Double.Parse(textBox1.Text);
    }
^这就是我给变量赋值的方式

double sqrtpart = b * b - 4 * a * c;
        answer = (b + Math.Sqrt(sqrtpart)) / 2 * a;
        textBox4.Text = answer.ToString();

第2a组并确保值有效(b^2>4ac)


第2a组并确保值有效(b^2>4ac)


如果您希望您的计算器能够计算复数,请尝试以下方法

 string ans = "";
        double root1 = 0;
        double root2 = 0;
        double b = 0;
        double a = 0;
        double c = 0;
        double identifier = 0;


        a =Convert.ToDouble(Console.ReadLine());
        b = Convert.ToDouble(Console.ReadLine());
        c = Convert.ToDouble(Console.ReadLine());

        identifier = b * b - (4 * a * c);

        if (identifier > 0)
        {
            root1 = (-b+(Math.Sqrt(identifier)/(2*a)));
            root2 = (-b - (Math.Sqrt(identifier) / (2 * a)));
            string r1 = Convert.ToString(root1);
            string r2 = Convert.ToString(root2);
            ans = "Root1 =" + r1 + "Root2 = " + r2;
            Console.WriteLine(ans);
        }

        if (identifier < 0)
        {
            double Real = (-b / (2 * a));
            double Complex = ((Math.Sqrt((identifier*(-1.00))) / (2 * a)));
            string SReal = Convert.ToString(Real);
            string SComplex = Convert.ToString(Complex);
            ans = "Roots = " + SReal + "+/-" + SComplex + "i";
            Console.WriteLine(ans);
        }

        if (identifier == 0)
        {
            root1 = (-b / (2 * a));
            string Root = Convert.ToString(root1);
            ans = "Repeated roots : " + Root;
        }
string ans=”“;
双根1=0;
双根2=0;
双b=0;
双a=0;
双c=0;
双标识符=0;
a=Convert.ToDouble(Console.ReadLine());
b=Convert.ToDouble(Console.ReadLine());
c=Convert.ToDouble(Console.ReadLine());
标识符=b*b-(4*a*c);
如果(标识符>0)
{
root1=(-b+(Math.Sqrt(标识符)/(2*a));
root2=(-b-(Math.Sqrt(identifier)/(2*a));
字符串r1=Convert.ToString(root1);
字符串r2=Convert.ToString(root2);
ans=“Root1=“+r1+”Root2=“+r2;
控制台写入线(ans);
}
如果(标识符<0)
{
双实数=(-b/(2*a));
双复数=((Math.Sqrt((identifier*(-1.00))/(2*a));
字符串SReal=Convert.ToString(实数);
字符串SComplex=Convert.ToString(复数);
ans=“Roots=“+SReal+”++/-”+SComplex+“i”;
控制台写入线(ans);
}
如果(标识符==0)
{
root1=-b/(2*a));
字符串根=Convert.ToString(root1);
ans=“重复根:”+根;
}

如果您希望您的计算器能够计算复数,请尝试以下方法

 string ans = "";
        double root1 = 0;
        double root2 = 0;
        double b = 0;
        double a = 0;
        double c = 0;
        double identifier = 0;


        a =Convert.ToDouble(Console.ReadLine());
        b = Convert.ToDouble(Console.ReadLine());
        c = Convert.ToDouble(Console.ReadLine());

        identifier = b * b - (4 * a * c);

        if (identifier > 0)
        {
            root1 = (-b+(Math.Sqrt(identifier)/(2*a)));
            root2 = (-b - (Math.Sqrt(identifier) / (2 * a)));
            string r1 = Convert.ToString(root1);
            string r2 = Convert.ToString(root2);
            ans = "Root1 =" + r1 + "Root2 = " + r2;
            Console.WriteLine(ans);
        }

        if (identifier < 0)
        {
            double Real = (-b / (2 * a));
            double Complex = ((Math.Sqrt((identifier*(-1.00))) / (2 * a)));
            string SReal = Convert.ToString(Real);
            string SComplex = Convert.ToString(Complex);
            ans = "Roots = " + SReal + "+/-" + SComplex + "i";
            Console.WriteLine(ans);
        }

        if (identifier == 0)
        {
            root1 = (-b / (2 * a));
            string Root = Convert.ToString(root1);
            ans = "Repeated roots : " + Root;
        }
string ans=”“;
双根1=0;
双根2=0;
双b=0;
双a=0;
双c=0;
双标识符=0;
a=Convert.ToDouble(Console.ReadLine());
b=Convert.ToDouble(Console.ReadLine());
c=Convert.ToDouble(Console.ReadLine());
标识符=b*b-(4*a*c);
如果(标识符>0)
{
root1=(-b+(Math.Sqrt(identifier)/(2*a));
root2=(-b-(Math.Sqrt(identifier)/(2*a));
字符串r1=Convert.ToString(root1);
字符串r2=Convert.ToString(root2);
ans=“Root1=“+r1+”Root2=“+r2;
控制台写入线(ans);
}
如果(标识符<0)
{
双实数=(-b/(2*a));
双复数=((Math.Sqrt((identifier*(-1.00))/(2*a));
字符串SReal=Convert.ToString(实数);
字符串SComplex=Convert.ToString(复数);
ans=“Roots=“+SReal+”++/-”+SComplex+“i”;
控制台写入线(ans);
}
如果(标识符==0)
{
root1=-b/(2*a));
字符串根=Convert.ToString(root1);
ans=“重复根:”+根;
}

首先,确保4ac
double sqrtpart = (b * b) - (4 * a * c);
answer1 = ((-1)*b + Math.Sqrt(sqrtpart)) / (2 * a);
answer2 = ((-1)*b - Math.Sqrt(sqrtpart)) / (2 * a);
textBox4.Text = answer1.ToString() + " and " + answer2.ToString();

首先,确保4ac
double sqrtpart = (b * b) - (4 * a * c);
answer1 = ((-1)*b + Math.Sqrt(sqrtpart)) / (2 * a);
answer2 = ((-1)*b - Math.Sqrt(sqrtpart)) / (2 * a);
textBox4.Text = answer1.ToString() + " and " + answer2.ToString();

使用以下代码:

using System;
using System.Collections.Generic;
using System.Text;

namespace SoftwareAndFinance
{
    class Math
    {

            // quadratic equation is a second order of polynomial       equation in       a single variable 
        // x = [ -b +/- sqrt(b^2 - 4ac) ] / 2a
        public static void SolveQuadratic(double a, double b, double c)
        {
            double sqrtpart = b * b - 4 * a * c;
            double x, x1, x2, img;
            if (sqrtpart > 0)
            {
                x1 = (-b + System.Math.Sqrt(sqrtpart)) / (2 * a);
                x2 = (-b - System.Math.Sqrt(sqrtpart)) / (2 * a);
                Console.WriteLine("Two Real Solutions: {0,8:f4} or  {1,8:f4}", x1, x2);
            }
            else if (sqrtpart < 0)
            {
                sqrtpart = -sqrtpart;
                x = -b / (2 * a);
                img = System.Math.Sqrt(sqrtpart) / (2 * a);
                Console.WriteLine("Two Imaginary Solutions: {0,8:f4} + {1,8:f4} i or {2,8:f4} + {3,8:f4} i", x, img, x, img);
            }
            else
            {
                x = (-b + System.Math.Sqrt(sqrtpart)) / (2 * a);
                Console.WriteLine("One Real Solution: {0,8:f4}", x);
            }
        }


        static void Main(string[] args)
        {

            // 6x^2 + 11x - 35 = 0
            SolveQuadratic(6, 11, -35);

            // 5x^2 + 6x + 1 = 0
            SolveQuadratic(5, 6, 1);

            // 2x^2 + 4x + 2 = 0
            SolveQuadratic(2, 4, 2);

            // 5x^2 + 2x + 1 = 0
            SolveQuadratic(5, 2, 1);
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用系统文本;
名称空间软件与金融
{
课堂数学
{
//二次方程是单变量多项式方程的二阶
//x=[-b+/-sqrt(b^2-4ac)]/2a
公共静态无效(双a、双b、双c)
{
双sqrtpart=b*b-4*a*c;
双x,x1,x2,img;
如果(sqrtpart>0)
{
x1=(-b+系统数学Sqrt(sqrtpart))/(2*a);
x2=(-b-系统数学Sqrt(sqrtpart))/(2*a);
WriteLine(“两个实解:{0,8:f4}或{1,8:f4}”,x1,x2);
}
否则如果(sqrtpart<0)
{
sqrtpart=-sqrtpart;
x=-b/(2*a);
img=System.Math.Sqrt(sqrtpart)/(2*a);
WriteLine(“两个假想解:{0,8:f4}+{1,8:f4}i或{2,8:f4}+{3,8:f4}i”,x,img,x,img);
}
其他的
{
x=(-b+系统数学Sqrt(sqrtpart))/(2*a);
WriteLine(“一个真正的解决方案:{0,8:f4}”,x);
}
}
静态void Main(字符串[]参数)
{
//6x^2+11x-35=0
二次型(6,11,-35);
//5x^2+6x+1=0
二次型(5,6,1);
//2x^2+4x+2=0
二次型(2,4,2);
//5x^2+2x+1=0
二次型(5,2,1);
}
}
}

这是原件。

使用以下代码:

using System;
using System.Collections.Generic;
using System.Text;

namespace SoftwareAndFinance
{
    class Math
    {

            // quadratic equation is a second order of polynomial       equation in       a single variable 
        // x = [ -b +/- sqrt(b^2 - 4ac) ] / 2a
        public static void SolveQuadratic(double a, double b, double c)
        {
            double sqrtpart = b * b - 4 * a * c;
            double x, x1, x2, img;
            if (sqrtpart > 0)
            {
                x1 = (-b + System.Math.Sqrt(sqrtpart)) / (2 * a);
                x2 = (-b - System.Math.Sqrt(sqrtpart)) / (2 * a);
                Console.WriteLine("Two Real Solutions: {0,8:f4} or  {1,8:f4}", x1, x2);
            }
            else if (sqrtpart < 0)
            {
                sqrtpart = -sqrtpart;
                x = -b / (2 * a);
                img = System.Math.Sqrt(sqrtpart) / (2 * a);
                Console.WriteLine("Two Imaginary Solutions: {0,8:f4} + {1,8:f4} i or {2,8:f4} + {3,8:f4} i", x, img, x, img);
            }
            else
            {
                x = (-b + System.Math.Sqrt(sqrtpart)) / (2 * a);
                Console.WriteLine("One Real Solution: {0,8:f4}", x);
            }
        }


        static void Main(string[] args)
        {

            // 6x^2 + 11x - 35 = 0
            SolveQuadratic(6, 11, -35);

            // 5x^2 + 6x + 1 = 0
            SolveQuadratic(5, 6, 1);

            // 2x^2 + 4x + 2 = 0
            SolveQuadratic(2, 4, 2);

            // 5x^2 + 2x + 1 = 0
            SolveQuadratic(5, 2, 1);
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用系统文本;
名称空间软件与金融
{
课堂数学
{
//二次方程是单变量多项式方程的二阶
//x=[-b+/-sqrt(b^2-4ac)]/2a
公共静态无效(双a、双b、双c)
{
双sqrtpart=b*b-4*a*c;
双x,x1,x2,img;
如果(sqrtpart>0)