如何解决c++多项式结果,只计算一个
我完全是 C++ 的开始……我必须创建一个程序来找出多项式的根。我已经编译它等(我更改了不同的代码并调整了它们)..一切都很好,直到多项式有两个解决方案。我刚刚注意到不幸的是我的程序只显示了一个。有没有人可以帮助我并更改以显示这两个结果? 提前致谢! 这是我的代码。
#include <iostream>
#include <math.h>
#include <iomanip>
using namespace std;
int root,p,q;
float numerOfRoots[100];
float algHor(int k,float x){ //Horner's algorithm
if (k == root){
return numerOfRoots[root];}
else{
return algHor(k + 1,x)* x + numerOfRoots[k];}
}
float algSTs(int j){ //Show-Trauba algorithm (function s(j))
return (root - j) % q;
}
float algSTR(int j){ //algorithm Show-Trauba (function r(j))
if (j % q == 0)
return q;
else
return 0;
}
float T(int i,int j,float x){ //algorithm Show-Trauba - main function
if (x == 0)
return numerOfRoots[j];
else
if (j == -1)
return numerOfRoots[root - i - 1]* pow(x,algSTs(i + 1));
else
if (i == j)
return numerOfRoots[root] * pow(x,algSTs(0));
else
return T(i-1,j-1,x)+T(i-1,j,x) * pow(x,algSTR(i - j));
}
float derivative(int degree,float point){
if (point == 0)
return T(root,degree,point);
else return T(root,point) / pow(point,degree%q);
}
main(){
int k;
int iteration = 100;
float beginning,ending,results;
p = 1;
q = root + 1;
cout << "\Enter the degree of the polynomial : ";
cin >> root;
if (root > 100){
cout << "Too big." << endl;
}
if (root < 2){
cout << "Too small";
}
cout << "\nNow enter the coefficients of the polynomial. Start with your greatest power .\n";
for(k = root; k >= 0; k--){
cout <<"a" << k << " - ";
cin >> numerOfRoots[k];
}
cout << "\nEnter the beginning: ";
cin >> beginning;
cout << "Enter the beginning: ";
cin >> ending;
if (derivative(2,beginning)*algHor(0,beginning)>0){
results = beginning;
} else {
results = ending;
}
for (k = 1; k <= iteration; k++){
results = results - (algHor(0,results)/derivative(1,results));
if (algHor(0,results)==0){
break;
}
}
if (algHor(0,results) == 0){
cout << "\nReal exact root: " << setprecision(5) << results << endl;
} else{
cout << "\nReal root: " << setprecision(5) << results << endl;
}
return(0);
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。