多项式乘法需要用到运算符重载

#include
#include
using namespace std;
const int MAX_POLY = 20;
int p[MAX_POLY], q[MAX_POLY], s[MAX_POLY * 2];
int n1, n2;
class Polynomial;

class Polynomial{//多项式类

public:
Polynomial();
void mulit_input();
void mulit_poly(int p[], int n1, int q[], int n2, int s[]);//添加一项,若有相同的指数项,则合并
void mulit_output();
};

void mulit_poly(int p[], int n1, int q[], int n2, int s[]) {
for (int i = 0; i < n1 + n2; i++) {
s[i] = 0;
}
for (int i = 0; i < n1; i++) {
for (int j = 0; j < n2; j++) {
s[i + j] += p[i] * q[j];
}
}
}
void mulit_input(){
cout << "输入第一个多项式的个数:";
cin >> n1;
cout << "输入第二个多项式的个数:";
cin >> n2;//输入第一行的个数
cout << "输入第一个多项式的系数:";
for (int i = n1 - 1; i >= 0; i--) {

    cin >> p[i];//为第一列系数
}
cout << "P(x)= ";
bool first = true;
for (int i = n1 + n2 - 1; i >= 0; i--) {//输出多项式
    if (p[i] != 0) {
        if (!first)//如果不是第一个
            cout << "+";//输出+
        cout << p[i];//输出第i项
        if (i != 0)//如果i不等于0输x^
            cout << "x^" << i;
       first=false;
    }
}
cout << "\n";
cout << "输入第二个多项式的系数:" ;
for (int i = n2 - 1; i >= 0; i--) {
    
cin >> q[i];//为第二列系数

}
cout << "Q(x)= " ;

for (int i = n1 + n2 - 1; i >= 0; i--) {//输出多项式
if (q[i] != 0) {
if (!first)//如果不是第一个
cout << "+";//输出+
cout << q[i];//输出第i项
if (i != 0)//如果i不等于0输x^
cout << "x^" << i;
first=false;
}

}

cout <<"\n";
}

void mulit_output(){
cout << "输出两个多项式相乘的值";
cout << "S(x)= ";
bool first = true;
for (int i = n1 + n2 - 1; i >= 0; i--) {//输出多项式
if (s[i] != 0) {
if (!first)//如果不是第一个
cout << "+";//输出+
cout << s[i];//输出第i项
if (i != 0)//如果i不等于0输x^
cout << "x^" << i;
first = false;
}
}
}
int main()
{

mulit_input();//输入
mulit_poly(p, n1, q, n2, s);//调用多项式函数
mulit_output();//输出

}
该如何修改呢


#include<iostream>
using namespace std;
class complex{
    private:
        int real;
        int image;
    public:
        complex(){}
        complex(int real,int image){
            this->real=real;
            this->image=image;
        }
        void setReal(int value){
            real=value;
        }
        void setImage(int value){
            image=value;
        }
        int getReal()const{
            return real;
        }
        int getImage()const{
            return image;
        }
        void display(){
            cout<<real<<'+'<<image<<'i'<<endl;
        }
        
        complex operator+(const complex &a)const{
            complex b;
            b.setReal(real+a.getReal());
            b.setImage(image+a.getImage());
            return b;
        }
        complex operator+(const int &a)const{
            complex b;
            b.setReal(real+a);
            b.setImage(image);
            return b;
        }
        //定义非成员函数operator+
        friend complex operator+(int a,const complex &b);
};
complex operator+(int a,const complex &b){
    complex c;
    c.setReal(a+b.getReal());
    c.setImage(b.getImage());
    return c;
}

int main(){
    complex a(1,2);
    complex c;
    complex d;
    c=a+2;
    c.display();
    d=4+a;
    d.display();
    return 0;
}


img


可以参考