C++ Polynomial类

#include<iostream>
using namespace std;
class P
{
private:
    double *co;
    int n;
public:
    P(double a[],int n);
    P(){n=0;co=NULL;}
    P(const P &b);
    ~P();
    void input(double a[],int n);
    P operator+(const P&b)const;
    void show()const;
    void showco()const;
};
P::P(double a[],int l)
{
    n=l;
    co=new double[l];
    for(int i=0;i<n;i++)
        co[i]=a[i];
}
P::P(const P &b)
{
    cout<<"copying..from ";
    b.show();
    n=b.n;
    co=new double[n];
    for(int i=0;i<n;i++)
        co[i]=b.co[i];
}
P::~P()
{
    cout<<"Destructing."<<endl;
    delete []co;
}
void P::input(double a[],int l)
{
    n=l;
    co=new double[l];
    for(int i=0;i<l;i++)
        co[i]=a[i];
}
P P::operator+(const P&b)const
{
    int l=n;
    double a[l];
    for(int i=0;i<l;i++)
        a[i]=co[i]+b.co[i];
    while(l>=1&&a[l-1]==0)
        l--;
    //P p(a,l);
    //cout<<"return :";p.show();
    return P(a,l);
}
void P::show()const
{
    if(n==0)
    {
        cout<<"0\n";
        return ;
    }
    int f=1;
    for(int i=0;i<n;i++)
    {
        if(co[i])
        {
            if(f)
                f=0;
            else if(co[i]>0)
                cout<<"+";
            if(i==0)
                cout<<co[i];
            else
            {
                if(co[i]!=1)    //系数为1则省略(指数非0)
                    cout<<co[i];
                cout<<"x";
                if(i!=1)        //指数为1则省略
                    cout<<"^"<<i;
            }

        }
    }
    cout<<endl;
}
void P::showco()const
{
    for(int i=0;i<n;i++)
        cout<<co[i]<<" ";
    cout<<endl;
}

int main()
{
    double a[]={1,2,3,4,5,6};
    double b[]={2,1,0,-4,-1,-6};
    double c[]={-1,-2,-3,-4,-5,-6};
    P x(a,6),y,z(c,6);
    cout<<"x=";x.show();
    cout<<"y=";y.show();
    y.input(b,6);
    cout<<"y=";y.show();
    cout<<"z=";z.show();
    P ans=x+y;      //用(x+y)为ans初始化,调用复制构造函数
    cout<<"ans=x+y=";ans.show();    //无措
    ans=x+y;        //赋值操作,不调用复制构造函数,将出错,随着(x+y)的消亡,ans中指针无效。
    ans.showco();
    cout<<"ans=w+y=";ans.show();
    ans=y+z;        //出错
    ans.showco();
    cout<<"ans=y+z=";ans.show();
    ans=x+z;        // 无错
    ans.showco();
    cout<<"ans=x+z=";ans.show();
}
  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的C++ Polynomial的示例。 ```c++ #include <iostream> #include <vector> using namespace std; class Polynomial { private: vector<int> coeffs; public: Polynomial() {} Polynomial(const vector<int>& c) { coeffs = c; } int degree() const { return coeffs.size() - 1; } int operator[](int i) const { if (i >= coeffs.size()) { return 0; } return coeffs[i]; } int& operator[](int i) { if (i >= coeffs.size()) { coeffs.resize(i+1); } return coeffs[i]; } Polynomial operator+(const Polynomial& other) const { vector<int> res(max(coeffs.size(), other.coeffs.size())); for (int i = 0; i < res.size(); i++) { res[i] = (*this)[i] + other[i]; } return Polynomial(res); } Polynomial operator-(const Polynomial& other) const { vector<int> res(max(coeffs.size(), other.coeffs.size())); for (int i = 0; i < res.size(); i++) { res[i] = (*this)[i] - other[i]; } return Polynomial(res); } Polynomial operator*(const Polynomial& other) const { vector<int> res(coeffs.size() + other.coeffs.size() - 1); for (int i = 0; i < coeffs.size(); i++) { for (int j = 0; j < other.coeffs.size(); j++) { res[i+j] += (*this)[i] * other[j]; } } return Polynomial(res); } void print() const { for (int i = coeffs.size()-1; i >= 0; i--) { if (coeffs[i] != 0) { if (i == coeffs.size()-1) { cout << coeffs[i] << "x^" << i; } else { cout << " + " << coeffs[i] << "x^" << i; } } } cout << endl; } }; int main() { vector<int> c1 = {1, 2, 3}; vector<int> c2 = {4, 5}; Polynomial p1(c1); Polynomial p2(c2); Polynomial p3 = p1 + p2; Polynomial p4 = p1 * p2; p1.print(); p2.print(); p3.print(); p4.print(); return 0; } ``` 上述代码实现了一个简单的Polynomial,支持多项式加减乘,以及输出多项式的功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值