【问题标题】:Beginner polynomial program in C++C ++中的初学者多项式程序
【发布时间】: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.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 中编译

【问题讨论】:

  • 看起来很奇怪,你得到了这个Poly.h,正如答案所指出的那样,这并不正确。
  • @sukhmel 我同意,这很奇怪,我已经通过电子邮件向教授发送了有关该错误的信息。虽然他还没有回信,但我认为这一定是他的错误。

标签: c++ arrays visual-studio visual-c++


【解决方案1】:

与您发布的代码有关的几件事。

  1. 您需要在Poly.h中切换以下几行:

    #define POLY_H
    #ifndef POLY_H
    

    否则,文件中的任何内容都不会包含在内。

  2. 这是一种不好的做法

    using namespace std;
    

    在 .h 文件中。使用显式类型名称,例如 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.:记得#include cstring 使用memset

【讨论】:

  • 非常感谢您的回答,这真的很有帮助,我现在觉得我有了一个不错的开始。然而,我对这个问题的默认构造函数有点挣扎,希望你能提供类似的见解。说明指出类构造函数必须将多项式(类对象)初始化为 0 的 0 次多项式。我有点困惑,在这种情况下,我是否应该首先将整个数组初始化为 0-MAXPOLY,然后poly 为 0 或其中之一。有什么建议吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-01
  • 2011-07-12
  • 2010-10-06
  • 2013-04-17
  • 1970-01-01
相关资源
最近更新 更多