用單連結串列實現多項式加,減,乘,簡單微分

Code->Y發表於2020-10-05
#include<iostream>
using namespace std;

struct polynomial;//多項式的結構體
typedef polynomial* PtrToPoly;
typedef PtrToPoly List;
typedef PtrToPoly Position;

//儲存多項式的係數和次數
typedef struct polynomial {
	int coef;//多項式中單項式的係數
	int power;//多項式中單項式的指數
	Position next;//指標域
};



//多項式相加,L1和L2是需相加的多項式,L3是兩個多項式相加後的結果
void Addpoly(List& L1, List& L2, List& L3)
{
	Position p;//定義一個工作指標
	p = L1->next;//將工作指標指向頭結點
	Position w = L3;
	//兩個while迴圈查詢L2中與L1相同指數的單項式,但不包括L2有L1沒有的情況
	while (p != NULL)
	{
		Position q;
		q = L2->next;
		bool flag = true;//判斷是否有L1有,L2無的情況(true為此情況)
		while (q != NULL)
		{
			if (p->power == q->power)
			{
				flag = false;//兩個單項的指數相等
				Position TmpCell = (PtrToPoly)malloc(sizeof(struct polynomial));//開闢存放多項式資訊的空間
				TmpCell->coef = p->coef + q->coef;
				TmpCell->power = q->power;
				w->next = TmpCell;
				w = w->next;
				break;
			}
			q = q->next;
		}
		if (flag)//L1有且L2無
		{
			Position TmpCell = (PtrToPoly)malloc(sizeof(struct polynomial));
			TmpCell->coef = p->coef;
			TmpCell->power = p->power;
			w->next = TmpCell;
			w = w->next;	
		}
		p = p->next;
	}
	//尋找L2有,L1無的單項
	Position q2 = L2->next;
	while (q2 != NULL)
	{
		bool flag = true;//判斷是否L2有,L1無(此情況為true)
		Position p2 = L1->next;
		while (p2 != NULL)
		{
			if (q2->power == p2->power)
				flag = false;
			p2 = p2->next;
		}
		if (flag)//L2有且L1無
		{
			Position TmpCell = (PtrToPoly)malloc(sizeof(struct polynomial));
			TmpCell->coef = q2->coef;
			TmpCell->power = q2->power;
			w->next = TmpCell;
			w = w->next;
		}
		q2 = q2->next;
	}
	w->next = NULL;
}


//多項式相減,L1和L2是需相加的多項式,L3是兩個多項式相加後的結果
void Subpoly(List& L1, List& L2, List& L3)
{
	Position p;//定義一個工作指標
	p = L1->next;//將工作指標指向頭結點
	Position w = L3;
	//兩個while迴圈查詢L2中與L1相同指數的單項式,但不包括L2有L1沒有的情況
	while (p != NULL)
	{
		Position q;
		q = L2->next;
		bool flag = true;//判斷是否有L1有,L2無的情況(true為此情況)
		while (q != NULL)
		{
			if (p->power == q->power)
			{
				flag = false;
				Position TmpCell = (PtrToPoly)malloc(sizeof(struct polynomial));
				TmpCell->coef = p->coef - q->coef;
				TmpCell->power = q->power;
				w->next = TmpCell;
				w = w->next;
				break;
			}
			q = q->next;
		}
		if (flag)//L1有且L2無
		{
			Position TmpCell = (PtrToPoly)malloc(sizeof(struct polynomial));
			TmpCell->coef = p->coef;
			TmpCell->power = p->power;
			w->next = TmpCell;
			w = w->next;
		}
		p = p->next;
	}
	//尋找L2有,L1無的單項
	Position q2 = L2->next;
	while (q2 != NULL)
	{
		bool flag = true;//判斷是否L2有,L1無(此情況為true)
		Position p2 = L1->next;
		while (p2 != NULL)
		{
			if (q2->power == p2->power)
				flag = false;
			p2 = p2->next;
		}
		if (flag)//L2有且L1無
		{
			Position TmpCell = (PtrToPoly)malloc(sizeof(struct polynomial));
			TmpCell->coef = 0 - q2->coef;
			TmpCell->power = q2->power;
			w->next = TmpCell;
			w = w->next;
		}
		q2 = q2->next;
	}
	w->next = NULL;
}


