数据结构 C++实现一元多项式运算(顺序存储结构实现)

实验目的

1、 了解线性表的特性,以及它在实际问题中的应用。
2、掌握线性表的顺序存储的实现方法以及它的基本操作,学会运用线性表来解决问题。

实验内容

用顺序存储结构实现一元多项式的加法、减法和乘法。具体要求为:用五个函数分别实现一元多项式的创建、输出、加法、减法和乘法;然后在主函数中调用这些函数实现这些功能的展示。

代码内容

#include<iostream>
using namespace std;
#define maxdegree 100	//多项式最大项数

//定义多项式结构体
typedef struct polynomial {
	int coefarray[maxdegree+1];	//多项式系数
	int highpower;	//最高项指数
}polynomial;

//创建多项式
void creatlist(polynomial* L) {
	int  n, cofe, index;	//项数,系数,指数
	L->highpower = 0;	//多项式指数初始化
	for (int i = 0; i <= maxdegree; i++)
		L->coefarray[i] = 0;	//多项式系数初始化
	cout << "输入多项式项数" << endl;
	cin >> n;
	for (int i = 1; i <= n; i++) {
		cout << "请输入第" <<i<<"项的系数和指数"<< endl;
		cin >> cofe >> index;
		if (L->highpower < index)
			L->highpower = index;	//更新最高项指数
		L->coefarray[index] = cofe;	//把对应的多项式系数存入
	}
}

//初始化多项式
void initialize(polynomial* L) {
	for (int i = 0; i <= maxdegree + 1; i++)
		L->coefarray[i] = 0;
	L->highpower = 0;
}

//输出多项式
void printout(polynomial *L) {
	if (L->coefarray[0] != 0)
		cout << L->coefarray[0] << "+";	//判断常数项是否为零
	for (int i = 1; i < L->highpower; i++) {
		if (L->coefarray[i] == 0) 
			;	//判断该项是否存在
		else {
			if (L->coefarray[i] < 0)
				cout << "\b";	//如果是负数删掉+
			cout << L->coefarray[i] << "x^" << i << "+";
		}
	}
	cout << L->coefarray[L->highpower] << "x^" << L->highpower ;	//单独输出最后一项
}

//多项式加法
void add_polynomial(polynomial *L1,polynomial *L2,polynomial *L3) {
	if (L1->highpower > L2->highpower)	//判断最高项指数
		L3->highpower = L1->highpower;
	else
		L3->highpower = L2->highpower;
	for (int i = 0; i <= L3->highpower; i++)
		L3->coefarray[i] = L1->coefarray[i] + L2->coefarray[i];
}

//多项式减法
void subtruct_polynomial(polynomial* L1, polynomial* L2, polynomial* L3) {
	if (L1->highpower > L2->highpower)	//判断最高项指数
		L3->highpower = L1->highpower;
	else
		L3->highpower = L2->highpower;
	for (int i = 0; i <= L3->highpower; i++)
		L3->coefarray[i] = L1->coefarray[i] - L2->coefarray[i];
}

//多项式乘法
void multiply_polynomial(polynomial* L1, polynomial* L2, polynomial* L3) {
	L3->highpower = L1->highpower + L2->highpower;	//同底数相乘等于底数不变幂数相加
	for (int i = 0; i <= L1->highpower; i++)
		for (int j = 0; j <= L2->highpower; j++)
			L3->coefarray[i + j] += L1->coefarray[i] * L2->coefarray[j];

}

//菜单函数
void display() {
	cout << "========================"<<endl;
	cout << "1.创建多项式" << endl;
	cout << "2.多项式加法" << endl;
	cout << "3.多项式减法" << endl;
	cout << "4.多项式乘法" << endl;
	cout << "0.退出" << endl;
	cout << "========================" << endl;
}

int main() {
	//为L1,L2,L3动态分布空间
	polynomial* L1 = new polynomial;
	polynomial* L2 = new polynomial;
	polynomial* L3 = new polynomial;
	int n;
	display();	//调用菜单函数
	while (true){
		cout << "请输入要执行的操作:" ;
		cin >> n;
		switch (n){
		case 1:
			creatlist(L1);	//调用创建多项式函数
			cout << "第一个多项式为:" ;
			printout(L1);
			cout << endl;
			creatlist(L2);
			cout << "第二个多项式为:";
			printout(L2);
			cout << endl;
			break;
		case 2:
			initialize(L3);	//调用初始化函数
			add_polynomial(L1, L2, L3);	//调用加法函数
			cout << "相加后结果为:" << endl;
			printout(L3);
			cout << endl;
			break;
		case 3:
			initialize(L3);
			subtruct_polynomial(L1, L2, L3);	//调用减法函数
			cout << "相减后结果为:" << endl;
			printout(L3);
			cout << endl;
			break;
		case 4:
			initialize(L3);
			multiply_polynomial(L1, L2, L3);	//调用乘法函数
			cout << "相乘后结果为:" << endl;
			printout(L3);
			cout << endl;
			break;
		case 0:
			exit(1);	//退出程序
		default:
			cout << "输入不合法,请重新输入!" << endl;
			cout << endl;
			break;
		}
	}
	delete L1, L2, L3;	//释放L1,L2,L3动态内存空间
	return 0;
}

运行结果

测试数据:
在这里插入图片描述

在这里插入图片描述

总结

代码中的算法很容易实现,只需要把两个数组对应的分量项进行加减乘运算即可,但顺序存储只适合稠密多项式,如果是系数多项式的话就很浪费空间和时间。

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

82年苏打

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值