2014-09-11 24 views
2

我正在爲我的C++編程類尋找一些練習幫助。不幸的是,本週我病得很厲害,無法上課,這意味着我只能將教科書作爲資源使用,所以在編寫這個程序時我很迷茫。因此,我想我會轉向這裏的真正專業人士尋求幫助。我意識到這是廣泛的,但任何幫助和/或提示將不勝感激。以下是我們給出的說明:C++中的初學者多項式程序

該賦值處理使用簡單數組表示和操作多項式。諸如焦慮+ an-1xn-1 + ... + a0之類的多項式將被實現爲係數陣列,係數ai被存儲在陣列的位置i中。係數是浮點數值 (可能爲負數),所以我們將使用double類型的數組。該陣列的尺寸爲MAXPOLY (恆定變量設置爲50),因此我們將被限制爲保持具有最大 程度MAXPOLY-1(或49)的多項式。

文件Poly.h描述了該類提供的所有功能。

你要實現以下的功能集:
- 默認構造函數初始化一個多項式零 多項式
- setCoeff設置特定的係數多項式
- retrieveCoeff獲得從多項式
特定係數 - incrementCoeff到的值添加到特定係數在多項式
- 瓦特HICH確定多項式
度 - numOfTerms,其確定在所述多項式的項數(即,陣列有多少個元素爲非零)
- 評估其評估爲X
的給定值的多項式 - 添加這增加了一個多項式到另一個,改變多項式加到
- 衍生物其計算的多項式
的衍生物 - 等於其確定的平等兩個多項式

幾個功能爲您提供:(1)的toString功能將提供給你,讓>我們的所有多項式將同樣顯示,(2)插入運算符定義,所以我們可以>很容易打印一個多項式,(3)提供了等式,不等式和加法運算符,並且簡單地根據等式和加法函數定義。你不應該改變任何提供的功能。

