-
-
[旧帖] [原创]学习数据结构,第一个程序,一元多项式链表乘法,写了一下午还是有错误,不管怎样,纪念一下 0.00雪花
-
发表于: 2012-3-9 22:47 1011
-
#include<iostream>
using namespace std;
struct node
{
float coef;
int expn;
node *next;
};
node *insert(node *head, node *num)
{
node *p;
p=head;
if(p==NULL)
{
head=num;
num->next=NULL;
return head;
}
if(p->expn>num->expn)
{
num->next=head;
head=num;
return head;}
while(p->next&&p->next->expn<num->expn)
p=p->next;
num->next=p->next;
p->next=num;
return head;
}
node *arrange()
{
node *head, *p;
int i;
head=NULL;
while(1)
{
cout<<"插入吗?(yes--1/no--0):";
cin>>i;
if(i==0) break;
p= new node;
cout<<"请输入系数和指数:"<<endl;
cin>>p->coef;
cin>>p->expn;
head=insert(head,p);
}
return head;}
node *delfirst(node *p, node *q)
{q=p;
q->next=p->next->next;
return q;
}
node *insfirst(node *p, node *q)
{
q->next=p->next;
p->next=q;
return p;
}
void add(node *pa, node *pb)
{
node *qa, *qb;
qa=pa->next;
qb=pb->next;
int a,b;node *ha,*hb; float sum;ha=pa;hb=pb;
while(qa&&qb)
{
a=qa->expn;
b=qb->expn;
if(a<b)
{
ha=qa;
qa=qa->next;
break;}
else if(a==b)
{
sum=qa->coef+qb->coef;
if(sum!=0.0){qa->coef=sum;ha=qa;}
else{delfirst(ha,qa);delete qa;}
delfirst(hb,qb);delete qb; qb=hb->next;qa=ha->next;break;}
if(a>b)
{
delfirst(hb,qb);insfirst(ha,qb);qb=hb->next;ha=ha->next;break;
}
}
if(pb->next!=NULL)
{
while(ha->next!=NULL)
ha=ha->next;
ha->next=qb;
}
delete hb;
}
void print(node *p)
{ node *q;
q=p->next;
while(q!=NULL)
{
if(p->next==q)
cout<<q->coef;
else cout<<"+"<<q->coef;
if(q->expn!=0)
cout<<"x^"<<q->expn;
q=q->next;
}
}
int main()
{
node *p1, *p2;
cout<<"请输入多项式P1:";
p1=arrange();
cout<<"请输入多项式P2:";
p2=arrange();
node *pa,*pb,*x,*y,*pc;
pa=p1;x=pa;pc=NULL;pb=pc;
for(y=p2->next;y!=NULL;y=y->next)
{
for(x=p1->next;x!=NULL;x=x->next,pc=pc->next)
{
node *nod;
nod->coef=x->coef*y->coef;
nod->expn=x->expn+y->expn;
pc->next=nod;
delete nod;
}
add( pa,pb);
}
print(pa);
return 0;
}
using namespace std;
struct node
{
float coef;
int expn;
node *next;
};
node *insert(node *head, node *num)
{
node *p;
p=head;
if(p==NULL)
{
head=num;
num->next=NULL;
return head;
}
if(p->expn>num->expn)
{
num->next=head;
head=num;
return head;}
while(p->next&&p->next->expn<num->expn)
p=p->next;
num->next=p->next;
p->next=num;
return head;
}
node *arrange()
{
node *head, *p;
int i;
head=NULL;
while(1)
{
cout<<"插入吗?(yes--1/no--0):";
cin>>i;
if(i==0) break;
p= new node;
cout<<"请输入系数和指数:"<<endl;
cin>>p->coef;
cin>>p->expn;
head=insert(head,p);
}
return head;}
node *delfirst(node *p, node *q)
{q=p;
q->next=p->next->next;
return q;
}
node *insfirst(node *p, node *q)
{
q->next=p->next;
p->next=q;
return p;
}
void add(node *pa, node *pb)
{
node *qa, *qb;
qa=pa->next;
qb=pb->next;
int a,b;node *ha,*hb; float sum;ha=pa;hb=pb;
while(qa&&qb)
{
a=qa->expn;
b=qb->expn;
if(a<b)
{
ha=qa;
qa=qa->next;
break;}
else if(a==b)
{
sum=qa->coef+qb->coef;
if(sum!=0.0){qa->coef=sum;ha=qa;}
else{delfirst(ha,qa);delete qa;}
delfirst(hb,qb);delete qb; qb=hb->next;qa=ha->next;break;}
if(a>b)
{
delfirst(hb,qb);insfirst(ha,qb);qb=hb->next;ha=ha->next;break;
}
}
if(pb->next!=NULL)
{
while(ha->next!=NULL)
ha=ha->next;
ha->next=qb;
}
delete hb;
}
void print(node *p)
{ node *q;
q=p->next;
while(q!=NULL)
{
if(p->next==q)
cout<<q->coef;
else cout<<"+"<<q->coef;
if(q->expn!=0)
cout<<"x^"<<q->expn;
q=q->next;
}
}
int main()
{
node *p1, *p2;
cout<<"请输入多项式P1:";
p1=arrange();
cout<<"请输入多项式P2:";
p2=arrange();
node *pa,*pb,*x,*y,*pc;
pa=p1;x=pa;pc=NULL;pb=pc;
for(y=p2->next;y!=NULL;y=y->next)
{
for(x=p1->next;x!=NULL;x=x->next,pc=pc->next)
{
node *nod;
nod->coef=x->coef*y->coef;
nod->expn=x->expn+y->expn;
pc->next=nod;
delete nod;
}
add( pa,pb);
}
print(pa);
return 0;
}
[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课
赞赏
他的文章
看原图
赞赏
雪币:
留言: