#include
#include
#include
#include
using namespace std;
typedef struct LNode
{
float coef;
int exp;
struct LNode *next;
}LinkNode;
LinkNode* LocateAdd(LinkNode* list1, LinkNode* list2){
LinkNode* p1 = list1->next;
LinkNode* p2 = list2->next;
LinkNode* p3 = (LinkNode *)malloc(sizeof(LinkNode));
p3->coef = 0;
p3->exp = 0;
p3->next = NULL;
LinkNode* p4 = p3;
while (p1 && p2){
LinkNode* pNode = (LinkNode *)malloc(sizeof(LinkNode));
double coef1 = p1->coef;
int expn1 = p1->exp;
double coef2 = p2->coef;
int expn2 = p2->exp;
if (expn1 == expn2){
if (coef1 + coef2 != 0){
pNode->coef = coef1 + coef2;
pNode->exp = expn1;
pNode->next = NULL;
p4->next = pNode;
p4 = pNode;
}
p1 = p1->next;
p2 = p2->next;
free(pNode);
}
if (expn1 < expn2){
pNode->coef = coef1;
pNode->exp = expn1;
pNode->next = NULL;
p4->next = pNode;
p4 = pNode;
p1 = p1->next;
}
if (expn1 > expn2){
pNode->coef = coef2;
pNode->exp = expn2;
pNode->next = NULL;
p4->next = pNode;
p4 = pNode;
p2 = p2->next;
}
}
if (p1 == NULL){
while (p2){
LinkNode* ppNode = (LinkNode *)malloc(sizeof(LinkNode));
ppNode->coef = p2->coef;
ppNode->exp = p2->exp;
ppNode->next = NULL;
p4->next = ppNode;
p4 = ppNode;
p2 = p2->next;
}
}
else if (p2 == NULL){
while (p1){
LinkNode* ppNode = (LinkNode *)malloc(sizeof(LinkNode));
ppNode->coef = p1->coef;
ppNode->exp = p1->exp;
ppNode->next = NULL;
p4->next = ppNode;
p4 = ppNode;
p1=p1->next;
}
}
p4=p3->next;
while (p4!NULL)
{
cout<
p4=p4->next;
}
return ;
}
void InputFormat1(float &item1)
{
cout<<"Please enter the coefficient of an item !"< cin>>item1; } void InputFormat2(int &item2) { cout<<"Please enter the power series of item !"< cin>>item2; } int main() { float item_1=0; int item_2=0; LinkNode *QX; LinkNode *PX; LinkNode *Q_x; LinkNode *P_x; LinkNode *Tp,*Tm; PX=(LinkNode *)malloc(sizeof(LinkNode)); QX=(LinkNode *)malloc(sizeof(LinkNode)); Tp=PX; Tm=QX; int num; cout<<"Please enter the number of terms of polynomial P(x)!"< cin>>num; while(num>0) { P_x=(LinkNode *)malloc(sizeof(LinkNode)); num--; InputFormat1(item_1); P_x->coef=item_1; InputFormat2(item_2); P_x->exp=item_2; Tp->next=P_x; Tp=P_x; } cout<<"Please enter the number of terms of polynomial Q(x)!"< cin>>num; while(num>0) { num--; Q_x=(LinkNode *)malloc(sizeof(LinkNode)); InputFormat1(item_1); Q_x->coef=item_1; InputFormat2(item_2); Q_x->exp=item_2; Tm->next=Q_x; Tm=Q_x; } LocateAdd(PX,QX); }



