#include <stdio.h>
#include <stdlib.h>
#include<iostream>
using namespace std;
#define MAXSIZE 100
#define OK 1
#define OVERFLOW -1
#define ERROR -2
#define False 0
typedef int status;
typedef int ElemType;
typedef struct
{
int coef;
int exp;
}polynomial;
typedef struct
{
polynomial *elem;
int length;
int listsize;
}Sqlist;
status Initlist_Sq(Sqlist &L)
{
L.elem=new polynomial[MAXSIZE];
if(!L.elem) exit(OVERFLOW);
L.length=0;
return OK;
}
int ListInput(Sqlist *L,polynomial e)
{
int i=L->length;
L->elem[i]=e;
L->length++;
return OK;
}
void Input1(Sqlist &L)
{
int n,i;
printf("输入多项式的项数:\n");
scanf("%d",&n);
printf("多项式:\n");
for(i=0;i<n;i++)
{
scanf("%d %d",&L.elem[i].coef,&L.elem[i].exp);
L.length++;
}
}
status ListInsert_Sq(Sqlist &L,int i,int e1,int e2)
{
if(i<1||i>L.length) return ERROR;
if(L.length==MAXSIZE) return ERROR;
for(int j=L.length-1;j>=i-1;j--)
{
L.elem[j+1]=L.elem[j];
}
L.elem[i-1].coef=e1;
L.elem[i-1].exp=e2;
++L.length;
return OK;
}
status Listadd(Sqlist &p,Sqlist &q)
{
int k;
Sqlist c;
c.elem =new polynomial;
//c数组的最大存储容量一定为p,q容量之和
int i, j;
//i-p j-q k-c
i = j = k = 0;
while (i<p.length&&j<q.length)
{
if (p.elem[i].exp< q.elem[j].exp) {
c.elem[k++] = p.elem[i];
i++;
}
else if (p.elem[i].exp > q.elem[j].exp) {
c.elem[k++] = q.elem[j];
j++;
}
else {
if (p.elem[i].coef!=0&&q.elem[j].coef!=0) {
c.elem[k].coef = p.elem[i].coef + q.elem[j].coef;
c.elem[k].exp = p.elem[i].exp;
k++;
i++;
j++;}
if(p.elem[i].coef==0){
c.elem[k].coef =q.elem[j].coef;
j++;
k++;}
}
if(q.elem[j].coef==0)
{
c.elem[k].coef =p.elem[i].coef;
k++;
i++;
}
}
while(i<p.length){
c.elem[k++]=p.elem[i];
i++;
}
while(j<q.length){
c.elem[k]=q.elem[j];
k++;
j++;
}
p.elem=c.elem;
return OK;
}
status Listsub_(Sqlist &p,Sqlist &q)
{
Sqlist c;
c.elem = (polynomial *)malloc((p.length + q.length) * sizeof(polynomial));
//c数组的最大存储容量一定为p,q容量之和
int i, j, k;
//i-p j-q k-c
i = j = k = 0;
while (i<p.length&&j<q.length)
{
if (p.elem[i].exp < q.elem[j].exp) {
c.elem[k++] = p.elem[i];
i++;
}
else if (p.elem[i].exp > q.elem[j].exp) {
c.elem[k++] = q.elem[j];
j++;
}
else {
if (p.elem[i].coef - q.elem[j].coef) { //系数相减不为0时
c.elem[k].coef = p.elem[i].coef - q.elem[j].coef;
c.elem[k].exp = p.elem[i].exp;
k++;
i++;
j++;
}
else{ //为0时
i++;
j++;
}
}
}
while(i<p.length){
c.elem[k++]=p.elem[i];
i++;
}
while(j<q.length){
c.elem[k]=q.elem[j];
c.elem[k].coef=-c.elem[k].coef;
k++;
j++;
}
p.elem=c.elem;
return OK;
}//顺序表实现多项式减法
//顺序表查找值为e的数据元素
int LocateELem(Sqlist &L,polynomial e1,polynomial e2)
{
for (int i=0;i< L.length;i++)
if (L.elem[i].coef==e1.coef&&L.elem[i].exp==e2.exp)
return i+1;
else return 0;
}
//顺序表中删除第i组数据元素
status ListDelete_Sq(Sqlist &L,int i)
{
int j;
if((i<1)||(i>L.length)) return ERROR;//i值不合法
for(j=i;j<=L.length-1;j++)
{
L.elem [j-1].coef=L.elem [j].coef;
L.elem [j-1].exp=L.elem [j].exp;
} //被删除元素之后的元素前移
--L.length; //表长减1
return OK;
}
void Printlist(Sqlist &L)
{
printf("输出顺序表:");
printf("F(x)=");
for(int i=0;i<L.length;i++)
{
printf("%dX^%d",L.elem[i].coef,L.elem[i].exp);
if(i<L.length-1)
printf("+");
}
}
int main()
{
Sqlist a,b;
Initlist_Sq(a);
Initlist_Sq(b);
Input1(a);
Input1(b);
Listadd(a,b);
Printlist(a);
return 0;
}