您將提供兩個入門文件,Poly.cppPoly.h。類聲明 文件Poly.h包含一個名爲Poly的類的完整規範。你的任務是實現類定義文件Poly.cpp中的所有指定函數(除了爲你提供的幾個函數外)。您還被提供了初始測試>程序PolyTest.cpp。您應該將代碼添加到PolyTest.cpp文件以充分測試Poly類(從您爲Project#1-Pre創建的PolyTest.cpp文件複製代碼)。

我們確實提供了這些文件。該Poly.h文件看起來像這樣:

#define POLY_H 
#ifndef POLY_H 
#include <string> 
using namespace std; 

const size_t MAXPOLY = 50;  

class Poly 
{ 

private: 
    // Data members [implementation of ADT's data object] 

    // array for holding the coefficients of the poly 
    double coeff[MAXPOLY];    
public: 

    // Default Class constructor: initializes a polynomial to the constant 0 
    // note: all array elements of coeff[] must be set to 0.0 
    Poly(); 

    // degree: finds the degree of a polynomial (the highest power with a non-zero coefficient) 
    size_t degree() const; 

    // setCoeff: sets a term, value*x^i, in a polynomial 
     // Throws <std::out_of_range> if index i does not meet the precondition. 
    void setCoeff (double value, size_t i); 

    // retrieveCoeff: finds the coefficient of the x^i term in poly 
    // Throws <std::out_of_range> if index i does not meet the precondition. 
    double retrieveCoeff (size_t i) const; 

    // incrementCoeff: changes a term, value*x^i, in a polynomial 
    // Throws <std::out_of_range> if index i does not meet the precondition. 
    void incrementCoeff(double value, size_t i); 

    // toString: produce a string representation of a Poly object 
    // note: This function has been provided for you -- DO NOT CHANGE IT! 
    string toString() const; 

    // numOfTerms: returns the number of terms in the polynomial. 
    size_t numOfTerms() const; 

    // evaluate: evaluate a polynomial for a specified value of X 
    double evaluate (double x) const; 

    // add: add one polynomial to another 
    void add (const Poly& aPoly); 

    // addition operator: add two polynomials together and return a new polynomial that is the result 
    // note: This function has been provided for you -- DO NOT CHANGE IT! 
    Poly operator+ (const Poly& rhs) const; 

    // equals: determine if two polynomials are equal 
    bool equals (const Poly& aPoly) const; 

    // Equality/inequality operators 
    // note: These functions have been provided for you -- DO NOT CHANGE IT! 
    bool operator== (const Poly& rhs) const; 
    bool operator!= (const Poly& rhs) const; 

    // derivative: compute the derivative of a polynomial 
    void derivative(); 

    // insertion operator for output 
    // note: This function has been provided for you -- DO NOT CHANGE IT! 
    friend ostream& operator<< (ostream& os, const Poly &p); 
}; 

#endif 

Poly.cpp文件看起來像這樣:

#include <iostream> 
#include <sstream> 
#include <stdexcept> 
#include <cmath> 
#include "Poly.h" 
using namespace std; 


// Class constructor 
Poly::Poly() 
{ 
    //ADD YOUR CODE HERE 
} 

// degree 
size_t Poly::degree() const 
{ 
    //ADD YOUR CODE HERE 
} 

// setCoeff 
void Poly::setCoeff (double value, size_t i) 
{ 
    // ADD YOUR CODE HERE 
} 

// retrieveCoeff 
double Poly::retrieveCoeff (size_t i) const 
{ 
    return 0; // REPLACE WITH YOUR CODE 
} 

// incrementCoeff 
void Poly::incrementCoeff(double value, size_t i) 
{ 
    // ADD YOUR CODE HERE 
} 

// toString 
string Poly::toString() const 
{ 
    ostringstream result; 
    bool printedSomething = false; 
    for (int i=(int)degree(); i>=0; i--) 
    { 
      double c = retrieveCoeff(i); 
      if (c != 0.0) 
     { 
      printedSomething = true; 
      if (i == 0) 
     { 
       result.setf(ios::showpos); 
       result << " " << c; 
       result.unsetf(ios::showpos); 
      } 
      else 
     { 
       result.setf(ios::showpos); 
       result << " " << c; 
       result.unsetf(ios::showpos); 
       result << "*X^" << i; 
      } 
      } 
     } 
    if (!printedSomething) 
    { 
     result.setf(ios::showpos); 
     result << " " << 0; 
     result.unsetf(ios::showpos); 
    } 
    return result.str(); 
} 


// numOfTerms 
size_t Poly::numOfTerms() const 
{ 
    return 0; // REPLACE WITH YOUR CODE 
} 

// evaluate 
double Poly::evaluate (double x) const 
{ 
    return 0; // REPLACE WITH YOUR CODE 
} 

// add 
void Poly::add (const Poly& aPoly) 
{ 
    // ADD YOUR CODE HERE 
} 

// addition operator 
Poly Poly::operator+ (const Poly& rhs) const 
{ 
    Poly result; 
    result.add(*this); 
    result.add(rhs); 
    return result; 
} 

// equals 
bool Poly::equals (const Poly& aPoly) const 
{ 
    return false; // REPLACE WITH YOUR CODE 
} 

// Equality/inequality operators 
bool Poly::operator== (const Poly& rhs) const 
{ 
    return equals(rhs); 
} 

bool Poly::operator!= (const Poly& rhs) const 
{ 
    return !equals(rhs); 
} 

// derivative 
void Poly::derivative() 
{ 
    // ADD YOUR CODE HERE 
} 

// Friend operator for printing a Poly object. 
ostream & operator << (ostream &out, const Poly& p) 
{ 
    out << p.toString(); 
    return out; 
} 

#endif 

雖然我有C++的一個基本的瞭解,這只是第二一週的課程(顯然是一個錯過的錯誤),所以我仍然處於學習階段。任何幫助,即使它只是一個開始的地方,將不勝感激。謝謝!

注:我編譯在Microsoft Visual Studio,如果這是任何幫助

+0

看起來很奇怪,你提供了這個'Poly.h',因爲它是由答案指出的,這是不正確的。 – sukhmel 2014-09-11 05:43:08

+0

@sukhmel我同意,這很奇怪,並且自從我向教授發送關於錯誤的電子郵件。雖然他還沒有回信,但我認爲這一定是他的錯誤。 – Nea 2014-09-11 21:55:10

回答

3

夫婦的事情與你貼的代碼。

  1. 你需要切換以下行Poly.h

    #define POLY_H 
    #ifndef POLY_H 
    

    否則,從文件中沒有獲取包括在內。

  2. 這是一個不好的做法在.h文件中使用

    using namespace std; 
    

    。使用明確的類型名稱,例如std::stringstd::ostream

即將到來的主要障礙,你必須弄清楚如何實現Poly.cpp中的功能。您可以使用測試驅動的方法來填充文件的內容。

假設您有一個名爲TestPoly.cpp的文件。該文件包含main函數並驅動對Poly的實現進行測試。

你可以這樣開始:

void testSetCoeff(); 

int main() 
{ 
    testSetCoeff(); 
    return 0; 
} 

你會如何實現testSetCoeff

這裏的東西開始:

void testSetCoeff() 
{ 
    std::cout << "Testing setCoeff()/retrieveCoeff(): "; 

    // Construct an instance of Poly. 
    Poly p; 

    // Set the 0-the coefficient.  
    p.setCoeff(1.0, 0); 

    // Retrieve the same coefficient. 
    double c = p.retrieveCoeff(0); 

    // Make sure that we get the same value. 
    if (almostEqual(c, 1.0)) 
    { 
     std::cout << "SUCCESS\n"; 
    } 
    else 
    { 
     std::cout << "FAILURE\n"; 
    } 
} 

戰略遵循的功能:

  1. 設置對象上的一些數據。
  2. 從同一個對象中檢索數據。
  3. 確保您找回有意義的值。爲它添加一個適當的測試。

在功能上面,我選擇使用的

if (almostEqual(c, 1.0)) 

代替

if (c == 1.0) 

,以確保我們能夠處理浮點表示的不精確的性質。

almostEqual實現是這樣的:

bool almostEqual(double x, double y) 
{ 
    static double const tolerance = 1.0E-6; 
    return (fabs(x-y) < tolerance); 
} 

把這些都在一起,的TestPoly.cc入門版的內容將是:

#include "Poly.h" 

#include <iostream> 
#include <cmath> 

bool almostEqual(double x, double y); 
void testSetCoeff(); 

int main() 
{ 
    testSetCoeff(); 
    return 0; 
} 

bool almostEqual(double x, double y) 
{ 
    static double const tolerance = 1.0E-6; 
    return (fabs(x-y) < tolerance); 
} 

void testSetCoeff() 
{ 
    std::cout << "Testing setCoeff()/retrieveCoeff(): "; 

    // Construct an instance of Poly. 
    Poly p; 

    // Set the 0-the coefficient.  
    p.setCoeff(1.0, 0); 

    // Retrieve the same coefficient. 
    double c = p.retrieveCoeff(0); 

    // Make sure that we get the same value. 
    if (almostEqual(c, 1.0)) 
    { 
     std::cout << "SUCCESS\n"; 
    } 
    else 
    { 
     std::cout << "FAILURE\n"; 
    } 
} 

隨着Poly.cpp目前的狀態,你將獲得FAILURE的地位。現在你可以去Poly.cpp並找出如何改變setCoeffretrieveCoeff的實現來使測試通過。

// setCoeff 
void Poly::setCoeff (double value, size_t i) 
{ 
    coeff[i] = value; 
} 

// retrieveCoeff 
double Poly::retrieveCoeff (size_t i) const 
{ 
    return coeff[i]; 
} 

然後,您可以開始添加其他測試。他們很可能首先失敗。然後你執行必要的功能,直到這些測試通過。

更新,響應OP的評論

係數可以使用memset構造函數初始化爲0

Poly::Poly() 
{ 
    memset(coeff, 0, sizeof(coeff)); 
} 

P.S:記住#包括cstring使用memset

+1

非常感謝你的回答,這真的很有幫助,我現在覺得我已經有了一個體面的開始。然而,我對這個問題的默認構造函數掙扎了一番,並希望你能夠提供類似的見解。這些指令指出類構造函數必須將多項式(類對象)初始化爲0次多項式0。我略微困惑,在這種情況下,我應該首先初始化整個數組中的0-MAXPOLY,然後多邊形在0或一個或另一個。有什麼建議? – Nea 2014-09-11 21:53:00

+1

@Nea,看我的更新。 – 2014-09-12 02:21:54