c++ - 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,degree,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);
}
解决方案
推荐阅读
- python - 在不同的日期时间索引上组合 DataFrame
- javascript - 使用曲线将输入范围弧形拇指连接到滑块
- node.js - 使用 node-postgres 以编程方式创建数据库
- enums - Rails 6:类中的枚举
- java - 作为 OSGi 运行时,如何在 Jetty 中正确初始化 WebAppContext?
- regex - notepad++ 正则表达式如何从此列表中提取 userId
- xamarin.android - 显示从列表视图中选择的项目
- python - Code is looking for attributes in the wrong class
- java - Drag items to canvas from menu in Android Studio
- manim - Issue in changing the background of a scene in manim