二次方程程序 我正在努力学习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); } } //用于测试函数的程序的“main”函数 //“四根”。 //=============================================================== #包括 int四根(双a,双b,双c,双*r1,双*r2); 无效打印根(整数编号,双*r1,双*r2); 使用名称空间std; int main() { 双根1[2],根2[2]; 内部门廊; //具有实根的示例 nRoots=四根(1.0,3.3,2.1,root1,root2); 打印根(nRoots、root1、root2); //具有复数根的示例 nRoots=四根(1.0,3.3,5.1,root1,root2); 打印根(nRoots、root1、root2); //具有实根的示例,1 0 nRoots=四根(1.0,3.3,0.0,root1,root2); 打印根(nRoots、root1、root2); //应产生1个根的线性函数示例 nRoots=四根(0.7-1.0+0.3,3.3,2.1,root1,root2); 打印根(nRoots、root1、root2); //没有解决方案的示例 nRoots=四根(0.7-1.0+0.3,0.0,5.5,root1,root2); 打印根(nRoots、root1、root2); cout

二次方程程序 我正在努力学习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); } } //用于测试函数的程序的“main”函数 //“四根”。 //=============================================================== #包括 int四根(双a,双b,双c,双*r1,双*r2); 无效打印根(整数编号,双*r1,双*r2); 使用名称空间std; int main() { 双根1[2],根2[2]; 内部门廊; //具有实根的示例 nRoots=四根(1.0,3.3,2.1,root1,root2); 打印根(nRoots、root1、root2); //具有复数根的示例 nRoots=四根(1.0,3.3,5.1,root1,root2); 打印根(nRoots、root1、root2); //具有实根的示例,1 0 nRoots=四根(1.0,3.3,0.0,root1,root2); 打印根(nRoots、root1、root2); //应产生1个根的线性函数示例 nRoots=四根(0.7-1.0+0.3,3.3,2.1,root1,root2); 打印根(nRoots、root1、root2); //没有解决方案的示例 nRoots=四根(0.7-1.0+0.3,0.0,5.5,root1,root2); 打印根(nRoots、root1、root2); cout,c++,pointers,C++,Pointers,这种形式的线条是可疑的 if ( sqrt((b*b) - 4*a*c) > 0 ) 因为您将以负数调用sqrt 您应该检查真实情况,例如,执行以下操作: if (b*b - 4*a*c > 0) 我还删除了不必要的括号。*的优先级高于-。仅对非负参数调用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); r1[1]=0; r2[1]=0; 返回(

这种形式的线条是可疑的

 if ( sqrt((b*b) - 4*a*c) > 0 )
因为您将以负数调用sqrt

您应该检查真实情况,例如,执行以下操作:

if (b*b - 4*a*c > 0)
我还删除了不必要的括号。
*
的优先级高于
-
。仅对非负参数调用
sqrt
函数。

如果(b*b-4*a*c)<0 你忘了根的真正部分了吗

为什么要测试系数“a”

我希望这对你有帮助

附言: 试试这个

int四根(双a、双b、双c、双*r1、双*r2)
{
如果(a!=0){
//二阶方程
如果(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;
返回(2);
}
else如果(b*b-4*a*c==0){
r1[0]=(-b)/(2*a);
r2[0]=0;
r1[1]=0;
r2[1]=0;
申报表(1);
}
否则{
//(b*b-4*a*c小于0)
//复根
r1[0]=-b/(2*a);
r2[0]=-b/(2*a);
r1[1]=sqrt(4*a*c-b*b)/(2*a);
r2[1]=-r1[1];
返回(2);
}
}
else如果(b!=0){
//一阶方程
r[0]=-c/b;
申报表(1);
}
否则{
//a==0&&b==0
//c=0???
返回(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
为负,则选择加号。

编译/运行此程序时实际会发生什么?“我收到一个错误”-错误消息包含有关问题的重要信息。请始终准确解释您遇到的错误。当我运行程序时,它会声明根不是共轭对,并会在“打印根”函数中正确显示根。我的问题是,我是否在不正确保存根时出错,或者这是一个数学错误我很想了解更多关于C++的事情,但是这已经让我困惑了三天了!谢谢你的帮助,我希望我已经澄清了我的问题。为什么你把你的根代表成这样的数组?你能发布你的程序的输出吗?谢谢Bathseba,我修改了我的代码并运行它。ode(Visual Studio 2012)指出并非所有控制路径都返回值,尽管我认为在这些控制结构中都有返回语句,但这就是我显示复杂根的NAN的原因吗?编译器不够智能(或者标准不能保证足够的)要确定
if/else if/else if/
分支中关于
b*b-4*a*c
值的至少一个分支必须为真。最后一种情况使用
else
。由于没有正确计算共轭对,复杂情况下会得到NAN:您需要再次运行数学。谢谢@GMERiello,我有com堆积并运行你的代码,这是一个巨大的帮助,因为我犯了一个愚蠢的错误,忘记了复数根的实部。但是当我运行这个程序时,对于两个实根的情况,我仍然发现共轭对不匹配。我已经更新了代码,你的意思是什么“对于两个实根的情况,我仍然得到共轭对不匹配”,如果实,则共轭部分为0
(-b ± √(b^2-4ac))/(2a) = 2c/(-b ∓ √(b^2-4ac)).