Charles_Su
Charles_Su
采纳率21.4%
2016-03-29 17:03

谁能帮我看下下面这段代码,多项式乘法和+=操作运行不了,求助啊,到底错在哪了

5
已采纳

#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 小灸舞 5年前

    多项式乘法中,楼主光new Polynomial[num]是不够的 ,还需要为里面的double数组,int数组也都需要new,num也要赋值
    加的三句我加注释了

     //多项式的乘法
        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;
        }
    

    结果:
    图片说明

    点赞 1 评论 复制链接分享
  • qq423399099 小灸舞 5年前

    你需要把相加后的值重新再赋给this指针,不然等于在函数里做了个加法,并没有把它们的和赋值给任何变量

     //多项式的加法
        Polynomial& operator+=(const Polynomial&x)
        {
            *this = *this + x;
            return *this;
        }
    
    点赞 2 评论 复制链接分享
  • devmiao devmiao 5年前

相关推荐