0
#include <iostream>
#include <math.h>
using namespace std;

class polynomial{
    int degree;
    float *coefs;
public:
    polynomial(int d);
    float operator()(float x);
    void operator==(polynomial P);
    polynomial operator^(int k);
    float operator[](float x);
    float *getcoefs();
};

polynomial::polynomial(int d){
    int i;
    degree=d;
    if(d>=0){
        coefs=(float *)malloc((d+1)*sizeof(float));
        cout<<"Create a polynomial of degree "<<d<<"\n";
        for (i=d;i>0;i--){
            cout<<"The coefficient of x^"<<i<<" is : ";
            cin>>coefs[i];
            cout<<"\n";
        }
        cout<<"The constant is : ";
        cin>>coefs[0];
        cout<<"\n";
        cout<<"The polynomial is created\n";
    }
}

这是 () 运算符的重载,因此 P(x) 返回多项式的值:

float polynomial::operator()(float x){
    float sum=0;
    int i,kstart;
    for(i=0;i<=degree;i++){
        if(coefs[i]!=0){
            kstart=i;
            break;            
        }                       
    }
    for(i=kstart;i<=degree;i++){
        sum+=coefs[i]*pow(x,i-kstart);                       
    }
    return sum;
}

这是 == 的重载,将一个多项式的系数复制到另一个多项式:

void polynomial::operator==(polynomial P){
     coefs=P.coefs;
}

这是 ^ 的重载,因此 P^k 返回 P 的第 k 个导数:

polynomial polynomial::operator^(int k){
    int i,j;
    polynomial G(-1);
    G.degree=this->degree;
    G==(*this);
    if(k>G.degree){
        for(i=0;i<=G.degree;i++){
            G.coefs[i]=0;
        }
    }
    else{
        for(i=0;i<k;i++){
            G.coefs[i]=0;
            for(j=i+1;j<=G.degree;j++){
                G.coefs[j]*=(j-i);                    
            }          
        }
    }
    return G;
}

float polynomial::operator[](float x){
    return (x-(*this)(x)/(((*this)^1)(x)));      
}

float *polynomial::getcoefs(){
      return coefs;
}

int main(){
    int i,d,maxiter,found;
    float seed,x1,x2,e;
    float *coefs;
    cout<<"The polynomial's degree is : ";
    cin>>d;
    cout<<"\n";
    polynomial P(d),temp(-1);
    cout<<"-------Solve P(x)=0--------\n";
    cout<<"Maxiterations = ";
    cin>>maxiter;
    cout<<"\n";
    cout<<"Tolerance = ";
    cin>>e;
    cout<<"\n";
    cout<<"Seed = ";
    cin>>seed;
    cout<<"\n";
    found=0;
    x1=seed;
    for(i=0;i<maxiter;i++){
        memcpy((void *)(&temp),(void *)(&P),sizeof(polynomial));
        x2=P[x1];
        if(fabs(x2-x1)<=e){
            found=1;
            break;                         
        }
        coefs=temp.getcoefs();
        cout<<coefs[0]<<"\n";
        cout<<coefs[1]<<"\n";
        cout<<coefs[2]<<"\n";
        x1=x2;
    }
    if(found==1){
        cout<<"A possible solution is x = "<<x2<<"\n";
    }
    else{
        cout<<"No solution found!\n"; 
    }
    system("PAUSE");
    return EXIT_SUCCESS;   
}

在这些测试行中可以看到问题:

cout<<coefs[0]<<"\n";
cout<<coefs[1]<<"\n";
cout<<coefs[2]<<"\n";

系数应该保持不变,但现在它们改变了

4

1 回答 1

0

我通过以下方式解决了我的问题......我更改了我的构造函数并使其在类定义中看起来像这样:

polynomial(int d,int test=0);

这是它的代码:

polynomial::polynomial(int d,int test){
    int i;
    degree=d;
    if(test==0){
        coefs=(float *)malloc((d+1)*sizeof(float));
        cout<<"Create a polynomial of degree "<<d<<"\n";
        for (i=d;i>0;i--){
            cout<<"The coefficient of x^"<<i<<" is : ";
            cin>>coefs[i];
            cout<<"\n";
        }
        cout<<"The constant is : ";
        cin>>coefs[0];
        cout<<"\n";
        cout<<"The polynomial is created\n";
    }
    else{
        coefs=(float *)malloc((d+1)*sizeof(float));
        for (i=d;i>=0;i--){
            coefs[i]=0;
        }
    }
}

然后导致问题的功能变成了这样:

void polynomial::operator==(polynomial P){
     int i;
     for(i=0;i<=degree;i++){
         coefs[i]=P.coefs[i];
     }
}

并相应地调整了我的代码的一小部分,我能够在不破坏多项式的情况下调用 P[x]。如果您有任何问题,请询问

于 2012-12-03T13:12:20.533 回答