多项式加法-C++实现

数据结构的选择:多项式的加法和乘法运算中大量涉及到数据的插入和删除操作,所以选用链表作为存储结构。链表的每个项节点有三个域,分别是系数c、指数e和指向下一结点的指针域next。

首先定义项节点Term:

class Term{
    friend ostream& operator<<(ostream &,const Term &);//重载运算符用于输出多项式的项
    friend class Polynominal;
public:
    Term(int c,int e);
    Term(int c,int e,Term* next);
    Term* InsertAfter(int c,int e);//在this指针指向的结点后面插入新结点
private:
    int c;
    int e;
    Term* next;
};

Term::Term(int c,int e){
    this->c = c;
    this->e = e;
    next = 0;
}

Term::Term(int c,int e,Term* next){ 
    this->c = c;
    this->e = e;
    this->next = next;
}

Term* Term::InsertAfter(int c,int e){
    next = new Term(c,e,next);
    return next;
}

ostream& operator <<(ostream& out,const Term &val){
    if(val.c!=0){
        out<<val.c;
        switch(val.e){
            case 
  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值