0

實現了一個多項式類,其中包含2維D數組,並將多個項作爲數據成員。數組行是係數,數組列是指數。我們的目標是超載+運算符做加法運算,即如果指數相同則加上係數,否則包括術語。具有二維數組成員的多項式類作爲數據成員並使用運算符+重載

輸出部分正確,輸出的最後一項不正確。請考慮運算符+重載並提供補救措施!在此先感謝...

請參閱注意刪除以前的缺陷。

#ifndef POLYNOMIAL_H 
#define POLYNOMIAL_H 

#include <iostream> 
using namespace std; 
#define MAX 10 

class Polynomial { 
     public : 
      Polynomial(); 
      //~Polynomial(){ delete [] terms[MAX]; } (see comments) 
      void enterTerms(); 
      Polynomial operator +(const Polynomial &); 
     private : 
      int terms[MAX][2]; //define size statically if not using "new" 
      int n; //number of terms 
};   
#endif 

#include "polynomial.h" 


Polynomial Polynomial :: operator + (const Polynomial & p){ 
      Polynomial temp, sum; 
      temp.n = n + p.n; 
      int common = 0; 
      //first write sum as concatenation of p1 and p2    
      for (int i = 0 ; i < n ; i++){ 
        temp.terms[i][0] = terms[i][0]; 
        temp.terms[i][1] = terms[i][1]; 
      } 
      //notice j and k for traversing second half of sum, and whole p2 resp 
      for (int j = n, k = 0; j < n + p.n, k < p.n ; j++, k++){ 
        temp.terms[j][0] = p.terms[k][0]; 
        temp.terms[j][1] = p.terms[k][1]; 
      } 
      for (int l = 0; l < temp.n - 1 ; l++){ // 0 to 1 less than length 
       for (int m = l + 1 ; m < temp.n ; m++){ // 1 more than l to length,so that compared pairs are non redundant 
        if(temp.terms[l][1] == temp.terms[m][1]){ 
          common++; //common terms reduce no. of terms 
          temp.terms[l][0] += temp.terms[m][0]; //coefficients added of exponents same 
          temp.terms[m][0] = 0;     
        } 
       } 
      }     
      sum.n = temp.n - common; 
      for (int q = 0, r = 0; q < temp.n, r < sum.n; q++, r++){ 
        if (temp.terms[q][0] == 0) 
         continue; 
        else if (temp.terms[q][0] != 0){ 
          sum.terms[r][0] = temp.terms[q][0]; 
          sum.terms[r][1] = temp.terms[q][1]; 
        } 
      }  
      cout << sum; 
      return sum; 
} 

    Polynomial :: Polynomial(){ 
     for (int i = 0; i < MAX; i++){ 
      terms[i][0] = 0; 
      terms[i][1] = 0; 
     }   
}  
void Polynomial :: enterTerms(){ 
     int num; 
     cout<<"enter number of terms in polynomial\n"; 
     cin >> num; 
     n = num >= 0 ? num : 1; 
     cout << "enter coefficient followed by exponent for each term in polynomial\n"; 
     for (int i = 0; i < n ; i++) 
       cin >> terms[i][0] >> terms[i][1] ; 
} 
    int main(){ 
    Polynomial p1 , p2; 
    p1.enterTerms(); 
    p2.enterTerms(); 
    p1 + p2; 
    system("PAUSE"); 
    return EXIT_SUCCESS; 
} 

op

+2

你看我要刪除尚未分配了'new'運營商的陣列。這是錯誤的。 – legends2k

+0

同意,所以我如何回收內存或我在析構函數看我的構造函數。 – ShivaniDhanked

+1

在構造函數中,你只是初始化你的靜態數組。刪除sholuld只用於dynmaic分配 – 999k

回答

1

Declarate你的最大元素的數組:

private : 
     int terms[MAX][2]; 
+0

SUPER!它的工作原理,但不是多維數組,除了第一維必須指定。你能否詳細說明這裏的含義? – ShivaniDhanked

+0

您必須告訴您的計算機需要多少內存。可以在編譯時靜態(如上所述),也可以在運行時使用new運算符動態獲取剛剛聲明的指針的內存。 – falkb

+0

@falkab,明白了!謝謝! – ShivaniDhanked

0

雖然我知道你的「問題」已被接受的答案解決,可能我說,出渣周圍當你在標準庫中有正確的數據結構時,C++中的指針和數組可以被查看,而不會浪費額外的資源或使用它們。對於一個多項式類,我相信地圖可以做,而且當你有孔時,即0度的係數,你不會像在你的數組實現中那樣浪費空間;另外不用擔心分配,釋放和泄漏內存。我建議你至少考慮一下下面的實現。我向你保證,這可以進一步提高很多。

#include <iostream> 
#include <map> 

using namespace std; 

class Polynomial 
{ 
public: 
    int& coefficient(unsigned degree) 
    { 
     return deg2coeff[degree]; 
    } 

    const int& coefficient(unsigned degree) const 
    { 
     return deg2coeff.at(degree); 
    } 

    size_t size() const 
    { 
     return deg2coeff.size(); 
    } 

    Polynomial operator+(const Polynomial &that) 
    { 
     Polynomial result; 
     result.deg2coeff = (this->size() > that.size()) ? deg2coeff : that.deg2coeff; 
     const auto &smaller = (this->size() <= that.size()) ? *this : that; 
     for (const auto &it : smaller.deg2coeff) 
     { 
      result.coefficient(it.first) += smaller.coefficient(it.first); 
     } 
     return result; 
    } 

    friend std::ostream& operator<<(std::ostream& ostr, const Polynomial &poly); 

private: 
    map<unsigned, int> deg2coeff; 
}; 

