C++ c++;初级班与传球

C++ c++;初级班与传球,c++,class,C++,Class,我试图打印这些多项式,但我似乎无法打印我想要的。 如果我的多项式是1*x+2*x^2,那么输出将是4*x。我想要1+4*x void Poly :: derivative (){ term *z ; if ( term_t == NULL) return ; term *temp1; temp1 = term_t; while ( temp1 != NULL ){ if ( term_t == NULL ){ term_t = new term ;

我试图打印这些多项式,但我似乎无法打印我想要的。 如果我的多项式是
1*x+2*x^2
,那么输出将是
4*x
。我想要
1+4*x

void Poly :: derivative (){
  term *z ;
  if ( term_t == NULL)
    return ;
  term *temp1;
  temp1 = term_t;
  while ( temp1 != NULL ){
   if ( term_t == NULL ){
     term_t = new term ;
      z = term_t ;
    }
    else{
      z -> next = new term ;
      z = z -> next ;
    }
    z-> coef = temp1 -> coef * temp1 -> exp;
    z-> exp = temp1 -> exp - 1;
    temp1 = temp1 -> next;
  }
  term_t=z;
}

我有一个类
poly
和一个结构
term\u t
,其中包含
coef
exp

就个人而言,我希望导数函数返回一个多边形,而不是修改现有的多边形。像这样的

Poly derivative() {
    Poly res;
    Term *temp1 = this->term_t;
    while (temp1 != NULL) {
      int nexp = temp1->getExp() - 1;
      int ncoef = temp1->getCoef() * temp1->getExp();
      res.add(new Term(ncoef, nexp));
      temp1 = temp1->getNext();
    }
    return res;
}
class Term {
public:
  Term(int coef, int exp) {
    this->coef = coef;
    this->exp = exp;
    this->next = NULL;
  }

  std::string toString() const {
    std::stringstream ss;
    if (this->coef == 0) return "0";
    if (this->exp == 0) ss << this->coef;
    else {
      if (this->coef != 1) ss << this->coef;
      ss << "x";
      if (this->exp != 1) ss << "^" << this->exp;
    }
    return ss.str();
  }

  int getCoef() const {return this->coef;}
  int getExp() const {return this->exp;}
  Term* getNext() const {return this->next;}
  void setNext(Term* n) {this->next = n;}

private:
  int coef;
  int exp;
  Term* next;
};
class Poly {
public:
  Poly() {
    this->term_t = NULL;
  }
  void add(Term* nt) {
    if (this->term_t == NULL) {
      this->term_t = nt;
    } else {
      Term* t = this->term_t;
      while(t->getNext() != NULL) t = t->getNext();
      t->setNext(nt);
    }
  }
  std::string toString() const {
    std::stringstream ss;
    Term* t = this->term_t;
    while (t != NULL) {
      ss << t->toString();
      if (t->getNext() != NULL) ss << " + ";
      t = t->getNext();
    }
    return ss.str();
  }

  Poly derivative() {
    Poly res;
    Term *temp1 = this->term_t;
    while (temp1 != NULL){
      int nexp = temp1->getExp() - 1;
      int ncoef = temp1->getCoef() * temp1->getExp();
      res.add(new Term(ncoef, nexp));
      temp1 = temp1->getNext();
    }
    return res;
  }

private :
  Term* term_t;
};
至于打印问题,是的,您没有提供足够的代码来真正了解发生了什么。所以,我写了我自己的。。。这就是我处理这个问题的方法。首先是这样的学期课

Poly derivative() {
    Poly res;
    Term *temp1 = this->term_t;
    while (temp1 != NULL) {
      int nexp = temp1->getExp() - 1;
      int ncoef = temp1->getCoef() * temp1->getExp();
      res.add(new Term(ncoef, nexp));
      temp1 = temp1->getNext();
    }
    return res;
}
class Term {
public:
  Term(int coef, int exp) {
    this->coef = coef;
    this->exp = exp;
    this->next = NULL;
  }

  std::string toString() const {
    std::stringstream ss;
    if (this->coef == 0) return "0";
    if (this->exp == 0) ss << this->coef;
    else {
      if (this->coef != 1) ss << this->coef;
      ss << "x";
      if (this->exp != 1) ss << "^" << this->exp;
    }
    return ss.str();
  }

  int getCoef() const {return this->coef;}
  int getExp() const {return this->exp;}
  Term* getNext() const {return this->next;}
  void setNext(Term* n) {this->next = n;}

private:
  int coef;
  int exp;
  Term* next;
};
class Poly {
public:
  Poly() {
    this->term_t = NULL;
  }
  void add(Term* nt) {
    if (this->term_t == NULL) {
      this->term_t = nt;
    } else {
      Term* t = this->term_t;
      while(t->getNext() != NULL) t = t->getNext();
      t->setNext(nt);
    }
  }
  std::string toString() const {
    std::stringstream ss;
    Term* t = this->term_t;
    while (t != NULL) {
      ss << t->toString();
      if (t->getNext() != NULL) ss << " + ";
      t = t->getNext();
    }
    return ss.str();
  }

  Poly derivative() {
    Poly res;
    Term *temp1 = this->term_t;
    while (temp1 != NULL){
      int nexp = temp1->getExp() - 1;
      int ncoef = temp1->getCoef() * temp1->getExp();
      res.add(new Term(ncoef, nexp));
      temp1 = temp1->getNext();
    }
    return res;
  }

private :
  Term* term_t;
};
类术语{
公众:
术语(int coef,int exp){
这->coef=coef;
这个->exp=exp;
此->下一步=空;
}
std::string toString()常量{
std::stringstream-ss;
如果(this->coef==0)返回“0”;
如果(此->exp==0)ss系数;
否则{
如果(此->系数!=1)ss系数;
ss exp!=1)ss coef;}
int getExp()常量{返回此->exp;}
Term*getNext()常量{返回此->下一步;}
void setNext(Term*n){this->next=n;}
私人:
内系数;
国际贸易;
术语*下一个;
};
然后是这样的一个多边形类

Poly derivative() {
    Poly res;
    Term *temp1 = this->term_t;
    while (temp1 != NULL) {
      int nexp = temp1->getExp() - 1;
      int ncoef = temp1->getCoef() * temp1->getExp();
      res.add(new Term(ncoef, nexp));
      temp1 = temp1->getNext();
    }
    return res;
}
class Term {
public:
  Term(int coef, int exp) {
    this->coef = coef;
    this->exp = exp;
    this->next = NULL;
  }

  std::string toString() const {
    std::stringstream ss;
    if (this->coef == 0) return "0";
    if (this->exp == 0) ss << this->coef;
    else {
      if (this->coef != 1) ss << this->coef;
      ss << "x";
      if (this->exp != 1) ss << "^" << this->exp;
    }
    return ss.str();
  }

  int getCoef() const {return this->coef;}
  int getExp() const {return this->exp;}
  Term* getNext() const {return this->next;}
  void setNext(Term* n) {this->next = n;}

private:
  int coef;
  int exp;
  Term* next;
};
class Poly {
public:
  Poly() {
    this->term_t = NULL;
  }
  void add(Term* nt) {
    if (this->term_t == NULL) {
      this->term_t = nt;
    } else {
      Term* t = this->term_t;
      while(t->getNext() != NULL) t = t->getNext();
      t->setNext(nt);
    }
  }
  std::string toString() const {
    std::stringstream ss;
    Term* t = this->term_t;
    while (t != NULL) {
      ss << t->toString();
      if (t->getNext() != NULL) ss << " + ";
      t = t->getNext();
    }
    return ss.str();
  }

  Poly derivative() {
    Poly res;
    Term *temp1 = this->term_t;
    while (temp1 != NULL){
      int nexp = temp1->getExp() - 1;
      int ncoef = temp1->getCoef() * temp1->getExp();
      res.add(new Term(ncoef, nexp));
      temp1 = temp1->getNext();
    }
    return res;
  }

private :
  Term* term_t;
};
类多边形{
公众:
Poly(){
此->术语\u t=NULL;
}
无效添加(术语*nt){
如果(此->术语\u t==NULL){
本->术语\u t=nt;
}否则{
术语*t=此->术语\u t;
而(t->getNext()!=NULL)t=t->getNext();
t->setNext(nt);
}
}
std::string toString()常量{
std::stringstream-ss;
术语*t=此->术语\u t;
while(t!=NULL){
ss-toString();
如果(t->getNext()!=NULL)ss getNext();
}
返回ss.str();
}
多元导数(){
Poly-res;
术语*temp1=此->术语;
while(temp1!=NULL){
int nexp=temp1->getExp()-1;
int ncoef=temp1->getCoef()*temp1->getExp();
决议增补(新条款(ncoef,nexp));
temp1=temp1->getNext();
}
返回res;
}
私人:
术语*术语t;
};

这会将术语对象的内存管理留给类用户,但您也可以为Poly类编写一个析构函数来删除它们。

您的
Poly::derivative()
方法与打印Poly有什么关系?您提供的代码片段与您的问题无关。