读书人

多项式加法演算

发布时间: 2012-11-11 10:07:57 作者: rapoo

多项式加法运算
这个程序调试了好久了,没有找出错误 ,大家帮帮忙,为什么没有做出加法运算。。。。。。谢谢了




#include <iostream>

using namespace std;

typedef struct LNode {
float coef;
int expn;
struct LNode *next;
}LNode ,*LinkList;

void CreatList_L (LinkList &L, int n){
//输入n个元素的值,建立带头节点的链表
LinkList p;
L = (LinkList) malloc(sizeof(LNode));
L->next = NULL; //建立一个带头结点的链表

for(int i = n; i>=1; i--){
p = (LinkList) malloc(sizeof(LNode)); //生成新节点
cin>>p->coef>>p->expn;
p->next = L->next;
L->next = p;
cout<<p->coef<<" "<<p->expn<<endl;
}
}

int getCurElem(LinkList &p){
return p->expn;
}

void setCurElem(LinkList p, float sum){
p->coef = sum;
}

void InsFirst(LinkList p,LinkList q){
p->next = q;
p = q;
q = q->next;
}

int compare(int i, int j){
if(i<j){
return -1;
}else {
if(i == j){
return 0;
}else{
return 1;
}
}
}

void FreeNode(LinkList &p){
delete(p);
}

void DelFirst(LinkList q,LinkList p){

}
bool ListEmpty(LinkList p){
if(p)
return false;
else
return true;
}
void Addpolyn (LinkList &pa , LinkList &pb){
//多项式加法 :pa = pa + pb
int a,b;
float sum;
LinkList ha = pa;
LinkList hb = pb;

LinkList qa,qb,s;

qa = pa->next;
qb = pb->next;

while (qa&&qb){//qa和qb均非空
//a和b为两表中当前比较元素
a =getCurElem(qa);
b =getCurElem(qb);

switch (compare(a,b)){
case -1: //多项式pa当前节点中指数值小
ha = qa;
qa = qa->next;
break;

case 0:
//两数相等
sum = qa->coef+qb->coef;
if(sum !=0.0){
//修改多项式pa当前节点的系数值
setCurElem(qa,sum);
}else {
s = qa;
qa = qa->next;
ha->next=qa;
FreeNode(s);
}
s = qb;
qb=qb->next;
hb=qb;
FreeNode(s);
break;

case 1:

ha->next = qb;
ha = qb;
qb = qb->next;
ha->next = qa;

}
}

if(!ListEmpty(pb)){
ha->next=qb;
hb->next=NULL;
delete(pb);
}else{
delete(pb);
}
}


void print(LinkList p){
while(p){
cout<<p->coef<<" "<<p->expn<<endl;
p = p->next;
}
}

int main(){
LinkList link1,link2;

CreatList_L (link1, 3);


CreatList_L (link2, 3);


Addpolyn(link1 , link2);
print(link1);

return 0;
}

[解决办法]
你的代码有点问题,假如输入的指数不是递增或者递减的呢?
[解决办法]
参考下面的一个程序,希望对你有所帮助。

C/C++ code
链表实现多项式的算术运算:(降幂排列)#include<iostream>using namespace std;class Term{    int coef;    int exp;    Term *link;    friend ostream& operator<<(ostream&,const Term&);    friend class Polynominal;public:    Term(int c,int e):coef(c),exp(e)    {        link=0;        }    Term(int c,int e,Term* nxt):coef(c),exp(e),link(nxt){}    Term* InsertAfter(int c,int e);};Term* Term::InsertAfter(int c,int e){    link=new Term(c,e,link);    return link;}ostream& operator<<(ostream& os,const Term& val){    if(val.coef==0)return os;    os<<val.coef;    switch(val.exp)    {        case 0:break;        case 1:os<<"X";break;        default:os<<"X^"<<val.exp;break;    }    return os;}class Polynominal{    Term* theList;    friend ostream& operator<<(ostream&,const Polynominal&);    friend istream& operator>>(istream&, Polynominal&);    friend Polynominal operator+ (Polynominal&,Polynominal&);public:    Polynominal()    {        theList=new Term(0,-1);        theList->link=theList;    }    void AddTerms(istream& in);    void Output(ostream& out)const;    void PolyAdd(Polynominal& r);};void Polynominal::AddTerms(istream& in){    Term* q=theList;    int c,e;    for(;;)    {        cout<<"input a term(coef,exp):"<<endl;        in>>c>>e;        if(e<0)break;        q=q->InsertAfter(c,e);    }}void Polynominal::Output(ostream& out)const{    int first=1;    Term* p=theList->link;    cout<<"the polynominal is:"<<endl;    for(;p!=theList;p=p->link)    {        if(!first&&(p->coef>0))            out<<"+";        first=0;        out<<*p;        }    cout<<endl;}void Polynominal::PolyAdd(Polynominal& r){    Term* q,*q1=theList,*q2,*p;    p=r.theList;    q=q1->link;    p=p->link;    while(p->exp>=0)    {        while(p->exp<q->exp)        {            q1=q;            q=q->link;        }        if(p->exp==q->exp)        {            q->coef=p->coef+q->coef;            if(q->coef==0)            {                q2=q;                q1->link=q->link;                q=q->link;                delete q2;                p=p->link;            }            else            {                q1=q;                q=q->link;                p=p->link;            }        }        else        {            q1=q1->InsertAfter(p->coef,p->exp);            p=p->link;        }        }}ostream& operator<< (ostream& out,const Polynominal& x){    x.Output(out);    return out;}istream& operator>> (istream& in, Polynominal& x){    x.AddTerms(in);    return in;}Polynominal operator+(Polynominal &a,Polynominal &b){    a.PolyAdd(b);    return a;}int main(){    Polynominal p,q;    cin>>p;    cout<<p;    cin>>q;    cout<<q;    q=q+p;    cout<<q;} 

读书人网 >C++

热点推荐