-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

Lines of this form are suspect;

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

as you will be calling sqrt on a negative.

You should check the real case, for example, by doing this:

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

I've also dropped the unnecessary brackets. * has higher precedence than -. Only call the sqrt function on a non-negative parameter.

于 2013-09-11T14:41:52.650 回答
0

您不知道数值分析的一个方面。假设你有二次方程

x^2 - 100 x + 1 = 0.

这两种解决方案是(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 回答
0

if (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 回答