二次方程程序
Quadratic Equation program
我正在尝试了解有关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);
}
}
这种形式的行是可疑的;
if ( sqrt((b*b) - 4*a*c) > 0 )
因为您将在负数上调用 sqrt。
您应该检查真实情况,例如,通过执行以下操作:
if (b*b - 4*a*c > 0)
我还删除了不必要的括号。 *
的优先级高于 -
。仅对非负参数调用 sqrt
函数。
if (b*b - 4*a*c) <0你忘记了根的真正部分吗?
为什么要测试系数的"a"?
http://en.wikipedia.org/wiki/Quadratic_equation我希望这对你有所帮助。
附注:试试这个
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);
}
数值分析有一个方面是你不知道的。 假设你有二次方程
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
为负数,请选择加号。
相关文章:
- Mongodb c++驱动程序:如何查询元素的数组
- C++,系统无法执行指定的程序
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 在VS代码中交叉编译Windows与Linux上的MinGW的SDL程序
- C++ Windows 驱动程序MSB3030无法复制该文件,因为它找不到
- 重载操作程序时出错>>用于类中的字符串 memebr
- 获取日期异步信号安全吗?如果在信号处理程序中使用,它会导致死锁吗
- 试图在visual studio上用C++创建一个桌面应用程序
- 模板元程序查找相似的连续类型名称
- FFmpeg:制作一个应用程序比直接使用ffmepg更好吗
- 如何通过cpp程序运行shell脚本
- 有没有什么方法可以使用一个函数中定义的常量变量,也可以由c++中同一程序中的其他函数使用
- IPC使用多个管道和分支进程来运行Python程序
- 如何将c++程序的一些输出传递给shell,以便在shell中使用
- 使用C++程序合并排序没有得到正确的输出
- 基于boost的程序的静态链接——zlib问题
- 编写一个程序来计算以下二次方程:
- 二次方程程序
- 二次方程程序输出数
- 一个求二次方程的x值的程序