void Copypoly(List& L3, List& L4);
//多項式相乘
void Multipoly(List& L1, List& L2, List& L3)
{
	Position p = L1->next;//L1的工作指標,可指向L3的任意地址
	Position w = L3;//L3的工作指標,可指向L3的任意地址,用於新增
	Position r = L3->next;
	while (p != NULL)
	{
		Position q = L2->next;
		while (q != NULL)
		{
			bool flag = true;//判斷是否有同類項,true為無同類項,false表示有同類項
			Position TmpCell = (PtrToPoly)malloc(sizeof(struct polynomial));		
			TmpCell->coef = p->coef * q->coef;
			TmpCell->power = p->power + q->power;
			while (r != NULL)//合併同類項(fail to make it!!!!)
			{
				//cout << "1" << endl;
				if (TmpCell->power == r->power)
				{
					flag = false;
					r->coef = r->coef + TmpCell->coef;
					break;
				}
				r = r->next;
			}
			if (flag)
			{
				w->next = TmpCell;
				w = w->next;
				w->next = NULL;
			}
			q = q->next;
		}
		p = p->next;
	}
}


//簡單微分操作
void Diffpoly(List& L)
{
	List p = L->next;
	while (p)
	{
		if (p->power != 0)
		{
			p->coef = p->coef * p->power;
			p->power--;
			p = p->next;
		}
		else
			p = p->next;
	}
}



//輸入多項式函式
void input(List& L)
{
	int N;
	Position p = L;
	cout << "請輸入項數:";
	cin >> N;
	for (int i = 1; i < N+1; i++)//頭插法輸入
	{
		Position TmpCell = (PtrToPoly)malloc(sizeof(struct polynomial));
		cout << "請輸入第" << i << "項的係數:";
		cin >> TmpCell->coef;
		cout << "請輸入第" << i << "項的次數:";
		cin >> TmpCell->power;
		p->next = TmpCell;
		p = p->next;
		cout << endl;
	}
	p->next = NULL;
}

//顯示多項式
void Showpoly(List& L)
{
	Position p;
	p = L->next;
	for (int i = 1; p != NULL; i++)
	{
		if (p->coef != 0)
		{
			cout << "第" << i << "項係數為:" << p->coef << endl;
			cout << "第" << i << "項指數為:" << p->power << endl;
			cout << endl;
			if (p->next != NULL)
				p = p->next;
			else
				break;
		}
		else
		{
			i--;
			if (p->next != NULL)
				p = p->next;
			else
				break;
		}
	}
}

int main()
{
	List L1;//多項式1
	L1 = (PtrToPoly)malloc(sizeof(struct polynomial));
	if (L1 == NULL)
		cout << "分配記憶體不成功" << endl;
	L1->next = NULL;

	List L2;//多項式2
	L2 = (PtrToPoly)malloc(sizeof(struct polynomial));
	L2->next = NULL;

	List L3;//結果多項式
	L3 = (PtrToPoly)malloc(sizeof(struct polynomial));
	L3->next = NULL;

	cout << "請輸入第一個多項式:" << endl;
	input(L1);
	//system("cls");
	cout << endl << "--------顯示L1---------" << endl;
	Showpoly(L1);
	cout << "請輸入第二個多項式:" << endl;
	input(L2);
	cout << endl << "--------顯示L2---------" << endl;
	Showpoly(L2);
	
	//加法測試
	Addpoly(L1, L2, L3);
	cout << endl << "--------加法·顯示L3---------" << endl;
	Showpoly(L3);
	free(L3);

	//減法測試
	L3 = (PtrToPoly)malloc(sizeof(struct polynomial));
	L3->next = NULL;
	Subpoly(L1, L2, L3);
	cout << endl << "--------減法·顯示L3---------" << endl;
	Showpoly(L3);
	free(L3);

	//乘法測試
	L3 = (PtrToPoly)malloc(sizeof(struct polynomial));
	L3->next = NULL;
	Multipoly(L1, L2, L3);
	cout << endl << "--------乘法·顯示L3---------" << endl;
	Showpoly(L3);

	//微分測試
	Diffpoly(L1);
	cout << endl << "--------微分·顯示L3---------" << endl;
	Showpoly(L1);

	return 0;
}

相關文章