稀疏多项式加法

/**
Polynominal& Polynominal::operator +(Polynominal& poly)
    iter1=this.pList.begin
    iter2=poly.pList.begin

    while(iter1!=pList.end && iter2!=poly.pList.end)//比较,选择小的挂上po.pList
        if(iter1->exp < iter2->exp)    
            po->pList.push(*iter1++)
        if(iter1->exp > iter2->exp)
            po->pList.push(*iter2++)
        if(iter1->exp == iter2->exp)
            po->pList.push(*iter1+*iter2)

    while(iter1!=pList.end())//如果有剩余,则挂上po.pList
        po->pList.push_back(*iter1++);
    
    while(iter2!=poly.pList.end())//如果有剩余,则挂上po.pList
        po->pList.push_back(*iter2++);
    
    return *po;            
*/
Polynominal
& Polynominal::operator +(Polynominal& poly){
    Polynominal
* po=new Polynominal();
    list
<Term>::iterator iter1=pList.begin();
    list
<Term>::iterator iter2=poly.pList.begin();
    
while(iter1!=pList.end() && iter2!=poly.pList.end()){
        
if(iter1->exp < iter2->exp){
            po
->pList.push_back(*iter1++);
        }
        
else if(iter1->exp > iter2->exp){
            po
->pList.push_back(*iter2++);
        }
        
else{
            Term
* t=new Term(*iter1++ +*iter2++);//拷贝函数,且Term的相加已经重载过
            po->pList.push_back(*t);
        }
    }
    
while(iter1!=pList.end()){
        po
->pList.push_back(*iter1++);
    }
    
while(iter2!=poly.pList.end()){
        po
->pList.push_back(*iter2++);
    }
    
return *po;
}

posted on 2008-10-23 21:25 deep2 阅读(323) 评论(0)  编辑 收藏 引用 所属分类: 链表


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   博问   Chat2DB   管理


<2008年10月>
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

导航

统计

常用链接

留言簿(1)

随笔分类

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