Calculation of polynomial (operation of the polynomial) C + +

Source: Internet
Author: User
Tags mul

This paper implements a polynomial class in which the addition, subtraction, multiplication of the polynomial is overloaded, and the >> and << are overloaded.

where the polynomial input (output) format is: K N1 aN1 N2 aN2 ... NK ANK

where k denotes the coefficients of the polynomial, NK represents the coefficients of the K term ank the corresponding coefficients, and the coefficients of the polynomial decrement the sequence.

1 //polynomial.h2#include <iostream>3#include <vector>4#include <iomanip>5 6 classpolynomial7 {8 Private:9std::vector<Double>coefficient;Tenstd::vector<int>Radix; One  Public: A polynomial () {} -Polynomial (ConstPolynomial &poly) {* This=Poly;} -~polynomial () {} theFriend polynomialoperator+(ConstPolynomial &poly1,ConstPolynomial &poly2); -Friend polynomialoperator-(ConstPolynomial &poly1,ConstPolynomial &poly2); -polynomial&operator+=(ConstPolynomial &poly) {* This= * This+ Poly;return* This; } -polynomial&operator-=(ConstPolynomial &poly) {* This= * This-Poly;return* This; } +Friend polynomialoperator*(ConstPolynomial &poly1,ConstPolynomial &poly2); -Friend std::istream&operator>> (Std::istream & is, Polynomial &Poly); +Friend std::ostream&operator<< (Std::ostream &os,ConstPolynomial &Poly); A }; at  -Polynomialoperator+(ConstPolynomial &poly1,ConstPolynomial &poly2) - { - polynomial polysum; -  -     inti =0, j =0; in      while(I<poly1.radix.size () | | j<poly2.radix.size ()) -     { to         //Only poly1 +         if(I<poly1.radix.size () && J = =poly2.radix.size ()) -         { the Polysum.radix.push_back (Poly1.radix[i]); * Polysum.coefficient.push_back (Poly1.coefficient[i]); $i++;Panax Notoginseng         } -         //Only poly2 the         Else if(J<poly2.radix.size () && i = =poly1.radix.size ()) +         { A Polysum.radix.push_back (Poly2.radix[j]); the Polysum.coefficient.push_back (Poly2.coefficient[j]); +J + +; -         } $         //The radix of poly1 greater than poly2 $         Else if(Poly1.radix[i] >Poly2.radix[j]) -         { - Polysum.radix.push_back (Poly1.radix[i]); the Polysum.coefficient.push_back (Poly1.coefficient[i]); -i++;Wuyi         } the         //The radix of poly1 smaller than poly2 -         Else if(Poly1.radix[i] <Poly2.radix[j]) Wu         { - Polysum.radix.push_back (Poly2.radix[j]); About Polysum.coefficient.push_back (Poly2.coefficient[j]); $J + +; -         } -         //The radix of poly1 equal to Poly2 -         Else A         { +             if(Poly1.coefficient[i] + poly2.coefficient[j]! =0) the             { - Polysum.radix.push_back (Poly1.radix[i]); $Polysum.coefficient.push_back (Poly1.coefficient[i] +poly2.coefficient[j]); the             } thei++; theJ + +; the         } -     } in  the     returnpolysum; the } About  thePolynomialoperator-(ConstPolynomial &poly1,ConstPolynomial &poly2) the { the polynomial negativepoly; +  -Negativepoly =Poly2; the      for(inti =0; I < negativePoly.coefficient.size (); i++)BayiNegativepoly.coefficient[i] =-Negativepoly.coefficient[i]; the  the     returnPoly1 +Negativepoly; - } -  thePolynomialoperator*(ConstPolynomial &poly1,ConstPolynomial &poly2) the { the polynomial mul; the  -     inti =0; the      while(I <poly2.coefficient.size ()) the     { thePolynomial part =poly1;94         Doublecoefficient =Poly2.coefficient[i]; the         intRadix =Poly2.radix[i]; the  the          for(intj =0; J < Part.coefficient.size (); J + +)98         { AboutPART.RADIX[J] + =Radix; -PART.COEFFICIENT[J] *=coefficient;101         }102Mul + =Part ;103i++;104     } the 106     returnMul;107 }108 109std::istream&operator>> (Std::istream & is, Polynomial &Poly) the {111     intK; the      is>>K;113      for(inti =0; i<k; i++) the     { the         DoubleTmpcoe; the         intTmprad;117          is>> Tmprad >>Tmpcoe;118 Poly.radix.push_back (Tmprad);119 Poly.coefficient.push_back (TMPCOE); -     }121 122     return  is;123 }124  thestd::ostream&operator<< (Std::ostream &os,ConstPolynomial &Poly)126 {127OS <<poly.radix.size (); -     if(Poly.radix.size ()! =0)129Std::cout <<" "; the      for(inti =0; I<poly.radix.size (); i++)131     { theOs << Poly.radix[i] <<" "<< std::fixed<< Std::setprecision (1) <<Poly.coefficient[i];133         if(I! = Poly.radix.size ()-1)134OS <<" ";135     }136 137     returnos;138}

Calculation of polynomial (operation of the polynomial) C + +

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.