2

확인.다항식 과부하 연산자

Adittion : 여기에 작업 내가 성공적으로 코드 지금까지 당신의 도움에의 감사를의

polinom operator+(const polinom& P) const 
{ 
    polinom Result; 
    constIter i = poly.begin(), j = P.poly.begin(); 

    while (i != poly.end() && j != P.poly.end()) { //logic while both iterators are valid 
      if (i->pow > j->pow) { //if the current term's degree of the first polynomial is bigger 
       Result.insert(i->coef, i->pow); 
       i++;  
      } 
      else if (j->pow > i->pow) { // if the other polynomial's term degree is bigger 
       Result.insert(j->coef, j->pow); 
       j++; 
      } 

      else { // if both are equal 
       Result.insert(i->coef + j->coef, i->pow); 
       i++; 
       j++; 
      } 
    } 

//handle the remaining items in each list 
//note: at least one will be equal to end(), but that loop will simply be skipped 

    while (i != poly.end()) { 
     Result.insert(i->coef, i->pow); 
     ++i; 
    } 

    while (j != P.poly.end()) { 
     Result.insert(j->coef, j->pow); 
     ++j; 
    } 
    return Result; 
} 

뺄셈 :

polinom operator-(const polinom& P) const //fixed prototype re. const-correctness 
{ 
    polinom Result; 
    constIter i = poly.begin(), j = P.poly.begin(); 

    while (i != poly.end() && j != P.poly.end()) { //logic while both iterators are valid 
      if (i->pow > j->pow) { //if the current term's degree of the first polynomial is bigger 
       Result.insert(-(i->coef), i->pow); 
       i++;  
      } 

      else if (j->pow > i->pow) { // if the other polynomial's term degree is bigger 
       Result.insert(-(j->coef), j->pow); 
       j++; 
      } 

      else { // if both are equal 
       Result.insert(i->coef - j->coef, i->pow); 
       i++; 
       j++; 
      } 
    } 

//handle the remaining items in each list 
//note: at least one will be equal to end(), but that loop will simply be skipped 

    while (i != poly.end()) { 
     Result.insert(i->coef, i->pow); 
     ++i; 
    } 

    while (j != P.poly.end()) { 
     Result.insert(j->coef, j->pow); 
     ++j; 
    } 
    return Result; 
} 

곱셈 :

polinom operator*(const polinom& P) const 
{ 
    polinom Result; 
    constIter i, j, lastItem = Result.poly.end(); 
    Iter it1, it2, first, last; 
    int nr_matches; 

    for (i = poly.begin() ; i != poly.end(); i++) { 
     for (j = P.poly.begin(); j != P.poly.end(); j++) 
       Result.insert(i->coef * j->coef, i->pow + j->pow); 
    } 

    Result.poly.sort(SortDescending()); 

    lastItem--; 

    while (true) { 
     nr_matches = 0; 

     for (it1 = Result.poly.begin(); it1 != lastItem; it1++) { 
      first = it1; 
      last = it1; 
      first++; 
      for (it2 = first; it2 != Result.poly.end(); it2++) { 
        if (it2->pow == it1->pow) { 
         it1->coef += it2->coef; 
         nr_matches++; 
        } 
      } 

      nr_matches++; 
      do { 
       last++; 
       nr_matches--; 
      } while (nr_matches != 0); 

      Result.poly.erase(first, last); 
     } 
     if (nr_matches == 0) 
      break; 
    }  

    return Result; 
} 

과 (을 편집) :

polinom operator/(const polinom& P) const 
{ 
    polinom Result, temp2; 
    polinom temp = *this; 
    Iter i = temp.poly.begin(); 
    constIter j = P.poly.begin(); 
    int resultSize = 0; 

    if (temp.poly.size() < 2) { 
     if (i->pow >= j->pow) { 
      Result.insert(i->coef/j->coef, i->pow - j->pow); 
      temp = temp - Result * P; 
     } 
     else { 
      Result.insert(0, 0); 
     } 

    } 

    else { 
     while (true) { 
      if (i->pow >= j->pow) {  
       Result.insert(i->coef/j->coef, i->pow - j->pow); 
       if (Result.poly.size() < 2) 
        temp2 = Result; 
       else { 
        temp2 = Result; 
        resultSize = Result.poly.size(); 
        for (int k = 1 ; k != resultSize; k++) 
         temp2.poly.pop_front(); 
       } 
       temp = temp - temp2 * P;    
      } 
      else 
       break; 
     } 
    } 

    return Result; 
} 

}};

처음 세 개는 올바르게 작동하지만 프로그램이 무한 루프에있는 것처럼 보이지 않습니다.

최종 업데이트 데이브을 듣고 후, 나는 마침내 모두를 오버로드하여 제작 /과 &는 몫과 나머지를 반환 덕분에 이렇게 도움과 좋은 아이디어에 특히 데이브에 대한 많은 모두!

P. 만약 누군가 내가이 2 개의 과부하 연산자를 게시하기를 원한다면 내 게시물에 코멘트를 달아달라고 부탁하십시오 (그리고 관련된 모든 사람들에게 투표권을 부여하십시오).

+3

수학 메모 : 2 다항식을 나눗셈하면 다항식이 대부분 발생하지 않습니다. 결과를 나타 내기 위해서는 합리적인 함수 (http://en.wikipedia.org/wiki/Rational_function)가 필요합니다. (실제로'/'가 몫이 아닌 한) – kennytm

+0

가능하면 답을 찾은 해결책을 게시해야합니다. – murgatroid99

답변

2

할당이 poly으로 변경 될 수 있다면 i*this으로 처음 할당 된 후에 유효하지 않습니다. 논란의 여지는 있지만 데이터 손상 대신 무한 루프로 벗어나는 것이 운이 좋은 것입니다.

알고리즘 작동 방식을 따르지 않습니다.

operator /()의 수정 동작은 *this이 아닙니다. 응답을 리턴하고 인수를 수정하지 않아야합니다.

+0

나는 * (초기 배당금)에 나머지를 저장하고 싶었다. 그게 뭐가 잘못 되었 니? – Vlad

+0

더 좋은 방법을 알고 있습니까? 너라면 우리에게 보여줘. ;) – Vlad

+1

'a = b/c;를 고려해보십시오.이 변수가 내장 된 유형이면'b'와'c'는 변경되지 않지만'polynom' 유형 인 경우'b'가 변경됩니다 , 이것은 그것을 사용하는 모든 프로그래머의 기대에 위배됩니다. 함수에서 몫과 나머지를 반환하려면 출력 참조 매개 변수를 사용하는 것이 가장 좋습니다.상수 만 반환하려면'operator /()'를, 나머지 만 반환하려면'operator %()'를 사용하십시오. – dave4420

4

나누는 동안 i 또는 j를 절대로 변경하지 마십시오. while 루프는 중단되지 않습니다.

3

어디에서 이터레이터를 늘리십니까? i와 j가 변경되지 않으면 "while (i-> pow> = j-> pow)"가 매번 동일한 값을 반환하므로 무한 루프가 발생합니다.

+0

@RickNotFred 왜냐하면 제수가 작을 때까지 매번 작아지기 때문에 배당금보다 더 큰 학위가 있기 때문입니다. 어떻게해야합니까? – Vlad

+0

변경 사항이 i와 j를 변경하는 이유는 무엇입니까? – Turtle