多项式类

这是我写的一个多项式的类,因为是作业,所以只实现了输入、输出和两个多项式相加而已。

polynomial.h:
#ifndef POLYNOMIAL_H
#define  POLYNOMIAL_H

#include 
< iostream >
#include 
< map >
#include 
< functional >

class  Polynomial
{
private :
     
//  using map container to store the terms, all terms are in descending order of exponentials
    typedef std::map < int int , std::greater < int >   >  TermsMap;

    TermsMap terms;

    
bool  printTerm(std::ostream  & out int  coefficient,  int  exponential,  bool  firstTerm)  const ;
public :
    typedef TermsMap::iterator Iterator; 
//  the iterator of terms
    typedef TermsMap::const_iterator ConstIterator;  //  the const iterator of terms

    
//  here are some interfaces, but some of them not be used in this project
    
//  they are only for integrality of the class
    Iterator begin( void ) {  return  terms.begin(); }
    Iterator end(
void ) {  return  terms.end(); }
    ConstIterator begin(
void const  {  return  terms.begin(); }
    ConstIterator end(
void const  {  return  terms.end(); }
    
int  getCoefficient( int  exponential) {  return  terms[exponential]; }
    
void  clear( void ) { terms.clear(); }

    Polynomial 
& operator += ( const  Polynomial  & p);

    friend std::istream 
& operator >> (std::istream  & in , Polynomial  & p);
    friend std::ostream 
& operator << (std::ostream  & out const  Polynomial  & p);
};

Polynomial 
operator + ( const  Polynomial  & leftPolynomial,  const  Polynomial  & rightPolynomial);

std::istream 
& operator >> (std::istream  & in , Polynomial  & p);
std::ostream 
& operator << (std::ostream  & out const  Polynomial  & p);

#endif

 polynomial.cpp:

#include  < iostream >
#include 
" polynomial.h "
using   namespace  std;

//  print a term in the correct form, return printed or not
bool  Polynomial::printTerm(ostream  & out int  coefficient,  int  exponential,  bool  firstTerm)  const
{
    
if  (coefficient  ==   0 //  do not print terms with coefficient zero
         return   false ;

    
//  print the coefficient
     if  (coefficient  >   0   &&   ! firstTerm)
        
out   <<   " + " ;
    
else   if  (coefficient  <   0 )
        
out   <<   " - " ;
    coefficient 
=  abs(coefficient);
    
if  (coefficient  !=   1   ||  exponential  ==   0 )
        
out   <<  coefficient;

    
if  (exponential  >   0 )
        
out   <<   ' X ' ;
    
if  (exponential  >   1 )
        
out   <<   ' ^ '   <<  exponential;

    
return   true ;
}

//  the '+='operation of polynomial, more operations can be added
Polynomial  & Polynomial:: operator += ( const  Polynomial  & p)
{
    
for  (ConstIterator it  =  p.begin(); it  !=  p.end();  ++ it)
        terms[it
-> first]  +=  it -> second;

    
return   * this ;
}

//  the plus operation of polynomial, more operations can be added
Polynomial  operator + ( const  Polynomial  & leftPolynomial,  const  Polynomial  & rightPolynomial)
{
    Polynomial result(leftPolynomial);
    result 
+=  rightPolynomial;
    
return  result;
}

//  input a polynomial
istream  & operator >> (istream  & in , Polynomial  & p)
{
    
char  separator;

    p.clear();
    
do  {
        
int  coefficient, exponential;
        
in   >>  coefficient  >>  exponential;
        p.terms[exponential] 
=  coefficient;
        
in   >>  noskipws  >>  separator  >>  skipws;  //  not skip white spaces, in order to read a space or a newline
    }  while  (separator  !=   ' ' );  //  input until a newline

    
return   in ;
}

//  print a polynomial
ostream  & operator << (ostream  & out const  Polynomial  & p)
{
    
bool  printed  =   false //  whether there is a term printed or not
     for  (Polynomial::ConstIterator it  =  p.begin(); it  !=  p.end();  ++ it)
        printed 
=  p.printTerm( out , it -> second, it -> first,  ! printed)  ||  printed;
    
if  ( ! printed)  //  no terms printed, print a zero to mark an empty polynomial
         out   <<   " 0 " ;
    
out   <<  endl;

    
return   out ;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值