Charles_Su 于 2016.03.30 01:03 提问

#include
#include
using namespace std;
class Polynomial
{
double *p_coefs;
int *p_exps, num;
public:
Polynomial();
Polynomial(double coefs[], int exps[], int size);//系数数组、指数数组和项数
Polynomial(const Polynomial&x);
~Polynomial();
//赋值操作符
Polynomial &operator=(const Polynomial&x)
{
if (&x == this) return *this;
num = x.num;
delete[]p_coefs;
delete[]p_exps;
p_coefs = new double[x.num];
p_exps = new int[x.num];
for (int i = 0; i <x. num; i++) { p_coefs[i] = x.p_coefs[i]; }
for (int j = 0; j <x.num; j++) { p_exps[j] = x.p_exps[j]; }
return *this;
}
//最高幂指数
int degree()const
{
return p_exps[num - 1];
}
//计算多项式的值
double evaluate(double x) const
{
double value = 0, temp = x;
for (int i = 0; i < num; i++)
{
for (int j = 0; j < p_exps[i]; j++) x *= temp;
x *= p_coefs[i];
value += x;
x = temp;
}
return value;
}
//判断两多项式是否相等
bool operator==(const Polynomial&x)const
{
if (num != x.num)return false;
for (int i = 0; i < num; i++)
if (p_exps[i] != x.p_exps[i] || p_coefs[i] != x.p_coefs[i])return false;
return true;
}
//判断两多项式是否不相等
bool operator!=(const Polynomial&x)const
{
if (num != x.num) return true;
for (int i = 0; i < num; i++)
if (p_exps[i] != x.p_exps[i] || p_coefs[i] != x.p_coefs[i])return true;
return false;

``````}
//多项式的加法
Polynomial operator+(const Polynomial&x)const
{
Polynomial new_poly;
new_poly.num = num + x.num;
new_poly.p_coefs = new double[num +x.num];
new_poly.p_exps = new int[num + x.num];
for (int i = 0; i < num; i++) { new_poly.p_coefs[i] = p_coefs[i]; }
for (int j = 0; j < num; j++) { new_poly.p_exps[j] = p_exps[j]; }
for (int i = num; i <new_poly.num; i++) { new_poly.p_coefs[i] = x.p_coefs[i - num]; }
for (int j = num; j <new_poly.num; j++) { new_poly.p_exps[j] = x.p_exps[j - num]; }
for (int i = 0; i < new_poly.num; i++)
for (int j = i + 1; j < new_poly.num; j++)
{
if (new_poly.p_exps[i] == new_poly.p_exps[j])
{
new_poly.p_coefs[i] = new_poly.p_coefs[i] + new_poly.p_coefs[j];
new_poly.p_coefs[j] = 0;
}
}
return new_poly;
}
//多项式的减法
Polynomial operator-(const Polynomial&x)const
{
Polynomial poly,poly1;
poly.num = num ;
poly.p_coefs = new double[num];
poly.p_exps = new int[num];
for (int i = 0; i < x.num; i++) { poly.p_coefs[i] =p_coefs[i]; }
for (int j = 0; j < x.num; j++) { poly.p_exps[j] = p_exps[j]; }
poly1.num = x.num;
poly1.p_coefs = new double[x.num];
poly1.p_exps = new int[x.num];
for (int i = 0; i < x.num; i++) { poly1.p_coefs[i] = -x.p_coefs[i]; }
for (int j = 0; j < x.num; j++) { poly1.p_exps[j] = x.p_exps[j]; }
return poly+poly1;
}
//多项式的乘法
Polynomial operator*(const Polynomial&x)const
{
Polynomial *p,sum;
p = new Polynomial[num];
for (int i = 0; i < num;i++)
{
for (int j = 0; j < x.num; j++)
{
p[i].p_coefs[j] = p_coefs[i] * x.p_coefs[j];
p[i].p_exps[j] = p_exps[i] + x.p_exps[j];

}

}
for (int i = 0; i < num; i++) {
sum =sum+p[i];
}
return sum;

}
//多项式的加法
Polynomial& operator+=(const Polynomial&x)
{
{return *this+ x; }
}
//多项式的减法
Polynomial& operator-=(const Polynomial&x)
{return *this - x;}
Polynomial& operator*=(const Polynomial&x)
{
return *this * x;
}
void display()const;
``````

};
//下面两个重载函数实现构造函数
Polynomial::Polynomial()
{
p_coefs = NULL; p_exps = NULL;
num = 0;
}
Polynomial::Polynomial(double coefs[], int exps[], int size)
{
num = size;
p_coefs = new double[num];
p_exps = new int[num];
for (int i = 0; i < num; i++) { p_coefs[i] = coefs[i]; }
for (int j = 0; j < num; j++) { p_exps[j] = exps[j]; }
}
Polynomial::Polynomial(const Polynomial&x)
{

``````num = x.num;
p_coefs = new double[num];
p_exps = new int[num];
for (int i = 0; i < num; i++) { p_coefs[i] = x.p_coefs[i]; }
for (int j = 0; j < num; j++) { p_exps[j] = x.p_exps[j]; }
``````

}
//析构函数
Polynomial::~Polynomial()
{
delete[]p_coefs;
p_coefs = NULL;
delete[]p_exps;
p_exps = NULL;
num = 0;
}

