一元稀疏多項式相加

是小飛象啦啦啦發表於2020-11-20

一元稀疏多項式相加

實驗原理
通過連結串列儲存多項式的係數和指數。對於指數並不是按照從低到高排列的多項式,通過氣泡排序的思想將指數從低到高排序,構成有序的多項式連結串列。在計算多項式加法的時候,通過定義一個新的連結串列用於儲存相加之後的多項式,然後定義兩個分別指向兩個待加連結串列首元的指標,比較兩個指標指向的指數域的大小關係,進行結點之間的交換、相加等連結串列操作,所有操作結束後,返回新的連結串列,及相加後的多項式,並用迴圈語句輸出新連結串列各結點的係數域和指數域,程式結束。

程式碼實現

#include<stdio.h>
#include<stdlib.h>
#define OK			1
#define TRUE		1
#define ERROR		0
#define FALSE		0
#define OVERFLOW	-1
typedef struct LNode {
	float coef;				//係數
	int expn;				//指數
	struct LNode* next;
}LNode, * Polynomail;			//多項式
typedef int Status;

Polynomail InitPolyn() {
	Polynomail P = (Polynomail)malloc(sizeof(LNode));
	if (!P) { printf("\n記憶體分配失敗\n");exit(OVERFLOW); }
	P->next = NULL;
	return P;
}
Status CreatPolyn(Polynomail P, int m) {
	float a = 0.0;
	int b = 0;
	LNode* Q = NULL;
	Q = P;
	for (int i = 0;i < m;i++) {
		printf("\n請輸入第%d項元素的係數和指數(eg:2 3):", i + 1);
		scanf_s("%f %d", &a, &b);
		LNode* new = (LNode*)malloc(sizeof(LNode));
		if (!new) { printf("\n記憶體分配失敗\n");exit(OVERFLOW); }
		new->coef = a;new->expn = b;
		new->next = NULL;Q->next = new;
		Q = new;
	}
	return OK;
}
Status PrintPolyn(Polynomail P) {
	LNode* Q = P->next;
	if (Q == NULL) { printf("\n空表\n");return FALSE; }
	while (Q != NULL) {
		printf("%.1fX^%d ", Q->coef, Q->expn);
		Q = Q->next;
	}
	printf("\n輸出成功!\n");
	return OK;
}
LNode* CreateNew(void) {
	LNode* new = (LNode*)malloc(sizeof(LNode));
	if (!new) { printf("\n記憶體分配失敗\n");exit(OVERFLOW); }
	return new;
}
Polynomail AddTwoPolyns(Polynomail La, Polynomail Lb) {
	Polynomail Lc;
	Lc = (Polynomail)malloc(sizeof(LNode));
	if (!Lc) { printf("\n記憶體分配失敗\n");exit(OVERFLOW); }
	LNode* pa = La->next;
	LNode* pb = Lb->next;
	LNode* pc = Lc;
	while (pa && pb) {
		if (pa->expn < pb->expn) {
			LNode* new = CreateNew();
			new->coef = pa->coef;new->expn = pa->expn;
			pc->next = new;pc = new;pa = pa->next;
		}
		else if (pa->expn == pb->expn || pa->expn == 0) {
			LNode* new = CreateNew();
			new->coef = pa->coef + pb->coef;new->expn = pa->expn;
			pc->next = new;pc = new;pa = pa->next;pb = pb->next;
		}
		else if(pb->expn < pa->expn){
			LNode* new = CreateNew();
			new->coef = pb->coef;new->expn = pb->expn;
			pc->next = new;pc = new;pb = pb->next;
		}
	}
	while (pa != NULL) {
		LNode* new = CreateNew();
		new->coef = pa->coef;new->expn = pa->expn;
		pc->next = new;pc = new;pa = pa->next;
	}
	while (pb != NULL) {
		LNode* new = CreateNew();
		new->coef = pb->coef;new->expn = pb->expn;
		pc->next = new;pc = new;pb = pb->next;
	}
	pc->next = NULL;
	return Lc;
}
Polynomail SubTwoPolyns(Polynomail La, Polynomail Lb) {
	Polynomail Lc;
	Lc = (Polynomail)malloc(sizeof(LNode));
	if (!Lc) { printf("\n記憶體分配失敗\n");exit(OVERFLOW); }
	LNode* pa = La->next;
	LNode* pb = Lb->next;
	LNode* pc = Lc;
	while (pa && pb) {
		if (pa->expn < pb->expn) {
			LNode* new = CreateNew();
			new->coef = pa->coef;new->expn = pa->expn;
			pc->next = new;pc = new;pa = pa->next;
		}
		else if (pa->expn == pb->expn || pa->expn == 0) {
			LNode* new = CreateNew();
			new->coef = pa->coef - pb->coef;new->expn = pa->expn;
			pc->next = new;pc = new;pa = pa->next;pb = pb->next;
		}
		else if (pb->expn < pa->expn) {
			LNode* new = CreateNew();
			new->coef = pb->coef;new->expn = pb->expn;
			pc->next = new;pc = new;pb = pb->next;
		}
	}
	while (pa != NULL) {
		LNode* new = CreateNew();
		new->coef = pa->coef;new->expn = pa->expn;
		pc->next = new;pc = new;pa = pa->next;
	}
	while (pb != NULL) {
		LNode* new = CreateNew();
		new->coef = pb->coef;new->expn = pb->expn;
		pc->next = new;pc = new;pb = pb->next;
	}
	pc->next = NULL;
	return Lc;
}
Status PolynSert(Polynomail P,int num) {
	for (int i = 0;i < num - 1;i++) {//氣泡排序法
		LNode* point1 = P->next;//指向第一個結點
		LNode* point2 = P->next->next;//指向第二個結點
		for (int j = 0;j < num - 1 - i;j++) {
			if (point1->expn > point2->expn)//如果前項的指數大於後項的指數
			{
				LNode* new = (LNode*)malloc(sizeof(LNode));
				if (!new) { printf("\n記憶體分配失敗\n");exit(OVERFLOW); }
				new->coef = point1->coef;
				new->expn = point1->expn;
				point1->coef = point2->coef;
				point1->expn = point2->expn;
				point2->coef = new->coef;
				point2->expn = new->expn;
				//交換兩個結點的指數和係數
			}
			point1 = point1->next;
			point2 = point2->next;
			//指標移向下兩個待比較元素
		}
	}
	printf("\n排序完成!\n");
	return OK;
}

void main() {
	Polynomail La = NULL;
	Polynomail Lb = NULL;	
	Polynomail Lc = NULL;
	La = InitPolyn();
	Lb = InitPolyn();
	int num = 0;

	printf("\n輸入La的項數:");
	scanf_s("%d", &num);
	CreatPolyn(La, num);
	PolynSert(La, num);
	PrintPolyn(La);

	printf("\n輸入Lb的項數:");
	scanf_s("%d", &num);
	CreatPolyn(Lb, num);
	PolynSert(Lb, num);
	PrintPolyn(Lb);

	Lc=AddTwoPolyns(La, Lb);
	printf("\n多項式的和為:");
	PrintPolyn(Lc);
}

相關文章