#include"stdafx.h"
class list{
public:
int cofe,exp;//宣告係數和值數
class list *next;//指向下一個節點
}
typedef class list node;
typedef node *link;
//建立多項式
link head,newnode,ptr;//告指標 head是第一節點,newnode新的節點,ptr目前節點
int data1[4]={3,0,4,2};
int data2[4]={6,8,6,9};
for(int i=0;i<4;i++)
{
newnode=new node;//配一個新的記憶體給newnode
if(!newnode)
{
cout<<";Error!!記憶體配置失敗!!";<<endl;
exit(1);
}
if(i==0)
{
newnode->cofe=data1[i];//將data1的係數填入
newnode->exp=3-i;//直接給值數為3
newnode->next=NULL;//將新建好的newnode的下一個節點=NULL
head=newnode;//head指向newnode
ptr=head;//prt指向head
}//到這裡data1的第一個節點建好了
else if(data[i]!=0){
newnode->cofe=data[i];
newnode->exp=3-i;
newnode->next=NULL;
ptr->next=newnode;//ptr的下一個節點是newnode
ptr=newnode;//ptr 指向newndoe
}
}
return head;//這裡是把整條鏈結建好(data1)//data2一樣
link sum_link(link a,link b)//副程式,將data1和data2的鏈結傳入
{
int sum[4],i=0;
link ptr;
ptr=b;
while(a!=NULL)//判斷a是不是NULL
{
b=ptr;//重複比較a和b的指數
while(b!= NULL)//判斷b是不是NULL
{
if(a->exp==b->exp)//可以想成第一個節點直數都是3
{
sum[i]=a->cofe+b->cofe;//系數相加存入sum[i]
a=a->next;//a的下一個節點
b=b->next;//b的下一個節點
i++;
}
else if (b->exp>a->exp)//可以想成b的直數比a大
{
sum[i]=b->cofe;//只有b的系數存入
b=b->next;
i++;
}
else if(a->exp>b->exp)
{
sum[i]=a->cofe;
a=a->next;
i++;
}
}
}
return sum;
}
I2luY2x1ZGUic3RkYWZ4LmgiCmNsYXNzIGxpc3R7CnB1YmxpYzoKaW50IGNvZmUsZXhwOy8v5a6j5ZGK5L+C5pW45ZKM5YC85pW4CmNsYXNzIGxpc3QgKm5leHQ7Ly/mjIflkJHkuIvkuIDlgIvnr4Dpu54KfQp0eXBlZGVmIGNsYXNzIGxpc3Qgbm9kZTsKdHlwZWRlZiBub2Rl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