Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.41 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <cstring>
  4. using namespace std;
  5.  
  6. class CPoly;
  7.  
  8. class CTerm // class term
  9. {
  10.   private:
  11.           int m_exp;
  12.           int m_coeff;
  13.           CTerm * m_llink;
  14.           CTerm * m_rlink;
  15.   public:
  16.           CTerm(int, int);
  17.           ~CTerm();
  18.           CTerm operator+(const CTerm &) const;
  19.           CTerm operator-(const CTerm &) const;
  20.           CTerm operator*(const CTerm &) const;
  21.           CTerm & operator=(const CTerm &);
  22.  
  23.           const int getExp() const;
  24.  
  25.  
  26.   /* friend function */
  27.   friend class CPoly;
  28.   friend ostream & operator<<(ostream &ostr, const CTerm & rhs);
  29.   friend ostream & operator<<(ostream &ostr, const CPoly & rhs);
  30.  
  31. };// end of class term
  32.  
  33. class CPoly // class polynomial
  34. {
  35.   private:
  36.           CTerm *m_dummy;
  37.           int m_size;
  38.  
  39.   public:
  40.           CPoly();
  41.           ~CPoly();
  42.           CPoly operator+(const CPoly &) const;
  43.           CPoly operator-(const CPoly &) const;
  44.           CPoly operator*(const CPoly &) const;
  45.           CPoly & operator=(const CPoly &) const;
  46.           void add(const CTerm &);
  47.           CTerm & at(int);
  48.           const int length() const;
  49.           const int degree() const;
  50.          
  51.  
  52.   /* friend function */
  53.  
  54.   friend ostream & operator<<(ostream &ostr, const CPoly & rhs);
  55.  
  56. };// end of class polynomial
  57.  
  58. //-------------------------------------------------------------------------------------------------------------------------
  59.  
  60. CTerm::CTerm(int c = 0, int e = 0) : m_exp(e), m_coeff(c), m_llink(NULL), m_rlink(NULL)
  61. // do nothing
  62. {}
  63.  
  64. CTerm::~CTerm()
  65. // do nothing
  66. {}
  67.  
  68. inline CTerm CTerm::operator+(const CTerm &rhs) const
  69. // this function does not check whether addable or not
  70. // it is programmer's responsibility
  71. {
  72.   if(getExp() != rhs.getExp())
  73.   {
  74.     cerr << "undefined addition...  program abort..." << endl;
  75.     abort();
  76.   }
  77.  
  78.   return  CTerm(m_coeff + rhs.m_coeff, m_exp);
  79. }// end of operator+
  80.  
  81. inline CTerm CTerm::operator-(const CTerm &rhs) const
  82. // this function does not check whether addable or not
  83. // it is programmer's responsibility
  84. {  
  85.   if(getExp() != rhs.getExp())
  86.   {
  87.     cerr << "undefined addition\nprogram abort..." << endl;
  88.     abort();
  89.   }
  90.  
  91.   return  CTerm(m_coeff - rhs.m_coeff, m_exp);
  92. }// end of operator-
  93.  
  94. inline CTerm CTerm::operator*(const CTerm &rhs) const
  95. {
  96.    return  CTerm(m_coeff * rhs.m_coeff, m_exp + rhs.m_exp);
  97. }// end of operator*
  98.  
  99. inline CTerm & CTerm::operator=(const CTerm &rhs)
  100. {
  101.   m_coeff = rhs.m_coeff;
  102.   m_exp = rhs.m_exp;
  103.   return *this;
  104. }// end of operator=
  105.  
  106. inline const int CTerm:: getExp() const
  107. {
  108.   return m_exp;
  109. }
  110.  
  111. ostream & operator<<(ostream &ostr, const CTerm & rhs)
  112. {
  113.   ostr << rhs.m_coeff << 'x' << rhs.m_exp;
  114.   return ostr;
  115. }// end of operator<<
  116.  
  117. //------------------------------------------------------------------------------------------------------------------------------
  118.  
  119. CPoly::CPoly() : m_size(0)
  120. // create dummy node
  121. {
  122.   m_dummy = new CTerm;
  123.   m_dummy->m_llink = m_dummy->m_rlink = m_dummy; // circular list
  124. }
  125.  
  126. CPoly:: ~CPoly()
  127. {
  128.   CTerm *delTerm = m_dummy;
  129.   while(m_size)
  130.   {
  131.     CTerm *tmp = delTerm;
  132.     delTerm = delTerm->m_rlink;
  133.     delete tmp;
  134.     m_size--;
  135.   }
  136.  
  137.   delete delTerm;
  138.  
  139. /*
  140.   if(m_size)
  141.   {
  142.     cerr << "delete failed... aborting..." << endl;
  143.     abort();
  144.   }
  145. */
  146. }
  147.  
  148. CTerm & CPoly:: at(int index)
  149. {
  150.   if(index >= m_size)
  151.   {
  152.     cerr << "array index overflow... aborting...";
  153.     abort();
  154.   }
  155.  
  156.   CTerm *ptr = m_dummy;
  157.  
  158.   if(index < m_size/2)
  159.   {
  160.     int count = index + 1; // dummy
  161.     while(count)
  162.     {
  163.       ptr = ptr->m_rlink;
  164.       count--;
  165.     }
  166.     return *ptr;
  167.   }
  168.   else
  169.   {
  170.     int count = m_size - index ;
  171.     while(count)
  172.     {
  173.       ptr = ptr->m_llink;
  174.       count--;
  175.     }
  176.     return *ptr;
  177.   }
  178. }
  179.  
  180.  
  181. void CPoly:: add(const CTerm &newTerm)
  182. {
  183.   CTerm *curTerm = m_dummy;
  184.  
  185.   while(curTerm->m_exp < newTerm.m_exp)
  186.   {
  187.     curTerm = curTerm->m_rlink;
  188.  
  189.     if(curTerm == m_dummy)
  190.       break;
  191.  
  192.   }
  193.  
  194.   if(curTerm->m_exp == newTerm.m_exp)
  195.     curTerm->m_coeff += newTerm.m_coeff;
  196.  
  197.   else
  198.   {
  199.     CTerm *tmp = new CTerm(newTerm.m_coeff,newTerm.m_exp);
  200.     tmp->m_llink = curTerm;
  201.     tmp->m_rlink = curTerm->m_rlink;
  202.     curTerm->m_rlink->m_llink = tmp;
  203.     curTerm->m_rlink = tmp;
  204.     m_size++;
  205.   }
  206. }
  207.  
  208. /* for degug */
  209.  
  210. void dbp(const CPoly &c)
  211. {
  212.   cout << c << endl;
  213. }
  214.  
  215. //
  216. //
  217.  
  218. ostream & operator<<(ostream &ostr, const CPoly & rhs)
  219. {
  220.   CTerm *ptr = rhs.m_dummy->m_rlink;
  221.   while(ptr != rhs.m_dummy)
  222.   {
  223.     ostr << *ptr << ' ';
  224.     ptr = ptr->m_rlink;
  225.   }
  226.   return ostr;
  227. }
  228.  
  229. inline const int CPoly:: length() const
  230. {
  231.   return m_size;
  232. }
  233.  
  234. inline const int CPoly:: degree() const
  235. {
  236.   return m_dummy->m_rlink->m_exp;
  237. }
  238.  
  239. CPoly CPoly:: operator+(const CPoly &rhs) const
  240. {
  241.   CPoly res, result , aaa, bbb , ccc;
  242. cout << "WOW" << result.length() << endl;
  243.  
  244.   CTerm *lhsPtr = m_dummy->m_rlink,
  245.         *rhsPtr = rhs.m_dummy->m_rlink;
  246.  
  247.   int lLen = this->length(),
  248.       rLen = rhs.length();
  249.  
  250.   while(lLen && rLen) // not empty
  251.   {
  252.     if(lhsPtr->m_exp == rhsPtr->m_exp)
  253.     {
  254.       if(lhsPtr->m_coeff + rhsPtr->m_coeff) // not zero
  255.       {
  256.         result.add(CTerm(lhsPtr->m_coeff + rhsPtr->m_coeff, lhsPtr->m_exp));
  257.         lhsPtr = lhsPtr->m_rlink;
  258.         rhsPtr = rhsPtr->m_rlink;
  259.         rLen--;
  260.         lLen--;
  261.       }
  262.     }
  263.  
  264.     else if(lhsPtr->m_exp < rhsPtr->m_exp)
  265.     {
  266.       result.add(*rhsPtr);
  267.       rhsPtr = rhsPtr->m_rlink;
  268.       rLen--;
  269.     }
  270.     else
  271.     {
  272.       result.add(*lhsPtr);
  273.       lhsPtr = lhsPtr->m_rlink;
  274.       lLen--;
  275.     }
  276.   }
  277.  
  278.   while(lhsPtr != this->m_dummy) // rest of lhs
  279.   {
  280.     result.add(*lhsPtr);
  281.     lhsPtr = lhsPtr->m_rlink;
  282.   }
  283.  
  284.   while(rhsPtr != rhs.m_dummy) // rest of lhs
  285.   {
  286.     result.add(*rhsPtr);
  287.     rhsPtr = rhsPtr->m_rlink;
  288.   }
  289.  
  290.   return result;
  291. }
  292.  
  293.  
  294.  
  295.  
  296. //--------------------------------------------------------------------------------------------------------------------------
  297.  
  298. int main()
  299. {
  300.   CPoly p1, p2;
  301.  
  302.  
  303.   for(int c = 1, e = 1 ; e < 10 ; c++, e++)
  304.   {
  305.     if(e==5)
  306.       p1.add(CTerm(100,8));
  307.    p1.add(CTerm(c,e));
  308.   }
  309.  
  310.   cout << p1 << endl;
  311. /*
  312.   for(int i = 0 ; i < 11 ; i++)
  313.   {
  314.     cout << p1.at(i) << ' ';
  315.   }
  316. */
  317.   p2.add(CTerm(1,1));
  318.   p2.add(CTerm(2,4));
  319.  
  320.   cout << p2 << endl;
  321.  
  322.   cout << p1 + p2 << "degree: " << (p1 + p2).degree() << endl;
  323.  
  324.   cout << endl;
  325.  
  326.   return 0;
  327. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement