多项式ADT链表实现
头文件
typedef struct Node *PtrToNode;
struct Node
{
int Cofficient;
int Exponent;
PtrToNode Next;
};
typedef PtrToNode Polynomial;
代码部分
#include <stdio.h>
#include <stdlib.h>
#include "PloynomialADTList.h"
#include<time.h>
#define SIZENODE 10
void ShowAPoly(Polynomial Poly)
{
Polynomial P1 = Poly;
for (; P1 != NULL; P1 = P1->Next)
{
printf("%d %d ", P1->Cofficient, P1->Exponent);
}
}
Polynomial ZeroPloynomial(Polynomial Poly)
{
Poly=(Polynomial)malloc(sizeof(struct Node));
Poly->Next = NULL;
Poly->Cofficient = 0;
Poly->Exponent = 0;
return Poly;
}
Polynomial InsertPolynomial(int Cofficient1, int Exponent1, Polynomial Poly)
{
Polynomial temp = (Polynomial)malloc(sizeof(struct Node));
temp->Next = NULL;
temp->Cofficient = Cofficient1;
temp->Exponent = Exponent1;
Polynomial p = Poly;
while (Poly)
{
Polynomial last = NULL;
if (Poly->Exponent == temp->Exponent)
{
Poly->Cofficient += temp->Cofficient;
continue;
}
else if (Poly->Exponent < temp->Exponent)
{
last = Poly;
Poly = Poly->Next;
}
else if (Poly->Exponent > temp->Exponent)
{
last->Next = temp;
temp->Next = Poly;
}
}
return p;
}
Polynomial RandAPoly(Polynomial Poly)
{
srand(unsigned(time(NULL)));
for(int i=0;i<SIZENODE;i++)
{
int cofficient=rand()%SIZENODE;
int exponent=rand()%SIZENODE;
Poly=InsertPolynomial(cofficient,exponent,Poly);
}
return Poly;
}
void AddPloynomial(const Polynomial Poly1, const Polynomial Poly2, Polynomial PloySum)
{
Polynomial p1 = Poly1;
Polynomial p2 = Poly2;
while (p1 && p2)
{
if (p1->Exponent == p2->Exponent)
{
PloySum->Exponent = p1->Exponent;
PloySum->Cofficient = p1->Cofficient + p2->Cofficient;
p1 = p1->Next;
p2 = p2->Next;
}
else if (p1->Exponent < p2->Exponent)
{
PloySum->Exponent = p1->Exponent;
PloySum->Cofficient = p1->Cofficient;
p1 = p1->Next;
}
else
{
PloySum->Exponent = p2->Exponent;
PloySum->Cofficient = p1->Cofficient + p2->Cofficient;
p2 = p2->Next;
}
}
PloySum->Next = p1 ? p1 : p2;
}
void MultPloynomial(const Polynomial Poly1, const Polynomial Poly2, Polynomial PloyProd)
{
Polynomial p1 = Poly1;
Polynomial p2 = Poly2;
for (; p1 != NULL; p1 = p1->Next)
{
for (; p2 != NULL; p2 = p2->Next)
{
int Cofficient = p1->Cofficient * p2->Cofficient;
int Exponent = p1->Exponent + p2->Exponent;
PloyProd = InsertPolynomial(Cofficient, Exponent, PloyProd);
}
}
}
int main(int argc, char const *argv[])
{
int number;
Polynomial p1=nullptr, p2=nullptr, psum=nullptr, pprod=nullptr;
p1=ZeroPloynomial(p1);
p2=ZeroPloynomial(p2);
psum=ZeroPloynomial(psum);
pprod=ZeroPloynomial(pprod);
while (1)
{
printf("\t\t\tpolynomial realize by list.\n");
printf("\t\t\tplease intput a intger to choose what you want to do.\n");
printf("\t\t\t1.insert a node to a poly.\n");
printf("\t\t\t2.show a poly.\n");
printf("\t\t\t3.puls two poly.\n");
printf("\t\t\t4.mult two poly.\n");
printf("\t\t\t5.get a random poly.\n");
printf("\t\t\t0.exit\n");
scanf("%d", &number);
if (!number)
break;
switch (number)
{
case 1:
printf("\t\t\tplease input a intger to choose a poly to insert.\n");
printf("\t\t\t1.poly1\t2.poly2\n\n");
int ch1, ch2;
scanf("%d", &ch1);
printf("\t\t\tplease intput two number which means a cofficient and its exponent.\n");
if (ch1 == 1)
{
scanf("%d%d", &ch1, &ch2);
InsertPolynomial(ch1, ch2, p1);
ShowAPoly(p1);
}
else if (ch1 == 2)
{
scanf("%d%d", &ch1, &ch2);
InsertPolynomial(ch1, ch2, p2);
ShowAPoly(p2);
}
break;
case 2:
printf("\t\t\tchoose a poly to show.\n");
printf("\t\t\t1.poly1\t2.poly2\n\n");
scanf("%d", &ch1);
if (ch1 == 1)
{
ShowAPoly(p1);
}
else if (ch1 == 2)
{
ShowAPoly(p2);
}
break;
case 3:
AddPloynomial(p1, p2, psum);
printf("\n\t\there is the answer.\n");
ShowAPoly(psum);
break;
case 4:
MultPloynomial(p1, p2, pprod);
printf("\n\t\there is the answer.\n");
ShowAPoly(pprod);
break;
case 5:
printf("\t\t\tplease input a intger to choose a poly.\n");
printf("\t\t\t1.poly1\t2.poly2\n\n");
scanf("%d", &ch1);
if (ch1 == 1)
{
p1=RandAPoly(p1);
ShowAPoly(p1);
}
else if (ch1 == 2)
{
scanf("%d %d", &ch1, &ch2);
p2=RandAPoly(p2);
ShowAPoly(p2);
}
break;
}
}
system("pause");
return 0;
}