一元多项式求和的两种实现(数组和链表)

一元多项式求和一般都是有两种实现方式,小编下面就附上我对这两种方式的理解和实现代码。

1.数组实现。这种方式实现一元多项式加法,是把数组下标当做一元多项式的指数,在数组中存放系数。这种方式在实现一元多项是加法是通过遍历数组下标来获取指数信息,通过指数信息将系数进行相加。利用数组在实现一元多项式加法,优点是操作简单,运算处理速度快,缺点是占用的内存大(此处内存大是指一开始所申请的内存计算机无法分配较大的连续内存)。

2链表实现。利用链表来实现一元多项式加法时,链表中要有指针域,两个数据域,一个存放指数,一个存放系数,它的优点是节省计算机内存(在指数a与一元多项式实际项数x x>(a+1)/3)(绝对内存)时,但操作复杂,计算机执行速度慢。

//头文件Array1
#pragma once
# include 
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 
using namespace std;
template < class  DataByte >
struct Node
{
	DataByte coef;
	int exp;
	Node* next;

};
template 
class Linklist {
	//操作链表的方法
public:
	Linklist();
	Linklist(DataByte a[], int n);
	void printList();
private:
	Node* first;

};



//头文件linklist1
#pragma once
# include 
# include "Linklist.h"
using namespace std;
template 
Linklist::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 
Linklist::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
Linklist::printList() {
	p = first->next;
	while (p != null) {
		cout << p->exp << " " << p->coef << " ";
		p = p->next;
	}
}
template
void Linklistadd(Linklist A, Linklist B) {
	Node*pre = A.first;
	Node*p = pre->next;
	Node*qre = B.first;
	Node*q = qre->next;
	while (p != null&&q != null) {
		if (p->exp < q->exp) {
			pre = p;
			p = p->next;
		}
		if (p->exp > q->exp) {
			Node*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
# 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 A;
		cout << "输入第二个一元多项式";
		Linklist B;
		Linklistadd(A, B);
		A.printList();

	}
	return 0;
}

 

你可能感兴趣的:(杂七杂八)