overload +运算符添加2个多项式C ++ [英] overload + operator to add 2 polynomials C++

查看:150
本文介绍了overload +运算符添加2个多项式C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写函数以将2个多项式加在一起,其中2个多项式具有相同数量的最高程度(不需要输入所有项)的情况很好,但是两个多项具有不同程度的情况不是工作,函数以某种方式存储一些大值作为系数

I'm writing the function to add 2 polynomials together, the case where 2 polynomials have the same amount of highest degree (all terms need not to be entered) works fine, but the case where two polys have different degree is not working, the function somehow store some big value as the coefficients

这是函数

// overload +
Polynomial Polynomial::operator+(const Polynomial &right)
{
    // get the highest exponent value for result
    int highestExp = 0;
    if (maxExp < right.maxExp)
        highestExp = right.maxExp;
    else if (maxExp >= right.maxExp)
        highestExp = maxExp;

    Polynomial res;
    res.setPolynomial(highestExp);

    for (int coeff=0; coeff < highestExp; coeff++)
            res.poly[0][coeff] = poly[0][coeff] + right.poly[0][coeff];

    return res;
}

例如,
case1: >

for example, case1: highest exps are equal

The first (original) polynomial is:
 - 4x^0 + x^1 + 4x^3 - 3x^4
The second polynomial is:
 - x^0 - x^3
The result polynomial is:
 - 5x^0 + x^1 + 3x^3 - 3x^4

case2:最高指数不相等

case2: highest exponents are not equal

The first (original) polynomial is:
 - 4x^0 + x^1 + 4x^3 - 3x^4 (highest exp = 4)
The second polynomial is:
 - x^0 - x^3 (highest exp = 5)
The result polynomial is:
 - 5x^0 + x^1 + 3x^3 - 3x^4 - 33686019x^5 (highest exp = 5)

请帮助!

更新:多项式类

class Polynomial
{
private:
    int **poly;
    int maxExp;
    void createPolynomialArray(int);
public:
    Polynomial();
    Polynomial(int); // constructor
    Polynomial(const Polynomial &); // copy constructor
    ~Polynomial(); // destructor

    // setter
    void setCoefficient(int,int);
    void setPolynomial(int);

    // getters
    int getTerm() const; 
    int getCoefficient(int,int) const; 

    // overloading operators
    void operator=(const Polynomial &); // assignment
    Polynomial operator+(const Polynomial &); // addition    
}


推荐答案

需要

Polynomial Polynomial::operator+(const Polynomial &right)
{
    Polynomial res;
    if(maxExp < right.maxExp)
    {
        res.setPolynomial(right.maxExp);
        int coeff = 0;
        for (; coeff < maxExp; coeff++)
                res.poly[0][coeff] = poly[0][coeff] + right.poly[0][coeff];
        for (; coeff < right.maxExp; coeff++)
                res.poly[0][coeff] = right.poly[0][coeff];
    }
    else
    {
        res.setPolynomial(maxExp);
        int coeff = 0;
        for (; coeff < right.maxExp; coeff++)
                res.poly[0][coeff] = poly[0][coeff] + right.poly[0][coeff];
        for (; coeff < maxExp; coeff++)
                res.poly[0][coeff] = poly[0][coeff];
    }
    return res;
}

您正在阅读超过较短多项式的结尾。

You are reading past the end of the shorter polynomial.

这篇关于overload +运算符添加2个多项式C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