#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";
    }
}

This is an overloading of the () operator so that P(x) returns the value of the polynomial:

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;
}

This is an overloading of the == that is copying the coefficients of one polynomial to another's:

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

This is an overloading of the ^ so that P^k returns the k-th derivative of P:

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;   
}

The problem can be seen in these testing lines:

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

The coefficients should stay the same,but now they change

有帮助吗?

解决方案

I solved my problem in the following way... I changed my constructor and made it look like that in the class definition:

polynomial(int d,int test=0);

and this is its code:

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;
        }
    }
}

then the function that was causing the problem was made into this:

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

and with accordingly adapting small parts of my code,I was able to call P[x] without destroying my polynomial.If you have any question,please ask

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top