hentaiw

Polynomial Code

Nov 27th, 2011
342
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.65 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <iomanip>
  4. #include <cstddef>
  5.  
  6. using std::cin;
  7. using std::cout;
  8. using std::endl;
  9. using std::ostream;
  10. using std::istream;
  11. using std::exit;
  12. using std::pow;
  13.  
  14. //ostream& operator << (ostream& out,const Term& object);
  15.  
  16. class Term
  17.     {
  18.     public:
  19.         //CONSTRUCTORS
  20.         Term(int theA,int theN,Term* theLink) : a(theA),n(theN),link(theLink){};
  21.         Term(int theA,int theN) : a(theA),n(theN),link(NULL){};
  22.         //ACCESSORS
  23.         int getA() const {return a;}
  24.         int getN() const {return n;}
  25.         Term* getLink() const {return link;}
  26.         //MUTATORS
  27.         void setA(int theA){a = theA;}
  28.         void setN(int theN){n = theN;}
  29.         void setPol(int theA,int theN){a = theA;n = theN;}
  30.         void setLink(Term* theLink){link = theLink;}
  31.         //EVALUATION
  32.         double evaluate(double theX);
  33.         //OVERLOADED OPERATORS
  34.         Term operator + (const Term& object);
  35.         Term operator - (const Term& object);
  36.         Term operator * (const Term& object);
  37.         friend ostream& operator << (ostream& out,const Term& object);
  38.         friend istream& operator >> (istream& in,Term& object);
  39.         //DESTRUCTORS
  40.         //virtual ~Term();
  41.     private: //a*x^n
  42.         int a;
  43.         int n;
  44.         Term* link;
  45.     };
  46.  
  47. class Polynomial
  48.     {
  49.     public:
  50.         Polynomial() : top(NULL){};
  51.         Polynomial(int theA) {addNew(theA,0);};
  52.         Polynomial(int theA,int theN) {addNew(theA,theN);}
  53.         Polynomial(const Polynomial& object);
  54.         void addNew(int theA,int theN);
  55.         bool isEmpty() const;
  56.         void simplify();
  57.         void remove(Term* afterMe);
  58.         double evaluate(double theX);
  59.         //OVERLOADED OPERATORS
  60.         Polynomial operator + (const Polynomial& object);
  61.         Polynomial operator - (const Polynomial& object);
  62.         Polynomial operator * (const Polynomial& object);
  63.         friend ostream& operator << (ostream& out,const Polynomial& object);
  64.         friend istream& operator >> (istream& in,Polynomial& object);
  65.     private:
  66.         Term* top; 
  67.     };
  68.  
  69. int main()
  70. {
  71.     cout << "PolynomialFULL - By Kudy" << endl;
  72.     cout << "This is a full version and included all necessary tools for polynomials." << endl;
  73.    
  74.     //TEST 1 : TERM::+ ERROR
  75.     Term left1(2,3,NULL),right1(5,6,NULL);
  76.     cout << "left1 = " << left1 << endl;
  77.     cout << "right1 = " << right1 << endl;
  78.     Term sum1 = left1 + right1;
  79.     cout << "sum1 = " << sum1 << endl;
  80.     cout << "----------\n";
  81.     //TEST 2 : TERM::+ SUCCESS
  82.     Term left2(-2,3,NULL),right2(5,3,NULL);
  83.     cout << "left2 = " << left2 << endl;
  84.     cout << "right2 = " << right2 << endl;
  85.     Term sum2 = left2 + right2;
  86.     cout << "sum2 = " << sum2 << endl;
  87.     cout << "----------\n";
  88.     //TEST3 : TERM::- SUCCESS
  89.     Term left3(-8,3,NULL),right3(-15,3,NULL);
  90.     cout << "left3 = " << left3 << endl;
  91.     cout << "right3 = " << right3 << endl;
  92.     Term dif3 = left3 - right3;
  93.     cout << "dif3 = " << dif3 << endl;
  94.     cout << "----------\n";
  95.     //TEST4 : TERM::* SUCCESS
  96.     Term left4(-8,3,NULL),right4(-15,3,NULL);
  97.     cout << "left4 = " << left4 << endl;
  98.     cout << "right4 = " << right4 << endl;
  99.     Term pro4 = left4 * right4;
  100.     cout << "pro4 = " << pro4 << endl;
  101.     cout << "----------\n";
  102.     //TEST5 : POLY AUTO
  103.     Polynomial poly;
  104.     poly.addNew(2,3);
  105.     poly.addNew(-4,2);
  106.     poly.addNew(8,1);
  107.     poly.addNew(-6,0);
  108.     cout << "poly = " << poly << endl;
  109.     cout << "----------\n";
  110.     //TEST6 : POLY MANU
  111.     Polynomial poly2;
  112.     cin >> poly2;
  113.     cout << "----------\n";
  114.     cout << "poly2 = " << poly2 << endl;
  115.     //TEST7 : SIMPLIFY MANU
  116.     poly2.simplify();
  117.     cout << "poly2 (simplified) = " << poly2 << endl;
  118.     cout << "----------\n";
  119.     //TEST8 : COPY CONSTRUSTOR
  120.     cout << "Copy Poly2 :\n";
  121.     Polynomial copy(poly2);
  122.     cout << copy << endl;
  123.     cout << "----------\n";
  124.     //TEST9 : ADD POLY
  125.     cout << "Addition :\n";
  126.     Polynomial poly3;
  127.     cin >> poly3;
  128.     poly3.simplify();
  129.     cout << "\n----------\n";
  130.     Polynomial sum = poly2 + poly3;
  131.     cout << "Factor 1 : " << poly2 << endl;
  132.     cout << "Factor 2 : " << poly3 << endl;
  133.     cout << "Result : \n" << sum;
  134.     cout << "\n----------\n";
  135.     //TEST10 : SUBTRACT POLY
  136.     Polynomial dif = poly2 - poly3;
  137.     cout << "Factor 1 : " << poly2 << endl;
  138.     cout << "Factor 2 : " << poly3 << endl;
  139.     cout << "Result : \n" << dif;
  140.     cout << "\n----------\n";
  141.     //TEST11 : EVALUATE POLY
  142.     cout << "poly = " << poly << endl;
  143.     cout << "x = 3" << endl;
  144.     cout << "Result : " << poly.evaluate(3);
  145.     cout << "\n----------\n";
  146.     //TEST12 : MULTIPLICATION POLY
  147.     Polynomial pro = poly2 * poly3;
  148.     cout << "Factor 1 : " << poly2 << endl;
  149.     cout << "Factor 2 : " << poly3 << endl;
  150.     cout << "Result : \n" << pro;
  151.     cout << "\n----------\n";
  152.     return 0;
  153. }
  154.  
  155. Polynomial Polynomial::operator* (const Polynomial& object)
  156. {
  157.     Term* factor1 = this->top;
  158.     Term* factor2 = object.top;
  159.     Polynomial result;
  160.     while(factor1 != NULL)
  161.     {
  162.         while(factor2 != NULL)
  163.         {
  164.             result.addNew(factor1->getA() * factor2->getA(),factor1->getN()+factor2->getN());
  165.             factor2 = factor2->getLink();
  166.         }
  167.         factor2 = object.top;
  168.         factor1 = factor1->getLink();
  169.     }
  170.     result.simplify();
  171.     return result;
  172. }
  173.  
  174. double Term::evaluate(double theX)
  175. {
  176.     return (a * pow(theX,static_cast<double>(n)));
  177. }
  178.  
  179. double Polynomial::evaluate(double theX)
  180. {
  181.     double result = 0;
  182.     Term *temp = top;
  183.     while(temp != NULL)
  184.     {
  185.         result = result + temp->evaluate(theX);
  186.         temp = temp->getLink();
  187.     }
  188.     return result;
  189. }
  190.  
  191. Polynomial::Polynomial(const Polynomial& object)
  192. {
  193.     Term *temp = object.top;
  194.     top = NULL;
  195.     if(object.isEmpty())
  196.     {
  197.         top = NULL;
  198.     }
  199.     while(temp != NULL)
  200.     {
  201.         addNew(temp->getA(),temp->getN());
  202.         temp = temp->getLink();
  203.     }
  204. }
  205.  
  206. void Polynomial::addNew(int theA,int theN)
  207. {
  208.     if(isEmpty())
  209.     {
  210.         top = new Term(theA,theN,NULL);
  211.     }
  212.     else
  213.     {
  214.         Term *temp = top;
  215.         while(temp->getLink() != NULL)
  216.             temp = temp->getLink();
  217.         //When reach end !
  218.         temp->setLink(new Term(theA,theN,NULL));
  219.     }
  220. }
  221.  
  222. Polynomial Polynomial::operator+ (const Polynomial& object)
  223. {
  224.     Term *factor1 = this->top;
  225.     Term *factor2 = object.top;
  226.     Polynomial result;
  227.     bool valAdded = false;
  228.  
  229.     if(factor2->getN() > factor1->getN())
  230.     {
  231.         Term *temp = factor1;
  232.         factor1 = factor2;
  233.         factor2 = temp;
  234.     }
  235.  
  236.     Term *lowEx = factor2;
  237.  
  238.     //This will add all elements from factor1 & factor2 to result,then simplify.
  239.  
  240.     while(factor1 != NULL)
  241.     {
  242.         result.addNew(factor1->getA(),factor1->getN());
  243.         factor1 = factor1->getLink();
  244.     }//End 1
  245.  
  246.     while(factor2 != NULL)
  247.     {
  248.         result.addNew(factor2->getA(),factor2->getN());
  249.         factor2 = factor2->getLink();
  250.     }//End 2
  251.  
  252.     result.simplify();
  253.    
  254.     return result;
  255. }
  256.  
  257. Polynomial Polynomial::operator- (const Polynomial& object)
  258. {
  259.     Term *factor1 = this->top;
  260.     Term *factor2 = object.top;
  261.     Polynomial result;
  262.     bool valAdded = false;
  263.  
  264.     //This will add all elements from factor1 & factor2(negatively) to result,then simplify.
  265.  
  266.     while(factor1 != NULL)
  267.     {
  268.         result.addNew(factor1->getA(),factor1->getN());
  269.         factor1 = factor1->getLink();
  270.     }//End 1
  271.  
  272.     while(factor2 != NULL)
  273.     {
  274.         result.addNew(-factor2->getA(),factor2->getN());
  275.         factor2 = factor2->getLink();
  276.     }//End 2
  277.  
  278.     result.simplify();
  279.    
  280.     return result;
  281. }
  282.  
  283. ostream& operator << (ostream& out,const Polynomial& object)
  284. {
  285.     Term *temp = object.top;
  286.     if(temp == NULL)
  287.         out << "0";
  288.     else
  289.     {
  290.         while(temp != NULL)
  291.         {
  292.             out << *temp;
  293.             temp = temp->getLink();
  294.         }
  295.     }
  296.     return out;
  297. }
  298.  
  299. void Polynomial::simplify()
  300. {
  301.     Term *temp = top;
  302.     int merge = 0;
  303.     while(temp != NULL)
  304.     {
  305.         int expo = temp->getN();
  306.         merge = temp->getA();
  307.         Term * temp2 = temp;
  308.         temp2 = temp2->getLink();
  309.         while(temp2 != NULL)
  310.         {
  311.             if(temp2->getN() == temp->getN())
  312.             {  
  313.                 merge = merge + temp2->getA();
  314.                 temp2->setA(0);
  315.                 temp2->setN(1);
  316.             }
  317.             temp2 = temp2->getLink();
  318.         }
  319.         temp->setA(merge);
  320.         temp = temp->getLink();
  321.         merge = 0;
  322.     }
  323. }
  324.  
  325. void Polynomial::remove(Term* afterMe)
  326. {
  327.     Term* discard = afterMe->getLink();
  328.     afterMe->setLink(discard->getLink());
  329.     delete discard;
  330. }
  331.  
  332. istream& operator >> (istream& in,Polynomial& object)
  333. {
  334.     cout << "Enter A Polynomial : " << endl;
  335.     char next;
  336.     int sign = 0;
  337.     int theA,theN;
  338.     cin.get(next);
  339.     bool savedSign = false;
  340.  
  341.     while(next != '\n')
  342.     {
  343.         if(next != ' ')
  344.         {
  345.             if(next == '-')
  346.             {
  347.                 if(sign == 0)
  348.                     sign = -1;
  349.                 else
  350.                     sign = -sign;
  351.                 cin.get(next);
  352.                 if(next != ' ')
  353.                 {
  354.                     cin.putback(next);
  355.                     cin.putback('-');
  356.                     savedSign = false;
  357.                 }
  358.                 else
  359.                 {
  360.                     cin.get(next);
  361.                     if(next != '+' && next != '-')
  362.                         cin.putback(next);
  363.                     else if(next == '-')
  364.                     {
  365.                         sign = -sign;
  366.                         savedSign = true;
  367.                         cin.putback(next);
  368.                     }
  369.                     else
  370.                         cin.putback(next);
  371.                 }
  372.                 cin.get(next);
  373.             }
  374.             else if(next == '+')
  375.             {
  376.                 if(sign == 0)
  377.                     sign = 1;
  378.                 cin.get(next);
  379.                 if(next != ' ')
  380.                 {
  381.                     cin.putback(next);
  382.                     cin.putback('+');
  383.                 }
  384.                 cin.get(next);
  385.             }
  386.             if((next >= '0') && (next <= '9'))
  387.             {
  388.                
  389.                 cin.putback(next);
  390.                 cin >> theA;
  391.                 cin.get(next);//Take the word x
  392.                 if(next != 'x' && (next == ' ' || next == '\n'))
  393.                 {
  394.                     theN = 0;
  395.                     cin.putback(next);
  396.                 }
  397.                 else if(next == 'x')
  398.                 {
  399.                     cin.get(next);//Take '^' or ' '
  400.                     if(next == ' ' || next == '\n')
  401.                     {
  402.                         theN = 1;
  403.                         cin.putback(next);
  404.                     }
  405.                     else if(next = '^')
  406.                     {
  407.                         cin >> theN;
  408.                     }
  409.                 }
  410.                 if(sign >= 0)
  411.                     object.addNew(theA,theN);
  412.                 if(sign < 0)
  413.                     object.addNew(-theA,theN);
  414.                 savedSign = false;
  415.                 if(!savedSign)
  416.                     sign = 0;
  417.             }
  418.         }
  419.         cin.get(next);
  420.     }
  421.  
  422.     //CAUTION : THIS FUCTION HAS A BUG IF YOU ADD A MINUS LIKE A + (-B)
  423.     //IT WORKS WELL FOR SUBTRACT A MINUS AND A PLUS.
  424.     return in;
  425. }
  426.  
  427. ostream& operator << (ostream& out,const Term& object)
  428. {
  429.     if(object.getA() != 0)
  430.     {
  431.         if(object.getA() < 0)
  432.             out << "- " << -object.getA() << "x^" << object.getN() << " ";
  433.         else
  434.             out << "+ " << object.getA() << "x^" << object.getN() << " ";
  435.     }
  436.     return out;
  437. }
  438.  
  439. istream& operator >> (istream& in,Term& object)
  440. {
  441.     int theA,theN;
  442.     char theX;
  443.     cin >> theA >> theX >> theN;
  444.     if(theX != 'x' && theX != 'X')
  445.     {
  446.         cout << "Attempt to input wrong ! Exitting..." << endl;
  447.         exit(1);
  448.     }
  449.     return in;
  450. }
  451.  
  452. bool Polynomial::isEmpty() const
  453. {
  454.     return(top == NULL);
  455. }
  456.  
  457. Term Term::operator + (const Term& object)
  458. {
  459.     cout << "Term::operator + : ";
  460.     if(getN() != object.getN())
  461.     {
  462.         cout << "Error ! Adding 2 term with different exponent !" << endl;
  463.         return object;
  464.     }
  465.     else
  466.     {
  467.         int sum = getA() + object.getA();
  468.         return Term(sum,getN());
  469.     }
  470. }
  471.  
  472. Term Term::operator - (const Term& object)
  473. {
  474.     cout << "Term::operator - : ";
  475.     if(getN() != object.getN())
  476.     {
  477.         cout << "Error ! Subtracting 2 term with different exponent !" << endl;
  478.         return object;
  479.     }
  480.     else
  481.     {
  482.         int dif = getA() - object.getA();
  483.         return Term(dif,getN());
  484.     }
  485. }
  486.  
  487. Term Term::operator * (const Term& object)
  488. {
  489.     cout << "Term::operator * : ";
  490.     return Term(getA() * object.getA(),getN() + object.getN());
  491. }
Add Comment
Please, Sign In to add comment