# 数据结构多项式乘积

#include<stdio.h>
#include<math.h>
#include<iostream>
#include<malloc.h>
#include<stdlib.h>
#include<conio.h>
using namespace std;
#define Listsize 20
#define OK 1
#define ERROR 0

typedef int Status;
typedef struct {
float coef;   //系数
int expn;     //指数

typedef struct LNode{       //结点类型
ElemType data;
struct LNode *next;

typedef struct {            //链表类型
int len;                //指示线性链表中数据元素个数

L.len = 0;
return OK;
}
}
P->data.coef = e.coef;
P->data.expn = e.expn;
return OK;
}
return h->next;
}
if(cur != NULL){
for (int i = 1; i <= P.len; i ++){
if(cur->data.expn == e.expn){
found = cur;
return OK;
}
else
cur = cur->next;
}
}
return ERROR;
}
s->data.coef = e.coef;
s->data.expn = e.expn;
return OK;
}
s->next = h->next;
h->next = s;
h = s;
free(s);
return OK;
}

InitList(L);
int m;
ElemType e;
e.coef = 0.0; e.expn = -1;
setCurElem(h,e);//设置头结点数据元素
cout << "Please input the term :  ";
cin >> m;
for(int i = 1; i <= m; i ++){//依次输入m个非零项
cin >> e.coef >> e.expn;
if(MakeNode(s, e)) {
if(InsFirst(h, s, e));
L.len ++;   //元素个数自加
}
}
}
}
cout << endl << "the first polynomial is : ";
for(int i = 1; i <= P.len; i ++){
cout << curp->data.coef << " " << curp->data.expn << " ";
curp = curp->next;
}
cout << endl <<"the second polynomial is : ";

for(int j = 1; j <= Q.len; j ++){
cout << curq->data.coef << " " << curq->data.expn << " ";
curq = curq->next;
}
cout << endl;
}
found->data.coef += temp->data.coef;
free(found);
}
else{
temp->next = hr->next;
hr->next = temp;
hr = temp;
free(temp);
R.len ++;
}
return OK;
}
cout << endl << "The multiple is: ";
for(int i = 1; i <= R.len; i ++){
cout << curr->data.coef << " " << curr->data.expn << " ";
curr = curr->next;
}
cout << endl;
return OK;
}
int length;//存储乘积的链表的长度
InitList(R);
for(int i = 1; i <= P.len; i ++){
for(int j= 1; j <= Q.len; j ++){
temp->data.coef = curp->data.coef * curq->data.coef;
temp->data.expn = curp->data.expn + curq->data.expn;
InsertR(R, temp, hr);
curq = curq->next;
}
curp = curp->next;
curq = NextPos(Q,hq);
}
printR(R);
}

int main(){
CreatPolyn(P);//输入多项式P
CreatPolyn(Q);//输入多项式Q
PrintPQ(P,Q);//打印多项式P和Q
multiple(P,Q);//输出多项式乘积