问题描述:
假设一个多项式形式为:p(x)=c1*x^(e1)+c2*x^(e2)+...+cm*x^(em)。其中ei(1≤i≤m)为整数类型的指数,并且没有相同指数的多项式项;ci(1≤i≤m)为实数类型的序数。编写求两个多项式相加的程序。
示例代码:
#include <iostream>
using namespace std;
const int MaxSize = 20;
struct PolyElem //顺序表元素类型
{
double coef; //系数
int exp; //指数
};
class PolyClass1 //多项式顺序表类
{
PolyElem *data; //存放顺序表元素
int length; //存放顺序表长度
public:
PolyClass1(); //构造函数
~PolyClass1(); //析构函数,释放head顺序表的所有结点
void DispPoly(); //输出多项式顺序表
void CreateListR(double a[], int b[], int n);
void Sort(); //对一个多项式顺序表按exp域递减排序
PolyClass1 &operator=(PolyClass1 &s);//重载运算符“=”成员函数
friend PolyClass1 &operator+(PolyClass1 &, PolyClass1 &);//友元函数,重载运算符“+”,用于求两个多项式相加运算
};
PolyClass1::PolyClass1() //构造函数
{
data = new PolyElem[MaxSize];
length = 0;
}
PolyClass1::~PolyClass1() //析构函数,释放顺序表的data空间
{
delete[] data;
}
void PolyClass1::DispPoly() //输出多项式顺序表
{
int i = 0;
bool first = true;
while (i<length)
{
if (first)
first = false;
else if (data[i].coef>0)
cout << "+";
if (data[i].exp == 0)
cout << data[i].coef;
else if (data[i].exp == 1)
cout << data[i].coef << "x";
else
cout << data[i].coef << "x^" << data[i].exp;
i++;
}
cout << endl;
}
void PolyClass1::CreateListR(double a[], int b[], int n) //由含有n个元素的系数数组a和指数数组b建立多项式顺序表
{
int i;
for (i = 0; i<n; i++)
{
data[i].coef = a[i];
data[i].exp = b[i];
}
length = n;
}
void PolyClass1::Sort() //采用直接插入排序方法对一个多项式顺序表按exp域递减排序
{
int i, j;
PolyElem tmp;
for (i = 1; i<length; i++)
{
tmp = data[i];
j = i - 1;
while (j >= 0 && data[j].exp<tmp.exp)
{
data[j + 1] = data[j];
j--;
}
data[j + 1] = tmp;
}
}
PolyClass1 &PolyClass1::operator=(PolyClass1 &s)//重载运算符“=”成员函数
{
int i;
delete[] data;
data = new PolyElem[MaxSize];
for (i = 0; i<s.length; i++)
{
data[i].coef = s.data[i].coef;
data[i].exp = s.data[i].exp;
}
length = s.length;
return *this;
}
PolyClass1 &operator+(PolyClass1 &poly1, PolyClass1 &poly2) //由两个多项式poly1、poly2相加运算得到poly3
{
int i = 0, j = 0, k = 0;
double c;
static PolyClass1 poly3; //因要返回该对象,所以设置为静态的
while (i<poly1.length && j<poly2.length)
{
if (poly1.data[i].exp>poly2.data[j].exp)
{
poly3.data[k].exp = poly1.data[i].exp;
poly3.data[k].coef = poly1.data[i].coef;
i++; k++;
}
else if (poly1.data[i].exp<poly2.data[j].exp)
{
poly3.data[k].exp = poly2.data[j].exp;
poly3.data[k].coef = poly2.data[j].coef;
j++; k++;
}
else
{
c = poly1.data[i].coef + poly2.data[j].coef; //求两指数相等元素的系数和
if (c != 0) //系数和不为0时复制
{
poly3.data[k].exp = poly1.data[i].exp; //复制元素
poly3.data[k].coef = c;
k++;
}
i++; j++;
}
}
while (i<poly1.length) //复制poly1余下的元素
{
poly3.data[k].exp = poly1.data[i].exp;
poly3.data[k].coef = poly1.data[i].coef;
i++; k++;
}
while (j<poly2.length) //复制poly2余下的元素
{
poly3.data[k].exp = poly2.data[j].exp;
poly3.data[k].coef = poly2.data[j].coef;
j++; k++;
}
poly3.length = k;
return poly3;
}
void main()
{
PolyClass1 Poly1, Poly2, Poly3; //建立3个多项式顺序表对象
double a[MaxSize];
int b[MaxSize], n;
//===创建第1个多项式顺序表
a[0] = 2.0; b[0] = 3;
a[1] = 3.2; b[1] = 5;
a[2] = -6.0; b[2] = 1;
a[3] = 10.0; b[3] = 0;
n = 4;
Poly1.CreateListR(a, b, n);
cout << "第1个多项式: "; Poly1.DispPoly();
Poly1.Sort();
cout << "排序后结果: "; Poly1.DispPoly();
//===创建第2个多项式顺序表
a[0] = 6.0; b[0] = 1;
a[1] = 1.8; b[1] = 5;
a[2] = -2.0; b[2] = 3;
a[3] = 1.0; b[3] = 2;
a[4] = -2.5; b[4] = 4;
a[5] = -5.0; b[5] = 0;
n = 6;
Poly2.CreateListR(a, b, n);
cout << "第2个多项式: "; Poly2.DispPoly();
Poly2.Sort();
cout << "排序后结果: "; Poly2.DispPoly();
//===
Poly3 = Poly1 + Poly2;
cout << "相加后多项式:"; Poly3.DispPoly();
cout << "销毁3个多项式顺序表\n";
}