如何在c ++中制作变量的副本

时间:2012-11-30 23:50:17

标签: c++ variables

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

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

1 个答案:

答案 0 :(得分: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]。如果您有任何疑问,请询问