【发布时间】:2014-11-04 21:42:08
【问题描述】:
我正在为我的 C++ 编程课程的练习寻找一些帮助。不幸的是,这周我病得很重,无法上课,这意味着我只能将教科书用作资源,所以在编写这个程序时我很迷茫。因此,我想我会在这里寻求真正的专业人士的帮助。我意识到这很广泛,但任何帮助和/或提示将不胜感激。以下是我们得到的指示:
此作业处理使用简单数组表示和操作多项式。一种 多项式,例如 anxn + an-1xn-1 + … + a0,将被实现为一个系数数组,其中 > 系数 ai 存储在数组的位置 i 中。系数是浮点值 (可能是负数),所以我们将使用一个 double 类型的数组。数组的大小为 MAXPOLY (一个设置为 50 的常数变量),因此我们将仅限于持有最大的多项式 MAXPOLY 的度数 - 1(或 49)。
Poly.h 文件描述了该类提供的所有功能。
您将实现以下一组功能:
- 将多项式初始化为零的默认构造函数 多项式
- setCoeff 设置多项式中的特定系数
- retrieveCoeff 从多项式中获取特定系数
- incrementCoeff 将值添加到多项式中的特定系数
- degree 确定多项式的次数
- numOfTerms 确定多项式中的项数(即有多少数组元素非零)
- evaluate 计算给定 X 值的多项式
- add 将一个多项式添加到另一个多项式,更改添加到的多项式
- derivative 计算多项式的导数
- equals 确定两个多项式是否相等为您提供了几个函数:(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 我同意,这很奇怪,我已经通过电子邮件向教授发送了有关该错误的信息。虽然他还没有回信,但我认为这一定是他的错误。
标签: c++ arrays visual-studio visual-c++