多项式类

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

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 ;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用 C 语言实现一个多项式,以下是一个简单的示例: ```c #include <stdio.h> #include <stdlib.h> typedef struct { int degree; int* coefficients; } Polynomial; Polynomial createPolynomial(int degree) { Polynomial p; p.degree = degree; p.coefficients = (int*)malloc((degree + 1) * sizeof(int)); for (int i = 0; i <= degree; i++) { p.coefficients[i] = 0; } return p; } void setCoefficient(Polynomial* p, int degree, int coefficient) { if (degree <= p->degree) { p->coefficients[degree] = coefficient; } else { printf("Degree is out of range.\n"); } } int getCoefficient(Polynomial* p, int degree) { if (degree <= p->degree) { return p->coefficients[degree]; } else { printf("Degree is out of range.\n"); return 0; } } void printPolynomial(Polynomial* p) { for (int i = p->degree; i >= 0; i--) { if (p->coefficients[i] != 0) { printf("%dx^%d ", p->coefficients[i], i); } } printf("\n"); } void destroyPolynomial(Polynomial* p) { free(p->coefficients); } int main() { Polynomial p = createPolynomial(3); setCoefficient(&p, 0, 2); setCoefficient(&p, 1, -3); setCoefficient(&p, 2, 1); setCoefficient(&p, 3, 4); printPolynomial(&p); destroyPolynomial(&p); return 0; } ``` 这个示例代码实现了一个多项式,具有以下功能: - `createPolynomial` 函数用于创建一个指定次数多项式,并初始化系数为 0。 - `setCoefficient` 函数用于设置指定次数的系数。 - `getCoefficient` 函数用于获取指定次数的系数。 - `printPolynomial` 函数用于打印多项式表达式。 - `destroyPolynomial` 函数用于释放多项式的内存。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值