#include <iostream>
using namespace std;
struct poly
{
float coef;
int expon;
poly *next;
};
void createpoly(poly *head)//創建鏈表
{
int a,n;
poly *s,*p;
p=head;
do
{
cin>>a>>n;
if(a!=10000)
{
s=new poly;
s->next=NULL;
s->coef=a;
s->expon=n;
p->next=s;
p=s;
}
}while(a!=10000);
}
void hebing(poly *head)//合并同類項,若合并后系數為零,則洗掉此結點
{
poly *p,*q,*h,*j,*i;
for(p=head->next;p->next!=NULL;p=p->next)
{
for(q=p,h=p->next;h!=NULL;)
{
if(h->expon==p->expon)
{
p->coef+=h->coef;
q->next=h->next;
h=q->next;
}
else
{
h=h->next;
q=q->next;
}
}
}
for(i=head,j=head->next;j!=NULL;)
{
if(j->coef==0)
{
i->next=j->next;
delete(j);
j=i->next;
}
else
{
j=j->next;
i=i->next;
}
}
}
void pailie(poly *head)//對多項式中各項按照冪的大小,從大到小排列
{
int i,j;
poly *p,*q;
for(p=head->next;p->next!=NULL;p=p->next)
{
i=p->coef;
j=p->expon;
for(q=p->next;q!=NULL;q=q->next)
{
if(q->expon>j)
{
i=q->coef;
j=q->expon;
}
}
if(p->expon!=j)
{
for(q=p->next;q->expon!=j;q=q->next);
q->coef=p->coef;
q->expon=p->expon;
p->coef=i;
p->expon=j;
}
}
}
void printpoly(poly *head)//兩個多項式的加法
{
int m=1,n=0;
poly *p;
hebing(head);
pailie(head);
for(p=head->next;p!= NULL;p=p->next)
n++;
cout<<n<<",";
for(p=head->next;p!= NULL;p=p->next)
{
cout<<p->coef<<","<<p->expon<<",";
}
cout<<endl;
}
poly*addpoly(poly *pa,poly *pb)
{
poly *p,*s,*i,*j;
i=pa;
j=pb;
s=pa->next;
p=pb->next;
while(s!=NULL&&p!=NULL)
{
if(i->next->expon==j->next->expon)
{
s->coef+=p->coef;
if(s->coef!=0)
{
i=i->next;
s=s->next;
delete(j);
j=new poly;
p=p->next;
j->next=p;
}
else
{
i->next=s->next;
delete s;
s=i->next;
delete j;
j=new poly;
p=p->next;
j->next=p;
}
}
else if(i->next->expon<j->next->expon)
{
delete(j);
j=new poly;
j->next=p->next;
i->next=p;
p->next=s;
p=j->next;
i=i->next;
}
else
{
i=i->next;
s=s->next;
}
}
if(p!=NULL)
{
delete(j);
i->next=p;
}
return (pa);
}
poly *multiply(poly *pa,poly *pb)//在加法的基礎上實作乘法運算
{
int k=0;
poly *str[100],*p,*s,*sum,*i,*j;
i=pa->next;
j=pb->next;
for(i;i!=NULL;i=i->next,k++)
{
str[k]=new poly;
str[k]->next=NULL;
p=str[k];
j=pb->next;
for(j;j!=NULL;j=j->next )
{
s=new poly;
s->next=NULL;
s->expon=i->expon+j->expon;
s->coef=i->coef*j->coef;
p->next=s;
p=s;
}
}
sum=str[0];
for(int m=1;m<k;m++)
sum=addpoly(sum,str[m]);
return (sum);
}
int main()
{
poly *poly1,*poly2,*poly3,*poly4,*head1,*p1;
head1=new poly;
head1->next=NULL;
p1=new poly;
p1->next=NULL;
cout<<"在輸入多項式時要注意:"<<endl;
cout<<"(1)隨便輸入資料,不必顧及其他"<<endl;
cout<<"(2)多項式輸出的形式是整數序列:n,coef1,expon1,coef2,expon2.....coefn,exponn.其中n是多項式的項數,coefi和exponi分別是地i項的系數和指數。序列按指數降序排列"<<endl;
cout<<"*******************************************************************************"<<endl;
cout<<"請輸入第一個多項式a每個結點的系數和指數,每輸一組按一次回車鍵,輸入10000 10000結束"<<endl;
createpoly(head1);
poly1=head1;
cout<<"您建立第一個多項式a成功,輸出最終運算式為:"<<endl;
cout<<endl;
printpoly(poly1);
cout<<endl;
cout<<"*******************************************************************************"<<endl;
cout<<"請輸入第二個多項式b每個結點的系數和指數,每輸一組按一次回車鍵,輸入10000 10000結束"<<endl;
createpoly(p1);
poly2=p1;
cout<<"您建立第二個多項式b成功,輸出最終運算式為:"<<endl;
cout<<endl;
printpoly(poly2);
cout<<endl;
cout<<"*******************************************************************************"<<endl;
poly4=multiply(poly1,poly2);
cout<<"您輸入的兩個多項式相乘,即a*b結果為:"<<endl;
cout<<endl;
printpoly(poly4);
cout<<endl;
cout<<"此程式到此結束"<<endl;
cout<<"********************************************************************************"<<endl;
return 0;
}
uj5u.com熱心網友回復:

別人寫好的代碼,還是要看懂的
自己不寫就算了!
uj5u.com熱心網友回復:
只能看懂一點,是在沒辦法了uj5u.com熱心網友回復:
找本資料結構的書,看下鏈表內容,然后設斷點除錯理解程式,自己看懂了才行,別人說不懂的
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/97282.html
標籤:基礎類
