2

最初の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();
            }
        }
    }
}
4

3 に答える 3

21

初めての C# プログラムを書きました。

素晴らしい。今こそ、悪い習慣をやめるための絶好の機会です。

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 を配置するのではなく、 を使用します。

次に、コメントがコードのアクションと一致しません。このコードは、結果が整数かどうかを判別します。コメントには、ゼロをチェックすると書かれています。

第三に、表現しようとしているものがループである場合は、goto を使用しないでください。

第 4 に、重複したコードをすべて見てください。同じコードをわずかに変更して 3 回繰り返します。

自分自身をヘルパー メソッドにします。

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

integersで演算を行ってから、double変換します。つまり、除算を行い、最も近い整数に丸めてから double に変換します。最初から double で (または、可能性は低いですが、decimal で) 実行します。そのはず:

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

つまり、a、b、および c は整数であってはなりません。

于 2012-04-20T20:26:22.223 に答える
3

x1 と x2 に割り当てるときに整数除算を行っています。(2 を 2.0 に変更して、2 倍の除算に変更し、2 倍の結果を得ることができます)

a、b、c、および d の値を double に変更して問題を回避し、係数に int 以外の値を入力できるようにすることも理にかなっています。

于 2012-04-20T20:01:35.633 に答える
3

int a、b、c; int d;

まず、整数を使用すると 1/3 = 0 になるため、int の代わりに double を使用してみてください。

于 2012-04-20T20:02:38.873 に答える