std::ostream& operator<<(std::ostream& ostr, const Polynomial &poly) 
{ 
    for (auto it = poly.deg2coeff.crbegin(); it != poly.deg2coeff.crend(); ++it) 
    { 
     if (it->second == -1) 
      ostr << '-'; 
     else if (it->first == 0 || it->second != 1) 
     { 
      if (it->second > 0) 
       ostr << "+"; 
      ostr << it->second; 
     } 

     if (it->first) 
     { 
      ostr << "x"; 
      if (it->first != 1) 
       ostr << "^" << it->first; 
     } 
    } 
    return ostr<<endl; 
} 

void read_polynomial(Polynomial &poly) 
{ 
    cout << "Number of degrees: "; 
    unsigned n; 
    cin >> n; 
    int degree, coeff; 
    for (unsigned i = 0; i < n; ++i) 
    { 
     cout << "Enter Degree, Coefficient: "; 
     cin >> degree >> coeff; 
     if (coeff) 
     { 
      poly.coefficient(degree) = coeff; 
     } 
    } 
} 

int main() 
{ 
    Polynomial p1; 
    read_polynomial(p1); 
    cout << p1; 

    Polynomial p2; 
    read_polynomial(p2); 
    cout << p2; 

    cout << (p1 + p2); 
} 

輸出

Number of degrees: 4 
Enter Degree, Coefficient: 9 2 
Enter Degree, Coefficient: 3 -12 
Enter Degree, Coefficient: 1 3 
Enter Degree, Coefficient: 0 -27 
+2x^9-12x^3+3x-27 
Number of degrees: 2 
Enter Degree, Coefficient: 13 10 
Enter Degree, Coefficient: 11 34 
+10x^13+34x^11 
+10x^13+34x^11+2x^9-12x^3+3x-27 
0
#ifndef POLYNOMIAL_H 
#define POLYNOMIAL_H 

#include <iostream> 
using namespace std; 

#define MAX 10 

class Polynomial { 
     friend ostream &operator<< (ostream &, const Polynomial &); 
     public : 
      Polynomial(); 
      void enterTerms(); 
      Polynomial operator +(const Polynomial &); 
     private : 
      int terms[MAX][2]; //either static size(MAX rows) or use "new" for dynamic allocation 
      int n; //number of terms 
};    

#endif 

#include "polynomial.h" 

ostream &operator<< (ostream & out, const Polynomial & p){ 
     for (int i = 0 ; i < p.n ; i++){ 
      if (i == p.n - 1)//last term does not have + appended 
        out << p.terms[i][0] <<"x^"<<p.terms[i][1]<<endl; 
      else 
        out << p.terms[i][0]<<"x^"<<p.terms[i][1]<<" + "; 
     } 
     return out; 
} 
Polynomial :: Polynomial(){ 
     for (int i = 0; i < MAX; i++){ 
      terms[i][0] = 0; 
      terms[i][1] = 0; 
     }   
}  
void Polynomial :: enterTerms(){//enterTerms() not in constructor so that no prompt for entering 
//terms while doing + - etc., they also produce Poylnomial instance (i.e. invoke constructor) 
     int num; 
     cout<<"enter number of terms in polynomial\n"; 
     cin >> num; 
     n = num >= 0 ? num : 1; 
     cout << "enter coefficient followed by exponent for each term in polynomial\n"; 
     for (int i = 0; i < n ; i++) 
       cin >> terms[i][0] >> terms[i][1] ; 
} 
Polynomial Polynomial :: operator + (const Polynomial & p){ 
      Polynomial temp, sum; 
      temp.n = n + p.n; 
      int common = 0; 

      // first write sum as concatenation of p1 and p2    
      for (int i = 0 ; i < n ; i++){ 
        temp.terms[i][0] = terms[i][0]; 
        temp.terms[i][1] = terms[i][1]; 
      } 
      //notice j and k for traversing second half of sum, and whole p2 resp 
      for (int j = n, k = 0; j < n + p.n, k < p.n ; j++, k++){ 
        temp.terms[j][0] = p.terms[k][0]; 
        temp.terms[j][1] = p.terms[k][1]; 
      } 
      for (int l = 0; l < temp.n - 1 ; l++){ // 0 to 1 less than length 
       for (int m = l + 1 ; m < temp.n ; m++){ // 1 more than l to length,so that compared pairs are non redundant 
        if(temp.terms[l][1] == temp.terms[m][1]){ 
          common++; //common terms reduce no. of terms in sum (see sum.n decl) 
          temp.terms[l][0] += temp.terms[m][0]; //coefficients added if exponents same 
          temp.terms[m][0] = 0;     
        } 
       } 
      } 
      sum.n = temp.n - common; //if you place it above, common taken as 0 and sum.n is same as temp.n (logical error)   

      //just to debug, print temporary array 
      cout << endl << temp; 

      for (int q = 0, r = 0; q < temp.n; q++){ 
        if (temp.terms[q][0] == 0) 
         continue; 
        else{ 
         sum.terms[r][0] = temp.terms[q][0]; 
         sum.terms[r][1] = temp.terms[q][1]; 
         r++; 
        } 
      } 

      cout << endl << sum; 
      return sum; 
}      
int main(){ 
    Polynomial p1 , p2; 
    p1.enterTerms(); 
    p2.enterTerms(); 
    p1 + p2; 
    system("PAUSE"); 
    return EXIT_SUCCESS; 
} 

op

+0

對於那些有興趣的人,可以嘗試在沒有temp數組的情況下在operator +函數中執行此操作。這是可能的,並將與循環樂趣! – ShivaniDhanked