回复 2楼 peach5460
程序代码:
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <iomanip>
#include <cstring>
using namespace std;
class Polynomial
{
public:
Polynomial(int = 0);
Polynomial & operator + (Polynomial & add);
Polynomial & operator - (Polynomial & add);
Polynomial & operator * (Polynomial &);
Polynomial & operator += (Polynomial & add);
Polynomial & operator -= (Polynomial & add);
Polynomial & operator *= (Polynomial &);
Polynomial & test(Polynomial & right);
private:
int *a;
int size, num;
};
Polynomial & Polynomial::operator + (Polynomial & add)
{
int number = add.num, mrk = 0;
Polynomial & mi = *this;
int cosize = add.size; int * q = new int[cosize];
for (int d=0;d<cosize;++d) q[d]=add.a[d];
for (int i = 0, s = 0; i < number; ++i, s+=2)
{
mrk = 0;
for (int j = 0, k = 0; j < mi.num; ++j,k+=2)
{
if (q[s]==mi.a[k])
{
mrk = 1;
mi.a[k+1]+=q[s+1];
q[s+1]=0;
}
}
if (!mrk)
{
int *p = new int[mi.size+2];
for (int d=0;d<mi.size;d++)
p[d]=mi.a[d];
p[mi.size] = q[s];
p[mi.size+1] = q[s+1];
q[s+1]=0;
mi.a = p;
mi.num++;
mi.size+=2;
}
}
return *this;
}
Polynomial & Polynomial::operator - (Polynomial & add)
{
int number = add.num, mrk = 0;
int cosize = add.size; int * q = new int[cosize];
for (int d=0;d<cosize;++d) q[d]=add.a[d];
for (int i = 0, s = 0; i < number; ++i, s+=2)
{
mrk = 0;
for (int j = 0, k = 0; j < num; ++j,k+=2)
{
if (q[s]==a[k])
{
mrk = 1;
a[k+1]-=q[s+1];
q[s+1]=0;
}
}
if (!mrk)
{
int *p = new int[size+2];
for (int d=0;d<size;d++)
p[d]=a[d];
p[size] = q[s];
p[size+1] = -q[s+1];
q[s+1]=0;
a = p;
num++;
size+=2;
}
}
return *this;
}
Polynomial & Polynomial::operator * (Polynomial & mu)
{
Polynomial b, c;
c = *this; b = *this;
for (int j=0,r=0; j<b.num;++j,r+=2)
{
(*this).a[r]+=mu.a[0]; (*this).a[r+1]*=mu.a[1];
}
for (int i=1,k=2;i<mu.num;++i,k+=2)
{
for (int j=0,r=0; j<b.num;++j,r+=2)
{
b.a[r]+=mu.a[k];
b.a[r+1]*=mu.a[k+1];
}
*this += b;
b = c;
}
return *this;
}
Polynomial & Polynomial::operator += (Polynomial & add)
{
int number = add.num, mrk = 0;
int cosize = add.size; int * q = new int[cosize];
for (int d=0;d<cosize;++d) q[d]=add.a[d];
for (int i = 0, s = 0; i < number; ++i, s+=2)
{
mrk = 0;
for (int j = 0, k = 0; j < num; ++j,k+=2)
{
if (q[s]==a[k])
{
mrk = 1;
a[k+1]+=q[s+1];
q[s+1]=0;
}
}
if (!mrk)
{
int *p = new int[size+2];
for (int d=0;d<size;d++)
p[d]=a[d];
p[size] = q[s];
p[size+1] = q[s+1];
q[s+1]=0;
a = p;
num++;
size+=2;
}
}
return *this;
}
Polynomial & Polynomial::operator -= (Polynomial & add)
{
int number = add.num, mrk = 0;
int cosize = add.size; int * q = new int[cosize];
for (int d=0;d<cosize;++d) q[d]=add.a[d];
for (int i = 0, s = 0; i < number; ++i, s+=2)
{
mrk = 0;
for (int j = 0, k = 0; j < num; ++j,k+=2)
{
if (q[s]==a[k])
{
mrk = 1;
a[k+1]-=q[s+1];
q[s+1]=0;
}
}
if (!mrk)
{
int *p = new int[size+2];
for (int d=0;d<size;d++)
p[d]=a[d];
p[size] = q[s];
p[size+1] = -q[s+1];
q[s+1]=0;
a = p;
num++;
size+=2;
}
}
return *this;
}
Polynomial & Polynomial::operator *= (Polynomial & mu)
{
Polynomial b, c;
b = *this;
c = *this;
for (int j=0,r=0; j<b.num;++j,r+=2)
{
(*this).a[r]+=mu.a[0]; (*this).a[r+1]*=mu.a[1];
}
for (int i=1,k=2;i<mu.num;++i,k+=2)
{
for (int j=0,r=0; j<b.num;++j,r+=2)
{
b.a[r]+=mu.a[k];
b.a[r+1]*=mu.a[k+1];
}
*this += b;
b = c;
}
return *this;
}
int main(void)
{
return 0;
}