#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int Coefficient;//多项式系数
int Exponent;//指数2
struct Node* Next;
};
typedef struct Node* nodePtr;
typedef nodePtr Polynomial;
Polynomial WritePoly();
void PrintPoly( Polynomial poly);
Polynomial AddPoly(Polynomial poly_1, Polynomial poly_2);
Polynomial MultPoly(Polynomial poly_1, Polynomial poly_2);
nodePtr Insert(int coefficient, int exponent, nodePtr position);
void freePoly(Polynomial poly);
int main(void)
{
Polynomial Poly_1 = WritePoly();
Polynomial Poly_2 = WritePoly();
PrintPoly(MultPoly(Poly_1, Poly_2));
PrintPoly(AddPoly(Poly_1,Poly_2));
freePoly(Poly_1);
freePoly(Poly_2);
return 0;
}
nodePtr Insert(int coefficient, int exponent, nodePtr position)
{
nodePtr insert = (nodePtr)malloc(sizeof(struct Node));
if (insert != NULL)
{
insert->Coefficient = coefficient;
insert->Exponent = exponent;
insert->Next = position->Next;
position->Next = insert;
}
return insert;
}
Polynomial WritePoly()
{
Polynomial Poly = (Polynomial)malloc(sizeof(struct Node));
if (Poly != NULL)
{
Poly->Next = NULL;
nodePtr end = Poly;//设立尾结点
int item;//多项式的项数
scanf("%d", &item);
//如果多项式项数等于0,那么就不输入信息
if (item != 0)
{
for (int i = 0; i < item; i++)
{
Polynomial tmp = (Polynomial)malloc(sizeof(struct Node));
if (tmp != NULL)
{
scanf("%d %d", &(tmp->Coefficient), &(tmp->Exponent));
end->Next = tmp;
end = tmp;
}
}
end->Next = NULL;
}
}
return Poly;
}
void PrintPoly(Polynomial poly)
{
if (poly != NULL)
{
Polynomial P = poly->Next;
if (P != NULL)//当多项式项数不为0时
{
printf("%d %d", P->Coefficient, P->Exponent);
P = P->Next;
while (P != NULL)
{
printf(" %d %d", P->Coefficient, P->Exponent);
P = P->Next;
}
}
else
{
printf("0 0");
}
printf("
");
}
freePoly(poly);
}
Polynomial AddPoly(Polynomial poly_1,Polynomial poly_2)
{
Polynomial resultPoly = (Polynomial)malloc(sizeof(struct Node));
if (resultPoly != NULL)
{
resultPoly->Next = NULL;
Polynomial curPoly1 = poly_1->Next;
Polynomial curPoly2 = poly_2->Next;
Polynomial lastPoly = resultPoly;
while (curPoly1 != NULL && curPoly2 != NULL)
{
if (curPoly1->Exponent != curPoly2->Exponent)
{
nodePtr ins = curPoly1->Exponent > curPoly2->Exponent ? curPoly1 : curPoly2;
lastPoly = Insert(ins->Coefficient, ins->Exponent, lastPoly);
if (ins == curPoly1)
{
curPoly1 = curPoly1->Next;
}
else
{
curPoly2 = curPoly2->Next;
}
}
else
{
int sumCoef = curPoly1->Coefficient + curPoly2->Coefficient;
if (sumCoef != 0)
{
lastPoly = Insert(sumCoef, curPoly1->Exponent, lastPoly);
}
curPoly1 = curPoly1->Next;
curPoly2 = curPoly2->Next;
}
}
Polynomial waitingforInsert = NULL;
if (curPoly1 != NULL || curPoly2 != NULL)
{
waitingforInsert = (curPoly1 == NULL) ? curPoly2 : curPoly1;
}
else//curPoly1 = NULL && curPoly2 = NULL
{
waitingforInsert = NULL;
}
while (waitingforInsert != NULL)
{
lastPoly = Insert(waitingforInsert->Coefficient, waitingforInsert->Exponent, lastPoly);
waitingforInsert = waitingforInsert->Next;
}
lastPoly->Next = NULL;
}
return resultPoly;
}
Polynomial MultPoly(Polynomial poly_1, Polynomial poly_2)
{
Polynomial resultPoly = (Polynomial)malloc(sizeof(struct Node));
if (resultPoly != NULL)
{
resultPoly->Next = NULL;
Polynomial curPoly1 = poly_1->Next;
Polynomial curPoly2 = poly_2->Next;
Polynomial lastPoly = resultPoly;
while (curPoly1 != NULL)
{
Polynomial polyTmp = (Polynomial)malloc(sizeof(struct Node));
if (polyTmp != NULL)
{
nodePtr ins = polyTmp;
polyTmp->Next = NULL;
curPoly2 = poly_2->Next;
while (curPoly2 != NULL)
{
int newCoef = curPoly1->Coefficient * curPoly2->Coefficient;
int newExpo = curPoly1->Exponent + curPoly2->Exponent;
ins = Insert(newCoef, newExpo, ins);
curPoly2 = curPoly2->Next;
}
ins->Next = NULL;
}
resultPoly = AddPoly(polyTmp, resultPoly);
freePoly(polyTmp);
curPoly1 = curPoly1->Next;
}
lastPoly->Next = NULL;
}
return resultPoly;
}
void freePoly(Polynomial poly)
{
nodePtr P = poly;
nodePtr tmp;
while (P != NULL)
{
tmp = P->Next;
free(P);
P = tmp;
}
}