我正在爲我的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.cpp和Poly.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,如果這是任何幫助
看起來很奇怪,你提供了這個'Poly.h',因爲它是由答案指出的,這是不正確的。 – sukhmel 2014-09-11 05:43:08
@sukhmel我同意,這很奇怪,並且自從我向教授發送關於錯誤的電子郵件。雖然他還沒有回信,但我認爲這一定是他的錯誤。 – Nea 2014-09-11 21:55:10