我该如何解决代码中的计算问题?

时间:2020-04-18 14:17:49

标签: c# visual-studio

我写了一个代码,使用class计算二阶方程的根。 当我为某些输入运行它时,它给了我正确的答案,而对于其他输入,它给出了错误的答案。 这是我的代码:

using System;

namespace file
{
    class Roots
    {
        public double a=0;
        public double b=0;
        public double c=0;
        public double x1;
        public double x2;
    }
    class Program
    {
        static void Main(string[] args)
        {
            Roots roots = new Roots();
            double thesquareroot;
            double delta;

            Console.WriteLine("For calculating the roots of the second order equation(ax^2+bx+c),Please enter");
            Console.WriteLine("a: ");
             roots.a = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("b: ");
            roots.b = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("c: ");
            roots.c = Convert.ToDouble(Console.ReadLine());


           if (Convert.ToBoolean(roots.a == 0))
            {
                Console.WriteLine("Error,This is not a second order equation.");
            }
           else if (Convert.ToBoolean(roots.a != 0))
            {
                delta = (roots.b * roots.b - 4 * roots.a * roots.c);
                thesquareroot = Math.Sqrt(delta);

                if (thesquareroot < 0)
                {
                    Console.WriteLine("Error,No real roots available for this equation.");
                }
                else
                {
                    roots.x1 = ((-roots.b + thesquareroot) / 2 * roots.a);
                    roots.x2 = ((-roots.b - thesquareroot) / 2 * roots.a);

                    Console.WriteLine("x1= " + roots.x1);
                    Console.WriteLine("x2= " + roots.x2);
                }
           }


        }
    }
}

2 个答案:

答案 0 :(得分:4)

您需要检查delta < 0而不是thesquareroot < 0

这是一个解决方案(带有一些封装):

static void Main()
{
  Roots roots = new Roots();

  Console.WriteLine("For calculating the roots of the second order equation(ax^2+bx+c),Please enter");
  Console.WriteLine("a: ");
  roots.a = Convert.ToDouble(Console.ReadLine());
  Console.WriteLine("b: ");
  roots.b = Convert.ToDouble(Console.ReadLine());
  Console.WriteLine("c: ");
  roots.c = Convert.ToDouble(Console.ReadLine());


  if (Convert.ToBoolean(roots.a == 0))
  {
    Console.WriteLine("Error,This is not a second order equation.");
  }
  else if (Convert.ToBoolean(roots.a != 0))
  {
    roots.Print();
  }
}

class Roots
{
  public double a { get; set; } = 0;
  public double b { get; set; } = 0;
  public double c { get; set;} = 0;
  public double x1 => (-b + Math.Sqrt(Delta)) / (2*a);
  public double x2 => (-b - Math.Sqrt(Delta)) / (2*a);

  public double Delta => b *b - 4 * a * c;

  public void Print()
  {
    if(Delta < 0)
    {
      Console.WriteLine("Error,No real roots available for this equation.");
    }
    else
    {
      Console.WriteLine("x1= " + x1);
      Console.WriteLine("x2= " + x2);
    }

  }
}

答案 1 :(得分:1)

您必须检查delta是否为负。计算根时,您需要在2 * a周围加上括号。因此正确的代码将是:

 class Roots
    {
        public double a = 0;
        public double b = 0;
        public double c = 0;
        public double x1;
        public double x2;
    }

    class Program
    {
        static void Main(string[] args)
        {
            Roots roots = new Roots();
            double thesquareroot;
            double delta;
            Console.WriteLine("For calculating the roots of the second order equation(ax^2+bx+c),Please enter");
            Console.WriteLine("a: ");
            roots.a = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("b: ");
            roots.b = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("c: ");
            roots.c = Convert.ToDouble(Console.ReadLine());


            if (roots.a.Equals(0.0))
            {
                Console.WriteLine("Error,This is not a second order equation.");
            }
            else
            {
                delta = roots.b * roots.b - 4 * roots.a * roots.c;

                if (delta < 0)
                {
                    Console.WriteLine("Error,No real roots available for this equation.");
                }
                else
                {
                    thesquareroot = Math.Sqrt(delta);
                    roots.x1 = (-roots.b + thesquareroot) / (2 * roots.a);
                    roots.x2 = (-roots.b - thesquareroot) / (2 * roots.a);

                    Console.WriteLine("x1= " + roots.x1);
                    Console.WriteLine("x2= " + roots.x2);
                }
            }
        }
    }