2012-03-09 3 views
0

다항식 (계수 및 지수)을 계수, 지수 및 다음 노드에 대한 포인터의 구조로 연결된 목록에 가져 오는 프로젝트가 있습니다. 저장소를 모두 설정했지만 운영자에게 문제가 있습니다. 나는 모든 것을 얻었지만 문제가있는 그 메신저를 뛰어 넘습니다.연산자와 연결된 목록 ADT

지금까지 나는 그것을 실행하는 것

Poly Poly::operator+ (const Poly& orig){ 
    bool firstTime = 0; 
    Poly temp; 
    temp.Head = new PolyTerm; 
    temp.Size = 1; 
    ptrType New = temp.Head; 
    ptrType cur = Head; 
    for(int i = 1; i <= Size; i++) { 
      ptrType org = orig.Head; 
      for(int j = 1; i <= orig.ListLength(); j++) { 
        if(org->exp == cur->exp) { 
          if(firstTime) { 
            New->Next = new PolyTerm; 
            New = New->Next; 
            New->Next = NULL; 
            temp.Size += 1; 
          } 
          New->coef = ((cur->coef) + (org->coef)); 
          New->exp = cur->exp; 
          firstTime = 1; 
          break; 
        } 
        org = org->Next; 

      } 
      cur = cur->Next; 
    } 

    return temp; 
} 

이 미세하고 반환 반환하고 도착,하지만 내 프로그램은 그 후 중단 브레이크 포인트. 나는 잘못한 것을 잘 모르지만 나는 단순하다고 생각한다.

충분한 정보를 제공했으면 좋겠다. 부담없이 물어보십시오

+0

'firstTime'은 처음에는'false'입니다. firsttime에'if()'조건을 입력하지 않을 것입니다. 너가 원하는게 그거야? – noMAD

+0

네, 그게 내가 원하는 걸, 그것은 처음 후에 일어날 것입니다 – Brian

+0

@ BrianMcNamara : 어쩌면 그 "notFirstTime"라고해야합니까? (그리고'false'로 초기화됩니다.) –

답변

0

폴리 유형을 어떻게 정의했는지 모르겠습니다. 이것은 내가 할 수있는 것입니다 : 폴리 타입은 int와 같이 숫자를 저장하는 다항식의 값을 저장합니다. 연산자 ==도 구현할 수 있습니다.

#include <memory> 

template<typename COEF,typename EXP> 
struct PolyList { 
    COEF coef; 
    EXP exp; 
    std::shared_ptr<const PolyList<COEF,EXP>> next; 
    template<typename C,typename E> 
    PolyList(C coef,E exp):coef(coef),exp(exp){} 
}; 

template<typename COEF,typename EXP> 
struct poly : public std::shared_ptr<const PolyList<COEF,EXP>> { 
    template<typename X> 
    poly(X x):std::shared_ptr<const PolyList<COEF,EXP>>(x){} 
    poly(){} 
}; 

template<typename COEF,typename EXP> 
poly<COEF,EXP> operator+(poly<COEF,EXP> a,poly<COEF,EXP> b) 
{ 
    if(!a) return b; 
    if(!b) return a; 
    if(a->exp > b->exp){ 
     PolyList<COEF,EXP> *ret = new PolyList<COEF,EXP>(a->coef,a->exp); 
     ret->next = a->next + b; 
     return ret; 
    } 
    if(a->exp < b->exp) return b+a; 
    // a->exp == b->exp 
    COEF c = a->coef + b->coef; 
    if(!c) return a->next + b->next; 
    // c!=0 
    PolyList<COEF,EXP> *ret = new PolyList<COEF,EXP>(c,a->exp); 
    ret->next = a->next + b->next; 
    return ret; 
}