資料結構與演算法分析c語言描述(Mark Allen)--多項式ADT連結串列實現
阿新 • • 發佈:2018-12-11
多項式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
//視覺化一個多項式ADT
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;
}
//產生一個隨機的多項式ADT
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;
}
- 每日ADT
- 每日機器學習
- 每日leetcode
- 每日cpp