首页 > 解决方案 > 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);
}

标签: c++polynomials

解决方案


推荐阅读