Advertisement
Guest User

Untitled

a guest
Apr 21st, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.25 KB | None | 0 0
  1. void Polynomial::insertTerm(double coeff, int exp) {
  2.     if (coeff != 0) {
  3.         if (head == NULL || exp > head -> exp){
  4.             insertNode(NULL, coeff, exp);
  5.             return;
  6.         }
  7.         Node *temp = head;
  8.         while (temp -> link != NULL && temp -> link -> exp > exp)
  9.             temp = temp -> link;
  10.         if (temp -> link == NULL){
  11.             if (temp -> exp == exp){
  12.                 temp -> coeff += coeff;
  13.                 if (temp -> coeff == 0)
  14.                     deleteNode(NULL, temp);
  15.             }
  16.             else
  17.                 insertNode(temp, coeff, exp);
  18.         }
  19.         else {
  20.             if (exp == temp -> link -> exp){
  21.                 temp -> link -> coeff += coeff;
  22.                 if (temp -> link -> coeff == 0)
  23.                     deleteNode(temp, temp -> link);
  24.             }
  25.             else
  26.                 insertNode(temp, coeff, exp);
  27.         }
  28.     }
  29.     return;
  30. }
  31. void Polynomial::print() {
  32.     if (head == NULL)
  33.         return;
  34.     Node *temp = head;
  35.     while (temp != NULL){
  36.         double posCoeff = temp -> coeff;
  37.         if (temp -> coeff < 0 && temp != head)
  38.             posCoeff = - temp -> coeff;
  39.         if (temp -> exp > 1)
  40.             cout << posCoeff <<"x" <<"^" <<temp -> exp ;
  41.         if (temp -> exp == 1)
  42.             cout << posCoeff <<"x" ;
  43.         if (temp -> exp == 0)
  44.             cout << posCoeff;
  45.         if (temp -> link != NULL){
  46.             if (temp -> link -> coeff > 0)
  47.                 cout <<" + ";
  48.             else
  49.                 cout <<" - ";
  50.         }
  51.         temp = temp -> link;
  52.     }
  53.     cout << endl;
  54. }
  55.  
  56. Polynomial Polynomial::operator+(const Polynomial& b) {
  57.     Polynomial result = b;
  58.     Node *temp = head;
  59.     while (temp != NULL){
  60.         result.insertTerm(temp -> coeff, temp -> exp);
  61.         temp = temp -> link;
  62.     }
  63.     return result;
  64. }
  65.  
  66. Polynomial Polynomial::operator*(const Polynomial& b) {
  67.     Polynomial result;
  68.     Node *tempA = head;
  69.     while (tempA != NULL) {
  70.         Node *tempB = b.head;
  71.         while (tempB != NULL) {
  72.             result.insertTerm(tempA -> coeff * tempB -> coeff, tempA -> exp + tempB -> exp);
  73.             tempB = tempB -> link;
  74.         }
  75.         tempA = tempA -> link;
  76.     }
  77.     return result;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement