Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/146.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C+中的多项式初学者程序+; 我正在为我的C++编程课寻找练习的帮助。不幸的是,我这周病得很重,不能上课,这意味着我只能把课本作为一种资源,所以当我写这个程序时,我非常迷茫。因此,我想我应该向这里真正的专业人士寻求帮助。我知道这是广泛的,但任何帮助和/或提示将不胜感激。以下是我们收到的指示:_C++_Arrays_Visual Studio_Visual C++ - Fatal编程技术网

C+中的多项式初学者程序+; 我正在为我的C++编程课寻找练习的帮助。不幸的是,我这周病得很重,不能上课,这意味着我只能把课本作为一种资源,所以当我写这个程序时,我非常迷茫。因此,我想我应该向这里真正的专业人士寻求帮助。我知道这是广泛的,但任何帮助和/或提示将不胜感激。以下是我们收到的指示:

C+中的多项式初学者程序+; 我正在为我的C++编程课寻找练习的帮助。不幸的是,我这周病得很重,不能上课,这意味着我只能把课本作为一种资源,所以当我写这个程序时,我非常迷茫。因此,我想我应该向这里真正的专业人士寻求帮助。我知道这是广泛的,但任何帮助和/或提示将不胜感激。以下是我们收到的指示:,c++,arrays,visual-studio,visual-c++,C++,Arrays,Visual Studio,Visual C++,此赋值处理使用简单数组表示和操作多项式。A. 多项式,例如anxn+an-1xn-1+…+a0,将被实现为系数数组,其中>系数ai存储在数组的位置i中。这些系数是浮点值 (可能是负数),因此我们将使用double类型的数组。阵列的大小将为MAXPOLY (一个常数变量设置为50),因此我们将仅限于持有最大值的多项式 最大聚合度-1(或49)。 文件Poly.h描述了类提供的所有函数 您需要实现以下一组功能: -将多项式初始化为零的默认构造函数 多项式 -setCoeff设置多项式中的特定系数 -

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

文件Poly.h描述了类提供的所有函数

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

我们为您提供了几个函数:(1)将向您提供toString函数,以便>所有多项式将以相同方式显示;(2)定义了插入运算符,以便>轻松打印多项式;(3)等式、不等式、,并提供了加法运算符>,它们只是根据equals和add函数定义的。您不应更改任何提供的函数

您将获得两个启动文件,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
#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
定义多边形 #ifndef POLY_H #包括 使用名称空间std; const size\u t MAXPOLY=50; 类聚 { 私人: //数据成员[ADT数据对象的实现] //用于保存多边形的系数的数组 双系数[MAXPOLY]; 公众: //默认类构造函数:将多项式初始化为常量0 //注意:coeff[]的所有数组元素必须设置为0.0 Poly(); //度数:查找多项式的度数(系数非零的最高幂) 大小度()常数; //setCoeff:在多项式中设置一个项,值*x^i //如果索引i不满足前提条件,则抛出。 无效设置系数(双值,大小i); //retrieveCoeff:查找多边形中x^i项的系数 //如果索引i不满足前提条件,则抛出。 双检索Coeff(大小)常数; //incrementCoeff:更改多项式中的项,值*x^i //如果索引i不满足前提条件,则抛出。 无效增量系数(双倍值,大小i); //toString:生成多边形对象的字符串表示形式 //注意:此函数是为您提供的--请勿更改! 字符串toString()常量; //numOfTerms:返回多项式中的项数。 大小\u t numOfTerms()常数; //求值:为指定的X值求值多项式 双评价(双x)常数; //添加:将一个多项式添加到另一个多项式 无效添加(const Poly&aPoly); //加法运算符:将两个多项式相加,并返回一个新的多项式作为结果 //注意:此函数是为您提供的--请勿更改! 多边形运算符+(常量多边形和rhs)常量; //等于:确定两个多项式是否相等 布尔等于(常数Poly和aPoly)常数; //等式/不等式运算符 //注意:这些函数是为您提供的--请勿更改! 布尔运算符==(常数多边形和rhs)常数; 布尔运算符!=(常数多边形和rhs)常数; //导数:计算多项式的导数 无效导数(); //用于输出的插入运算符 //注意:此函数是为您提供的--请勿更改! friend-ostream&operator=0;i--) { 双c=retrieveCoeff(i); 如果(c!=0.0) { printedSomething=true; 如果(i==0) { result.setf(ios::showpos);
结果与您发布的代码有关的几件事

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

    #define POLY_H
    #ifndef POLY_H
    
    否则,将不包括文件中的任何内容

  • 这是一种不好的习惯

    using namespace std;
    
    在.h文件中。使用显式类型名,例如
    std::string
    std::ostream

  • 谈到您的主要障碍,您必须了解如何实现
    Poly.cpp中的函数
    
    void testSetCoeff();
    
    int main()
    {
       testSetCoeff();
       return 0;
    }
    
    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";
       }
    }
    
       if ( almostEqual(c, 1.0) )
    
       if ( c == 1.0 )
    
    bool almostEqual(double x, double y)
    {
       static double const tolerance = 1.0E-6;
       return (fabs(x-y) < tolerance);
    }
    
    #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";
       }
    }
    
    // setCoeff
    void Poly::setCoeff (double value, size_t i)
    {
       coeff[i] = value;
    }
    
    // retrieveCoeff
    double Poly::retrieveCoeff (size_t i) const
    {
       return coeff[i];
    }
    
    Poly::Poly ()
    {
       memset(coeff, 0, sizeof(coeff));
    }