//显示多项式
void Polynomial::display()const
{
double t1;
int t2;
//冒泡排序
for (int j = 0; j < num - 1; j++)
for (int i = 0; i < num - 1 - j; i++)
if (p_exps[i]>p_exps[i + 1])
{
t1 = p_coefs[i]; t2 = p_exps[i];
p_coefs[i] = p_coefs[i + 1]; p_exps[i] = p_exps[i + 1];
p_coefs[i + 1] = t1; p_exps[i + 1] = t2;
}
if (p_coefs[0] != 0)
{
if (p_exps[0] != 0 && p_exps[0] != 1)
{
if (p_coefs[0] != 1 && p_coefs[0] != -1)cout << p_coefs[0] << "x^" << p_exps[0];
else if (p_coefs[0] == 1)cout << "x^" << p_exps[0];
else cout << "-x^" << p_exps[0];
}
else if (p_exps[0] == 0)
{
cout << p_coefs[0];
}
else
{
if (p_coefs[0] != 1 && p_coefs[0] != -1)cout << p_coefs[0] << "x";
else if (p_coefs[0] == 1)cout << "x";
else cout << "-x";
}

``````}
for (int i = 1; i < num; i++)
{
if (p_coefs[i] != 0)
{
if (p_exps[i] != 0 && p_exps[i] != 1)
{
if (p_coefs[i] > 0 && p_coefs[i] != 1 && p_coefs[i] != -1)cout << '+' << p_coefs[i] << "x^" << p_exps[i];
else if (p_coefs[i] == 1)cout << "+x^" << p_exps[i];
else if (p_coefs[i] == -1)cout << "-x^" << p_exps[i];
else cout << p_coefs[i] << "x^" << p_exps[i];
}
else if (p_exps[i] == 0)
{
cout << p_coefs[i];
}
else
{
if (p_coefs[i] != 1 && p_coefs[i] != -1)cout << p_coefs[i] << "x";
else if (p_coefs[i] == 1)cout << "x";
else cout << "-x";
}

}
}
cout << endl;
``````

}
int main()
{
double a[5] = { 1,2,3,4,5 }, i[5] = { 1,3,5,7,10 };
int c = 5, b[5] = { 1,2,3,4,5 }, j[5] = { 1,3,5,8,7 };
Polynomial poly1(a, b, c), poly2, poly3(i, j, c),poly4,poly5;
cout << "多项式一:"; poly1.display();
poly2 = poly1;
cout << "多项式二:"; poly2.display();
cout << "多项式三:"; poly3.display();
cout <<"多项式一最高次幂:"<< poly1.degree() << endl;
cout << "多项式一值为:" << poly1.evaluate(3) << endl;
cout <<"多项式二与三是否相等:"<< boolalpha << (poly2 == poly3) << endl;
cout << "多项式二与三是否不相等:" << boolalpha << (poly2 != poly3) << endl;
cout << "多项式一与三的和:"; (poly1 + poly3).display();
cout << "多项式一与三的差:"; (poly1 - poly3).display();
cout << "多项式一与三的积:"; (poly1 * poly3).display();
poly2 += poly3;
cout << "多项式二增加后:"; poly2.display();
poly2 -= poly3;
cout << "多项式二减少后:"; poly2.display();
poly2 *= poly3;
cout << "多项式二相乘后:"; poly2.display();
system("pause");
return 0;
}

3个回答

qq423399099      2016.03.30 09:21

`````` //多项式的乘法
Polynomial operator*(const Polynomial&x)const
{
Polynomial *p,sum;
p = new Polynomial[num];
for (int i = 0; i < num;i++)
{
p[i].p_coefs = new double[x.num]; //要new
p[i].p_exps = new int[x.num];        //要new
p[i].num = x.num;                       //num也要赋值
for (int j = 0; j < x.num; j++)
{
p[i].p_coefs[j] = p_coefs[i] * x.p_coefs[j];
p[i].p_exps[j] = p_exps[i] + x.p_exps[j];
}

}
for (int i = 0; i < num; i++) {
sum =sum+p[i];
}
return sum;
}
``````

Charles_Su 回复小灸舞: 谢谢，大神![图片说明](http://forum.csdn.net/PointForum/ui/scripts/csdn/Plugin/001/face/5.gif)[图片说明](http://forum.csdn.net/PointForum/ui/scripts/csdn/Plugin/001/face/5.gif)[图片说明](http://forum.csdn.net/PointForum/ui/scripts/csdn/Plugin/001/face/5.gif)

qq423399099 回复Charles_Su: 我回复在另一个帖了，你要把相加后的和重新再赋值给this.....

Charles_Su 回复小灸舞: 是的，为什么是错的![图片说明](http://forum.csdn.net/PointForum/ui/scripts/csdn/Plugin/001/face/1.gif)

qq423399099 回复Charles_Su: 你是说求积后面打印的那三行么。。。我还在想怎么打印出来的都是一样的。。。

Charles_Su 那我的自加，自减，自乘操作为什么不对啊？

qq423399099      2016.03.30 14:31

`````` //多项式的加法
Polynomial& operator+=(const Polynomial&x)
{
*this = *this + x;
return *this;
}
``````
devmiao      2016.03.30 07:00