-1

C++ プログラミングについてもっと学ぼうとしていますが、プログラムに問題があります。このプログラムは単純ですが、エラーが発生しました。ポインターに関係していると思われます。コードを何度も修正しようとしましたが、私が書いたものに問題は見つかりませんでした。問題を解決する方法についてのヘルプまたは指示をいただければ幸いです。

// The 'main' function for a program to test your function
// 'quadRoots'.
//===============================================================
#include <iostream>

int  quadRoots(double a,double b, double c,double* r1,double* r2);
void printRoots(int nr,double* r1,double* r2);

using namespace std;

int main()
{
double root1[2], root2[2];
int nRoots;

// example with real roots
nRoots = quadRoots(1.0, 3.3, 2.1, root1, root2);
printRoots(nRoots,root1,root2);

// example with complex roots
nRoots = quadRoots(1.0, 3.3, 5.1, root1, root2);
printRoots(nRoots,root1,root2);

// example with real roots, one zero
nRoots = quadRoots(1.0, 3.3, 0.0, root1, root2);
printRoots(nRoots,root1,root2);

// example of a linear function that should produce 1 root
nRoots = quadRoots(0.7-1.0+0.3, 3.3, 2.1, root1, root2);
printRoots(nRoots,root1,root2);

// example that has no solutions
nRoots = quadRoots(0.7-1.0+0.3, 0.0, 5.5, root1, root2);
printRoots(nRoots,root1,root2);

cout << "Press Enter key to quit" << endl;
char qq = cin.get();

return(0);
}

void printRoots(int nr,double* r1,double* r2)
{
if(nr == 0){
    cout << "No Roots" << endl << endl;
}
else if(nr == 1){
    cout << "Root 1: " << r1[0] << endl << endl;
}
else if(fabs(r1[1]) < 0.0000001){   // print real roots
    cout << "Root 1: " << r1[0] << endl;
    cout << "Root 2: " << r2[0] << endl << endl;
}
else{  // print complex roots
    if(fabs(r1[1]+r2[1]) > 0.00001){
        cout << "Something is wrong: complex roots not in conjugate pairs."         << endl;
    }
    else{
        cout << "Root 1: " << r1[0] << " + " << fabs(r1[1]) << " i" << endl;
        cout << "Root 2: " << r2[0] << " - " << fabs(r2[1]) << " i" << endl << endl;
    }
}
}
int quadRoots(double a,double b,double c,double* r1,double* r2)
{
if ( a > 0 ){
    if ( sqrt((b*b) - 4*a*c) > 0 ){
        r1[0] = (-b + (sqrt((b*b) - 4*a*c))) / (2 *a);
        r2[0] = (-b - (sqrt((b*b) - 4*a*c))) / (2 *a);
        return (2);
    }
    else if (sqrt((b*b) - 4*a*c) == 0 ) {
        r1[0] =  (-b )/(2 *a);
        return (1);
    }
    else if (sqrt((b*b) - 4*a*c) < 0 ) {
        r1[1] = (-b + (-(sqrt(-(b*b) - 4*a*c)))) / (2 *a);
        r2[1] = (-b - (-(sqrt(-(b*b) - 4*a*c)))) / (2 *a);
        return (2);
    }
}
else if (b == 0 ){
    r1[0] = r2[0] = 0;
    return (1);
}
else {
    return (0);
}
}
4

3 に答える 3

3

この形式の行は疑わしいものです。

 if ( sqrt((b*b) - 4*a*c) > 0 )

負の値で sqrt を呼び出すためです。

たとえば、次のようにして、実際のケースを確認する必要があります。

if (b*b - 4*a*c > 0)

不要なブラケットも削除しました。*より優先度が高い-sqrt負でないパラメーターでのみ関数を呼び出します。

于 2013-09-11T14:41:52.650 に答える
0

(b*b - 4*a*c) < 0 の場合、ルートの実部を忘れていますか?

なぜ係数「a」をテストするのですか?

http://en.wikipedia.org/wiki/Quadratic_equation これがお役に立てば幸いです。

PS:これを試してください

int quadRoots(double a,double b,double c,double* r1,double* r2)
{
if ( a != 0 ){
    // 2nd order equation
    if ( b*b - 4*a*c > 0 ){
        r1[0] = (-b + sqrt(b*b - 4*a*c)) / (2 *a);
        r2[0] = (-b - sqrt(b*b - 4*a*c)) / (2 *a);
        r1[1] = 0;
        r2[1] = 0;
        return (2);
    }
    else if ( b*b - 4*a*c == 0 ) {
        r1[0] =  (-b )/(2 *a);
        r2[0] =  0;
        r1[1] =  0;
        r2[1] =  0;
        return (1);
    }
    else {
        // ( b*b - 4*a*c IS < 0 )
        // complex roots
        r1[0] = -b / (2 *a);
        r2[0] = -b / (2 *a);

        r1[1] = sqrt( 4*a*c - b*b) / (2 *a);
        r2[1] = -r1[1];
        return (2);
    }
}
else if (b != 0 ){
    // firts order equation
    r[0] = -c/b;
    return (1);
}
else {
    // a == 0 && b == 0
    // c = 0 ???
    return (0);
}
于 2013-09-11T15:00:55.040 に答える
0

あなたが気付いていない数値解析の 1 つの側面があります。次の二次方程式があるとします。

x^2 - 100 x + 1 = 0.

2つのソリューションは(100 ± √9996)/2. 負の符号を持つルートを考えてみましょう: (100 - √9996)/2. ここ。お互いに非常に近いです100。減算により、有効桁数√9996が失われます。4代わりに、次の事実を使用できます。

(-b ± √(b^2-4ac))/(2a) = 2c/(-b ∓ √(b^2-4ac)).

次に、ほぼ同じ数を減算しないように符号を選択​​します。bが正の場合-bは負なので、マイナス記号を選択します。が負の場合bは、プラス記号を選択します。

于 2013-09-11T17:09:52.710 に答える