1. 程式人生 > >一元多項式求和的兩種實現(陣列和連結串列)

一元多項式求和的兩種實現(陣列和連結串列)

一元多項式求和一般都是有兩種實現方式,小編下面就附上我對這兩種方式的理解和實現程式碼。

1.陣列實現。這種方式實現一元多項式加法,是把陣列下標當做一元多項式的指數,在陣列中存放係數。這種方式在實現一元多項是加法是通過遍歷陣列下標來獲取指數資訊,通過指數資訊將係數進行相加。利用陣列在實現一元多項式加法,優點是操作簡單,運算處理速度快,缺點是佔用的記憶體大(此處記憶體大是指一開始所申請的記憶體計算機無法分配較大的連續記憶體)。

2連結串列實現。利用連結串列來實現一元多項式加法時,連結串列中要有指標域,兩個資料域,一個存放指數,一個存放係數,它的優點是節省計算機記憶體(在指數a與一元多項式實際項數x x>(a+1)/3)(絕對記憶體)時,但操作複雜,計算機執行速度慢。

//標頭檔案Array1
#pragma once
# include <iostream>
using namespace std;
void arraylist() {
	int m;
	int n;
	int alength1 = 0;
	int blength1 = 0;
	cout << "輸入第一個一元多項式的最大指數:";
	cin >> m;
	cout << endl;
	cout << "輸入第一個一元多項式的最大指數:";
	cin >> n;
	cout << endl;
	int alength = m + 1;
	int blength = n + 1;
	int *a = new int[alength] {0};
	int *b = new int[blength] {0};
	cout << "輸入第一個一元多項式:(先輸入指數再輸入係數)" << endl;

	while (1) {
		//宣告指數和係數
		//位置異常處理沒寫
		int exp = 0;
		int coef = 0;
		cin >> exp;
		if (exp == -1) {
			break;
		}
		else
		{
			cin >> coef;
			a[exp] = coef;
		}
	}
	cout << "輸入第二個一元多項式:(先輸入指數再輸入係數)" << endl;
	while (1) {
		//宣告指數和係數
		//位置異常沒寫
		int exp = 0;
		int coef = 0;
		cin >> exp;
		if (exp == -1)
			break;
		else {
			cin >> coef;
			b[exp] = coef;
		}

	}
	//獲取一元多項式實際項數
	//alength1 = length1(a, alength);
	//blength1 = length1(b, blength);
	//陣列實現的一元多項式加法函式呼叫
	arraylistadd(a, b, alength, blength);
	delete[]a;
	delete[]b;
	}
//獲取一元多項式項數的函式
int length1(int a[], int length) {
	int length1 = 0;
	for (int i = 0; i < length; i++) {
		if (a[i] != 0)
			length1++;
	}
	return length1;
}


void arraylistadd(int a[], int b[],int alength,int blength) {
	if (alength >= blength) {
		for (int i = 0; i < blength; i++) {
			a[i] = a[i] + b[i];
		}
		cout << "輸出結果(指數/係數):";
		for (int i = 0; i < alength; i++) {
			if(a[i]!=0)
			cout << i << " " << a[i] << " ";
		}
	}
	else {
		for (int i = 0; i < alength; i++) {
			b[i] = a[i] + b[i];
		}
		cout << "輸出結果(指數/係數):";
		for (int i = 0; i < blength; i++) {
			if(b[i]!=0)
			cout << i << " " << b[i] << " ";
		}
	}
}




//標頭檔案Linklist
#pragma once
# include <iostream>
using namespace std;
template < class  DataByte >
struct Node
{
	DataByte coef;
	int exp;
	Node<DataByte>* next;

};
template <class DataByte>
class Linklist {
	//操作連結串列的方法
public:
	Linklist();
	Linklist(DataByte a[], int n);
	void printList();
private:
	Node<DataByte>* first;

};



//標頭檔案linklist1
#pragma once
# include <iostream>
# include "Linklist.h"
using namespace std;
template <class DataByte>
Linklist<DataByte>::Linklist() {
	first = new Node;
	r = first;
	cout << "先輸入指數 再輸入係數,指數為-1時結束輸入:" << endl; 
	while (1) {
		s = new Node;
		int i;
		cin >> i;
		if (i ==-1 ) {
			break;
		}
		else {
			s->exp = i;
			int j;
			cin >> j;
			s->coef = j;
			r->next = s;
			r = s;
		}
	}
	r->next = null;
}
template <class  DataByte>
Linklist<DataByte>::Linklist(DataType a[],DataByte b[], int n) {
	first = new Node;
	r = first;
	for (int i = 0; i < n; i++) {
		s = new Node;
		s->exp = b[i];
		s->coef = a[i];
		r->next = s;
		r = s;
	}
	r->next = null;
}
template<class DataByte>
Linklist<DataByte>::printList() {
	p = first->next;
	while (p != null) {
		cout << p->exp << " " << p->coef << " ";
		p = p->next;
	}
}
template<class DataByte>
void Linklistadd(Linklist<DataByte> A, Linklist<DataByte> B) {
	Node<DataByte>*pre = A.first;
	Node<DataByte>*p = pre->next;
	Node<DataByte>*qre = B.first;
	Node<DataByte>*q = qre->next;
	while (p != null&&q != null) {
		if (p->exp < q->exp) {
			pre = p;
			p = p->next;
		}
		if (p->exp > q->exp) {
			Node<DataByte>*v = p->next;
			pre->next = q;
			q->next = p;
			q = v;
		}
		if (p->exp = q->exp) {
			p->coef = p->coef + q->coef;
			if (p->coef == 0) {
				pre->next = p->next;
				delete p;
				p = pre->next;
			}
			else {
				pre = p;
				p = p->next;
			}
			qre->next = q->next;
			delete q;
			q = qre->next;
		}
		if (q != null)
			pre->next = q;
		delete B.first;
	}

}






# include<iostream>
# include"Linklist.h"
# include "Linklist1.h"
# include "Array1.h"
using namespace std;
//主函式
int main() {
	int m;
	 cout<< "輸入m:(m=1基於陣列實現一元多項式加法 m=0 基於連結串列實現一元多項式加法)";
	cin >> m;
	if (m==1) {
		arraylist();
	}
	else {
		cout << "輸入第一個一元多項式";
		Linklist<int> A;
		cout << "輸入第二個一元多項式";
		Linklist<int> B;
		Linklistadd(A, B);
		A.printList();

	}
	return 0;
}