C ++中的初学多项式程序

时间:2014-09-11 04:17:51

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

我正在为我的C ++编程课练习寻求帮助。不幸的是,本周我病得很重,无法上课,这意味着我只能使用教科书作为资源,所以在编写这个程序时我很丢失。因此,我想我会转向真正的专业人士寻求帮助。我意识到这是广泛的,但任何帮助和/或提示将不胜感激。以下是我们给出的说明:

  

此赋值用于处理使用简单数组表示和操作多项式。一个   多项式,例如anxn + an-1xn-1 + ... + a0,将被实现为系数阵列,其中>系数ai存储在阵列的位置i中。系数是浮点值   (可能是否定的),所以我们将使用double类型的数组。该数组的大小为 MAXPOLY   (一个常数变量设置为50)因此我们将限制为保持多项式最大值   度 MAXPOLY - 1(或49)。

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

  您将实现以下功能集:
    - 将多项式初始化为零的默认构造函数      多项式
    - setCoeff 在多项式中设置特定系数
    - retrieveCoeff 从多项式中获取特定系数
    - incrementCoeff 将值添加到多项式中的特定系数
    - 确定多项式的次数
    - numOfTerms 确定多项式中的项数(即,有多少数组元素为非零)
    - evaluate ,它评估给定X X值的多项式     - add ,它将一个多项式添加到另一个,将更改的多项式更改为
    - 导数计算多项式的导数
    - 等于,它确定两个多项式的相等性

     

为您提供了几个函数:(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进行编译,如果有任何帮助的话

1 个答案:

答案 0 :(得分:3)

与您发布的代码相关的一切。

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

    #define POLY_H
    #ifndef POLY_H
    

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

  2. 使用

    是一种不好的做法
    using namespace std;
    
    <。>在.h文件中。使用显式类型名称,例如std::stringstd::ostream

  3. 遇到主要障碍,你必须弄清楚如何在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. 确保您获得有意义的价值。为它添加适当的测试。
    4. 在上面的函数中,我选择使用

         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