2011-11-13 77 views
0

我正在写这个程序处理多项式。我在完成作业的最后一部分时遇到了麻烦。我需要按照度数对多项式进行排序,然后在多项式中存在双度时能够减少多项式。我在下面写的代码对两个函数都有分段错误。在我的void reduce(poly &p1)我的错误发生在zc= zc->next。是否有不同的方式来完成这些功能?加上使用链接列出了我的多项式链接列表插入排序和减少多项式C++

class Poly{ 
private : 
    struct term{ 
    double coef ; 
    unsigned exp ; 
    term *next ; 
    } *term_t ; 
public : 
    Poly() ; 
    void insert (float c, int e) ; 
    void isort(Poly &p1); 
    void reduce (Poly &p1); 
    ~Poly() ; 
} ; 
void Poly :: isort (Poly & p1){ 
    term *z; 
    term * zc; 
    term *c; 
    if (term_t == NULL && p1.term_t == NULL) 
    return ; 
    while (p1.term_t != NULL){ 
    c=p1.term_t; 
    p1.term_t = p1.term_t->next; 
    while (p1.term_t!=NULL && c->exp > zc->exp){ 
     z->coef = c->coef; 
     z->exp=c->exp; 
     c=c->next; 
    } 
    z.printPoly(); 
    zc->exp = z->exp; 
    z=z->next; 
    //c=c->next; 
    } 
} 
void Poly :: reduce (Poly & p1){ 
    term *z ; 
    if (term_t == NULL && p1.term_t == NULL) 
    return ; 
    term *temp1; 
    temp1 = p1.term_t ; 
    while (temp1 != NULL){ 
    if (term_t == NULL){ 
     term_t = new term ; 
     z = term_t ; 
    } 
    else{ 
     z -> next = new term ; 
     z = z -> next ; 
    } 
    if (temp1 -> exp == z->exp) 
     temp1->coef= temp1-> coef + z->coef; 
    z->coef = temp1 -> coef; 
    z->exp = temp1->exp; 
    temp1=temp1->next; 
    } 
    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 ; 
    z -> exp = temp1 -> exp ; 
    temp1 = temp1 -> next ; 
    } 
    z -> next = NULL ; 
} 
+2

我很高兴你说,这是问题的功课,但你也应该使用'[功课]'标签。 –

回答

0

IM您可以使用std::liststd::vector或其他一些STL container