c# - 在使用 C# 二次方程求解器时遇到问题

标签 c# rounding

我刚刚编写了我的第一个 C# 程序。

这是一段求解二次方程的简单代码。

它与某些函数(例如 -6x2-6x+12)完美配合,而与其他函数(4x2-20x+25)配合使用时,我怀疑是舍入误差。

我是 C# 的新手,看不出任何问题;有人能帮我调试这段代码吗?

namespace ConsoleApplication {
    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, x1, x2;

            while (true) {
                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 = -b / (2 * a);
                Console.WriteLine("The only solution is x={0}.", x1);
                Console.ReadLine();
            }

            // If d < 0, no real solutions exist
            else if (d < 0) { 
                Console.WriteLine("There are no real solutions");
                Console.ReadLine();
            }

            // If d > 0, there are two real solutions 
            else {
                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.

太棒了。现在是不要养成坏习惯的好时机:

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;             
} 

问题比比皆是。首先,使用 int.TryParse,而不是在可能失败的地方放置 try-catch。

第二,注释与代码的 Action 不符。该代码确定结果是否为整数;评论说它检查零。

第三,当你试图表示的是一个循环时,不要使用 goto。

第四,看看那些重复的代码!您将相同的代码重复了三次,但略有不同。

让自己成为一个辅助方法:

 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!");
     }
 }

现在你的主线是:

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

你的错误在这里:

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

您在整数中进行算术运算,然后转换为 double 。也就是说,您执行除法,四舍五入到最接近的整数,然后转换为 double 。从一开始就以 double (或者,不太可能,以小数)进行。应该是:

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

也就是说,a、b 和 c 不应该是整数。

关于c# - 在使用 C# 二次方程求解器时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10252740/

相关文章:

c# - asp.net SERVER_NAME 返回错误的域名

c# - DLL 搜索 DLL

c# - 在 LINQ 选择中使用公式

c# - 下载文件时显示自定义对话框

java - 带偏移量的 double 最接近的倍数 java

c# - 日期时间转换异常

c++ - float 到字符串而不四舍五入

python - Round 在 Python 中向下 float 以仅保留一位非零小数

sql-server - SQL Server 按每小时日期时间计数进行分组?

php float 计算2位小数