c++ beginner - class and passing

I am trying to print these polynomials but I can not seem to print what I want. If my polynomial was 1*x + 2*x^2 then output would be 4*x. I want 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;
}

I have a class poly and a struct term_t with coef and exp in them.

Answers


Personally, I'd have the derivative function return a Poly, rather than modify the existing one. Something like this

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;
}

As for the printing problem though, yeah, you haven't provided enough of your code to really know what's going on. So, I wrote my own... this is how I'd approach the problem. First a Term class like this

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;
};

then a Poly class like this

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;
};

That'd leave the memory management of the Term objects up to the class users, but you could also write a destructor for the Poly class that deletes them.


Need Your Help

Static vs global in terms of speed and space consumption in C

c gcc static embedded global

I would like to know difference between static variables and global variables in terms of access speed and space consumption. (If you want to know my platform: gcc compiler on Windows. (I am using ...

Order of condition execution in MySQL

mysql database

Suppose I have a MySQL query with two conditions:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.