hw15_03.cpp

3、设计一个名为PolyInt 的类,表示整系数多项式(即系数为整数),这个类包括:
 一个私有int整型数据: int n, 表示多项式的次数,
一个私有int整型指针: int *a, 表示多项式的系数,即𝑝𝑝
其中a指向系数向量[𝑎𝑎0, 𝑎𝑎1, 𝑎𝑎2, ⋯ , 𝑎𝑎𝑛𝑛]的首地址
 不带形参的构造函数PolyInt(), 用于创建0多项式𝑝𝑝
𝑥
𝑥 =𝑎𝑎0+𝑎𝑎1𝑥𝑥+𝑎𝑎2𝑥𝑥2+⋯+𝑎𝑎𝑛𝑛𝑥𝑥𝑛𝑛
𝑥
𝑥 =0, 即n=0, a[0]=0 (提示: 用new为对象申请存储空间)
 带一个形参的构造函数PolyInt(int n), 用于创建系数全部为0的n次多项式
 带两个形参的构造函数: PolyInt(int n, int a[])
 以成员函数方式重载赋值运算 =,实现深度拷贝: PolyInt & operator=(const PolyInt & p)
 以成员函数方式重载运算符(),计算多项式在给定点的值,如𝑝𝑝
𝑥
𝑥 =1+2𝑥𝑥+4𝑥𝑥3,则𝑝𝑝
int operator() (const int x)
 以非成员函数方式重载输出运算符 <<, 如𝑝𝑝
𝑥
𝑥 =1+2𝑥𝑥+4𝑥𝑥3 输出为[1,2,0,4]
friend ostream & operator<<(ostream & out, const PolyInt & p)
 使用非成员函数方式实现两个整系数多项式的乘法运算:
PolyInt operator*(const PolyInt & p, const PolyInt & q)
 析构函数:~PolyInt(),释放构造函数中由new申请的存储空间。
实现这个类,并在主函数中测试这个类:创建多项式𝑝𝑝
𝑥
𝑥 =1+2𝑥𝑥+4𝑥𝑥3 和𝑞𝑞
2 =37
𝑥
𝑥 =2−4𝑥𝑥2−3𝑥𝑥3+𝑥𝑥5,计算乘积
s(x)=p(x)*q(x),并输出 p(x), q(x) 和 s(x),以及 p(2) 和 s(2) 的值。
(程序取名hw15_03.cpp)
#include <iostream>  
#include <cstdlib>   
#include <cmath>  


using namespace std;

class PolyInt {
public:
    PolyInt() {
        this->n=0;
        this->a = new int[n+1]{0};
    }
    PolyInt(int n) {
        this->n = n;
        this->a = new int[n+1];
        for (int i = 0; i <= n; i++) {
            this->a[i] = 0;
        }
    }
    PolyInt(int n, int a[]) {
        this->n = n;
        this->a = new int[n+1];
        for (int i = 0; i <= n; i++) {
            this->a[i] = a[i];
        }
    }
    PolyInt & operator=(const PolyInt & p) {
            delete[] this->a;
            this->n = p.n;
            this->a = new int[p.n+1];
            for (int i = 0; i < p.n; ++i) {
                this->a[i] = p.a[i];
            }
    }
    int operator() (const int x) {
        int value = a[n];
        for (int i = n-1; i >=0 ; i--) {
            value= value*x;
            value += a[i];
        }
        return value;
    }
    friend ostream & operator<<(ostream & out, const PolyInt & p);

    void Display() const {
        cout << *this;
    }
    friend PolyInt operator+(const PolyInt &p, const PolyInt &q);
    friend PolyInt operator*(const PolyInt &p, const PolyInt &q);
    ~PolyInt() {
        delete[] a;
    }
private:
    int n;
    int *a;
};
PolyInt operator+(const PolyInt &p, const PolyInt &q) {
    PolyInt newp(max(p.n, q.n));

    for (int i = 0; i <= newp.n; ++i) {
        if (i < p.n && i < q.n) {
            newp.a[i] = p.a[i] + q.a[i];
        } else if (i < p.n) {
            newp.a[i] = p.a[i];
        } else {
            newp.a[i] = q.a[i];
        }
    }
    return newp;
}
PolyInt operator*(const PolyInt &p, const PolyInt &q) {
    PolyInt newp(p.n + q.n);
    for (int i = 0; i <= p.n; ++i) {
        for (int j = 0; j <= q.n; ++j) {
            newp.a[i+j] += p.a[i] * q.a[j];
        }
    }
    return newp;
}
ostream & operator<<(ostream & out, const PolyInt & p) {
    out << "[";
    for (int i = 0; i <=p.n; i++) {
        out << p.a[i];
        if (i < p.n) {
            out << ",";
        }
    }
    out << "]";
    return out;
}
int main() {
    int p[] = {1, 2, 0, 4};
    int q[] = {2, 0, 4, 3, 5};
    PolyInt p1(3, p);
    PolyInt q1(4, q);
    PolyInt s = p1*q1;
    cout << "p(x): "<<p1<<endl;
    cout << "q(x): "<<q1<<endl;
    cout << "s(x): "<<s<<endl;
    cout<<p1<<"+"<<q1<<"="<<p1+q1<<endl;
    cout<<"p(2)="<<p1(2)<<endl;
    cout<<"s(2)="<<s(2)<<endl;
    return 0;
}

发表评论

您的邮箱地址不会被公开。 必填项已用 * 标注

滚动至顶部