Having trouble with C# quadratic equation solver

I just wrote my first C# program.

It's a simple piece of code which solves quadratic equations.

It works with some functions (such as -6x2-6x+12) perfectly, while with others, (4x2-20x+25) it exhibits what I suspect are rounding errors.

I'm completely new to C#, and I can't see an problems; would someone be able to help me debug this code?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication7
{
    class Program
    {
        static void Main(string[] args)
        {
            int a, b, c;
            int d;
            double x1, x2;
            entA: Console.Write("a?");
            try
            {
                a = Convert.ToInt32(Console.ReadLine());
            }
            catch
            {
                /*If a=0, the equation isn't quadratic*/
                Console.WriteLine("Invalid input");
                goto entA;
            }
            entB: Console.Write("b?");
            try
            {
                b = Convert.ToInt32(Console.ReadLine());
            }
            catch
            {
                Console.WriteLine("Invalid input");
                goto entB;
            }
            entC: Console.Write("c?");
            try
            {
                c = Convert.ToInt32(Console.ReadLine());
            }
            catch
            {
                Console.WriteLine("Invalid input");
                goto entC;
            }
            /*Calculating a discriminant*/
            d = b * b - 4 * a * c;
            if (d == 0)
            {
                x1 = x2 = -b / (2 * a);
                Console.WriteLine("The only solution is x={0}.", x1);
                Console.ReadLine();
            }
            else if (d < 0) /*If d<0, no solutions are possible*/
            { 
                Console.WriteLine("There are no possible solutions");
                Console.ReadLine();
            }
            else /*If d>0, there are two possible solutions*/
            {
                x1 = (-b - Math.Sqrt(d)) / (2 * a);
                x2 = (-b + Math.Sqrt(d)) / (2 * a);
                Console.WriteLine("x1={0} and x2={1}.", x1, x2);
                Console.ReadLine();
            }
        }
    }
}

Here's the updated code:

    namespace ConsoleApplication7
    {
    class Program
    {
        static int ObtainInput(string prompt, bool canBeZero)
        {
            double a = ObtainInput("A? ", false);
            double b = ObtainInput("B? ", true);
            double c = ObtainInput("C? ", true);
            double d;
            double x1, x2;
            while (true) // loop forever!
            {
                Console.Write(prompt);
                string input = Console.ReadLine();
                int result;
                bool success = int.TryParse(input, out result);
                if (success && (canBeZero || result != 0))
                    return result;
                Console.WriteLine("Invalid input!");
            }
            /Calculating a discriminant/
            d = b * b - 4 * a * c;
            if (d == 0)
            {
                x1 = x2 = -b / (2 * a);
                Console.WriteLine("The only solution is x={0}.", x1);
                Console.ReadLine();
            }
            else if (d < 0) /If d<0, no solutions are possible/
            { 
                Console.WriteLine("There are no possible solutions");
                Console.ReadLine();
            }
            else /If d>0, there are two possible solutions/
            {
                x1 = (-b - Math.Sqrt(d)) / (2 * a);
                x2 = (-b + Math.Sqrt(d)) / (2 * a);
                Console.WriteLine("x1={0} and x2={1}.", x1, x2);
                Console.ReadLine();
            }
        }
    }
    }
    


I just wrote my first C# program.

Awesome. Now would be a great time to not get into bad habits:

entA: Console.Write("a?");   
try { a = Convert.ToInt32(Console.ReadLine()); }
catch 
{ /*If a=0, the equation isn't quadratic*/
  Console.WriteLine("Invalid input"); 
  goto entA;             
} 

Problems abound. First off, use int.TryParse , rather than putting a try-catch around something that can fail.

Second, the comment does not match the action of the code. The code determines if the result is an integer; the comment says that it checks for zero.

Third, do not use a goto when what you are attempting to represent is a loop.

Fourth, look at all that duplicated code! You have the same code repeated three times with minor variations.

Make yourself a helper method:

 static int ObtainInput(string prompt, bool canBeZero)
 {
     while(true) // loop forever!
     {
         Console.Write(prompt);
         string input = Console.ReadLine();
         int result;
         bool success = int.TryParse(input, out result);
         if (success && (canBeZero || result != 0))
             return result;
         Console.WriteLine("Invalid input!");
     }
 }

And now your mainline is:

int a = ObtainInput("A? ", false);
int b = ObtainInput("B? ", true);
int c = ObtainInput("C? ", true);

Your bug though is here:

x1 = x2 = -b / (2 * a);   

You do the arithmetic in integers, and then convert to doubles. That is, you do the division, round to the nearest integer, and then convert to double. Do it in doubles (or, less likely, in decimals) from the start. It should be:

double a = ObtainInput("A? ", false);
double b = ObtainInput("B? ", true);
double c = ObtainInput("C? ", true);

That is, a, b, and c should not ever be integers.


You're doing integer division when assigning to x1 and x2; (you can just change the 2 to 2.0 to change it to double division and get a double result)

It might also make sense to change your a,b,c, and d values to double which will also get past the problem, and allow people to enter non-int values for the coefficients.


int a, b, c; int d;

first of all, try to use double instead of int, since 1/3 = 0 using integers.

链接地址: http://www.djcxy.com/p/52778.html

上一篇: Unity3D C#脚本:类变量数组数据问题

下一篇: C#二次方程求解器遇到问题