【问题标题】:Overloading + operator with classes containing array pointers (C++)使用包含数组指针的类重载 + 运算符 (C++)
【发布时间】:2013-03-13 20:35:36
【问题描述】:

我目前正在编写一个“多项式”类来练习运算符重载。我已经成功地重载了流提取和插入运算符,但是我在使用“+”运算符时遇到了一些问题。

我的类有一个私有指针,它继续在构造函数中创建一个数组。例如,我了解如何使用复数类重载“+”运算符,但我对这个程序感到困惑。

我们将不胜感激寻求解决方案的指导。 谢谢。

#include<iostream>
#include<stdexcept>
using namespace std;

#ifndef POLYNOMIAL_H
#define POLYNOMIAL_H

class Polynomial 
{
      friend istream& operator>>(istream& in, Polynomial& p);
      friend ostream& operator<<(ostream& out, const Polynomial& p);
      public:
             Polynomial(int = 10);
             ~Polynomial();
             void assignExponent();
             Polynomial operator+(const Polynomial& other);

      private:
              int SIZE;
              int *exponents;
              int *polyPtr; //***************
};

#endif

//CONSTRUCTOR
Polynomial::Polynomial(int arraySize)
{
     if(arraySize > 0)
                  SIZE = arraySize;
     else
     {
         cout << "Array size must be greater than 0. Program will now "
              << "Terminate..." << endl;

         system("pause");
         exit(0);
     }

     polyPtr = new int[SIZE]; //*********************
     exponents = new int[SIZE];

     for(int i = 0; i<SIZE; i++)
             polyPtr[i] = 0;

     assignExponent();
};

//DESTRUCTOR
Polynomial::~Polynomial() //******************
{
     delete [] polyPtr;                         
};

//STREAM INSERTION
istream& operator>>(istream& in, Polynomial& p)
{
         for(int i = 0; i<p.SIZE; i++)
         {
               in >> p.polyPtr[i];  //*************        
         }

         return in;
};

//STREAM EXTRACTION
ostream& operator<<(ostream& out, const Polynomial& p)
{
         int exponent;
         for(int i = 0; i<p.SIZE; i++)
         {
               exponent = (p.SIZE - 1) - i;

               if(p.polyPtr[i] != 1)
               {  
                  if(exponent > 0 && exponent != 1)
                           out << p.polyPtr[i] << "x^" << exponent << " + ";  

                  if(exponent == 1)
                           out << p.polyPtr[i] << "x" << " + ";

                  if(exponent == 0)
                           out << p.polyPtr[i];  
               }

               //In order to not display coefficient if = 1
               else
               {
                   if(exponent > 0 && exponent != 1)
                           out << "x^" << exponent << " + ";  

                   if(exponent == 1)
                           out << "x" << " + ";

                   if(exponent == 0)
                           out << p.polyPtr[i];
               }    
         }       

         return out;
}; 

//Assigns a value for exponent
void Polynomial::assignExponent()
{
     for(int i = 0; i<SIZE; i++)
     {
             exponents[i] = (SIZE - 1) - i;        
     }
};

//OVERLOAD OF + OPERATOR
Polynomial Polynomial::operator+(const Polynomial& other)
{
         Polynomial sum(SIZE);
         int difference;

         //If the first polynomial is larger
         if (SIZE > other.SIZE)
         {
            difference = SIZE - other.SIZE;

            for(int i = 0; i<SIZE; i++)
            {
                 if(i - difference < 0)
                      sum.polyPtr[i] = polyPtr[i];

                 else
                 {
                     sum.polyPtr[i] = polyPtr[i] + 
                                  other.polyPtr[i - difference];  
                 }                            
            }         
         }

         //If the second polynomial is larger       **PROBLEM**  
         if(other.SIZE > SIZE)
         {
            difference = other.SIZE - SIZE;

            for(int i = 0; i<other.SIZE; i++)
            {
                 if(i - difference < 0)
                      sum.polyPtr[i] = other.polyPtr[i];

                 else
                 {
                     sum.polyPtr[i] = other.polyPtr[i] + 
                                  polyPtr[i - difference];  
                 }                            
            }         
         }

         //If the polynomials are equal
         if(SIZE == other.SIZE)     
         {
                  for(int i = SIZE-1; i >= 0; i--)
                  {
                          sum.polyPtr[i] = polyPtr[i] + other.polyPtr[i];        
                  }   
         }

         return sum;
};

int main()
{
    int polySize;

    //User enters a size for the first & second polynomial
    cout << "Enter a size for the first polynomial: ";
    cin >> polySize;
    Polynomial pOne(polySize);

    cout << "\nEnter a size for the second polynomial: ";
    cin >> polySize;
    Polynomial pTwo(polySize);

    //User enters in values (Overload of >> operator
    cout << "\n\nEnter in values for the first polynomial, "
         << "in the format - (x x x x): " << endl;
    cin >> pOne;
    cout << "\nEnter in values for the second polynomial, "
         << "in the format - (x x x x): " << endl;
    cin >> pTwo;

    //Overload << operator for output
    cout << "\nPolynomial 1 is: " << pOne << endl
         << "Polynomial 2 is: " << pTwo << endl;

    Polynomial pThree = pOne + pTwo;

    cout << "\nAfter being added together, the new polynomial is: "
         << pThree << endl;

 system("pause");   
} 

我已经更新了我当前的代码,因为我认为打开另一个问题不是最好的方法。无论如何,在尝试排列要添加的多项式时,我已经部分成功了。出现的唯一问题是当第二个多项式 pTwo 大于第一个时。我已经标记了代码部分,PROBLEM。提前致谢。

【问题讨论】:

  • 你应该遵循三法则。更好的是,使用矢量。
  • 在声明 pOnepTwo 之前请注意你正在做 Polynomial pThree = pOne + pTwo;
  • @Andy Prowl - 刚刚解决了这个问题,谢谢。
  • 提示:使用铅笔和纸,如何将 (x^3 + 2x^2 + 3x + 5) + (x^2 + 4x + 6) 相加?那么你的程序应该首先添加{1,2,3,5}{1,4,6}呢?
  • @aschepler - 你说得对,我需要从头开始。

标签: c++ class operator-overloading


【解决方案1】:

我猜+ 应该像下面这样写:

class Polynomial 
{
  // ...

  Polynomial operator+(const Polynomial& other);

 // ...
};

Polynomial Polynomial::operator+(const Polynomial& other)
{
     Polynomial sum(SIZE);

     for(int i = 0; i< SIZE; i++)
     {
         sum.polyPtr[i] = polyPtr[i] + other.polyPtr[i];
     }

     return sum;
}

【讨论】:

  • 成功了!现在我无法返回数组。像这样:return Polynomial(newPoly[]);谢谢
  • @Ericafterdark:+时最好不要返回。 void
  • 您不能将变量SIZE 用作数组大小。 operator+ 应该返回总和。
  • 那我不用回去了?
  • 谢谢。我现在收到的唯一错误是:invalid conversion from 'Polynomial*' to 'int' 当我尝试返回总和时。
猜你喜欢
  • 1970-01-01
  • 2015-07-28
  • 1970-01-01
  • 2014-12-24
  • 1970-01-01
  • 2021-07-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多