基于c++数据结构 用线性表实现多项式相加、相乘、微分

题目:用线性表实现多项式相加,相乘和微分。其中相加是基本要求,一定要实现。相乘和微分是附加。

报告正文:
边缘情况考虑:系数为零、指数为零时不输出x、相乘时系数为零如何处理、输出时指针是否为空的判断等

#include<iostream>
#include<cstdlib>//生成多项式随机系数
#include<ctime>
const int MM = 10;//全局变量定义多项式个数
using namespace std;
typedef struct poly {//定义结点
	float coef;
	int expn;
	struct poly *next;
}poly, *polypointer;
polypointer creatpoly( ) {//多项式生成
	polypointer head;
	polypointer pnew;
	polypointer ptemp;	
	head = (polypointer)malloc(sizeof(poly));
	head->coef = 0;
	head->expn = 0;
	head->next = NULL;
	ptemp = head;
	for (int i = 0; i < MM; i++) {
		pnew = (polypointer)malloc(sizeof(poly));
		pnew->coef = rand() % 16;
		pnew->expn =MM-i-1;
		pnew->next = NULL;
		ptemp->next = pnew;
		ptemp = pnew;
	}

	return head;
}
void disp(polypointer ko) {
	ko = ko->next;
	if (ko->coef != 0)
		cout << ko->coef << "X^" << ko->expn ;
	ko = ko->next;
	while (ko != NULL) {//设置结束条件 防止越界
		if (ko->expn != 0)
			if (ko->next == NULL)//判断是否结束 结束则换行
				cout << " + " << ko->coef << "X^" << ko->expn << endl;
			else
				cout << " + " << ko->coef << "X^" << ko->expn;
		else if(ko->expn==0)//如果多项式系数为零 则只输出他的系数
			cout << " + " << ko->coef<<endl;
		ko = ko->next;
	}`在这里插入代码片`
}
int compare(int a, int b) {//比较系数大小的函数
	if (a == b)
		return 0;
	else
		return a > b ? 1 : -1;
}
void attach(float coef, int expn, polypointer& c) {
	polypointer temp = (polypointer)malloc(sizeof(poly));
	temp->coef=coef;
	temp->expn = expn;
	c->next = temp;
	c = c->next;
	
}
polypointer padd(polypointer a, polypointer b) {//多项式相加
	polypointer c,temp, rear;
	float sum;
	c = (polypointer)malloc(sizeof(poly));	
	c->coef = 0;
	c->expn = 0;
	temp= (polypointer)malloc(sizeof(poly));
	c->next =temp;
	temp->coef = 0;
	temp->expn = 0;
	rear=c->next;	//rear作为传递 往C中串指针
	a = a->next;
	b = b->next;
	while (a&&b) {
		switch (compare(a->expn, b->expn)) {
		case -1:
			attach(b->coef, b->expn, rear);
			b = b->next;
			break;
		case 0:
			sum = a->coef + b->coef;
			if (sum != 0) {
				attach(sum, a->expn, rear);
				a = a->next;
				b = b->next;
				break;

			}
		case 1:
			attach(a->coef, a->expn, rear);
			a = a->next;
			break;
		}
	}
	for (; a; a = a->next)
		attach(a->coef, a->expn, rear);
	for (; b; b->next)
		attach(b->coef, b->expn, rear);
	rear->next = NULL;	
	c = c->next;
	return c;
}
polypointer derivation(polypointer a) {//求微分函数
	polypointer temp, tal, ptem;
	float co;
	int ex;	
	tal= (polypointer)malloc(sizeof(poly));
	tal->coef = 0;
	tal->expn = 0;
	tal->next = NULL;
	temp = tal;
	a = a->next;
	
		while (a != NULL) {//判断结束条件
			co = a->coef*(a->expn);
			ex = a->expn - 1;
			ptem = (polypointer)malloc(sizeof(poly));
			if (ex != -1) {//判断指数是否为零
				ptem->coef = co;
				ptem->expn = ex;
				ptem->next = NULL;
				temp->next = ptem;
				temp = ptem;
				a = a->next;
			}
			else {//此时其指数为零,求导项数为零 跳过之
				a = a->next;
			}
		}
	
	return tal;//返回头指针

}

polypointer mult(polypointer a, polypointer b) {//多项式相乘函数
	polypointer temp, temp1;
	polypointer ptem, tre, head, daib;	
	a = a->next;
	b = b->next;
	daib = b;//记录b指针的第一项 防止之后相乘 遍历后丢失
	head = (polypointer)malloc(sizeof(poly));
	head->coef = 0;
	head->expn = 0;
	head->next = NULL;
	temp = head;
	temp1 = temp;
	float co;
	int ex;
	int num = 0;
	
		while (b != NULL) {//判断b是否遍历完毕

			ptem = (polypointer)malloc(sizeof(poly));
			co = a->coef * b->coef;
			ex = a->expn + b->expn;
			if (co != 0) {//判断相乘后系数值是否为零
				ptem->coef = co;
				ptem->expn = ex;
				ptem->next = NULL;
				temp->next = ptem;
				temp = ptem;
				num++;//用以a第一项与b相乘,得到一组链表,后面的的相乘多项式插入到这里
			}
			b = b->next;
		}
	
	a = a->next;
	b = daib;//b重新回到指针第一项,刚才已经遍历完毕
	while (a != NULL) {
		b = daib;
		while (b != NULL) {
			ptem = (polypointer)malloc(sizeof(poly));
			co = a->coef * b->coef;
			ex = a->expn + b->expn;
			if (co != 0) {//判断该节点系数是否为零,不为零则便利数组比较指数大小实现插入
				ptem->coef = co;
				ptem->expn = ex;
				ptem->next = NULL;
				tre = temp1;//设置一个指针 保留temp1的前一位,便于指针插入
				temp1 = temp1->next;
				for (int i = 0; i <= MM*MM; i++) {		//实现结点插入	
                    
					if (temp1->expn < ptem->expn) {//当temp1小于ptem时,ptem插入到temp1的前一位暨tre
						ptem->next = tre->next;
						tre->next = ptem;
						tre = ptem;
						break;
					}
					if (temp1->expn == ptem->expn) {//相等时 仅系数相加
						temp1->coef = temp1->coef + ptem->coef;
						break;
					}
			
					tre = temp1;
					temp1 = temp1->next;
					if (temp1 == NULL) {//如果遍历到最后,则把该项插入到指针尾部
						tre->next = ptem;
						break;
					}
				}
				
			}
			temp1 = (polypointer)malloc(sizeof(poly));
			tre = (polypointer)malloc(sizeof(poly));
			temp1 = head;
			b = b->next;
		}
		a = a->next;
		
	}
	return head;
}
int main()
{
	polypointer a, b, add=new poly,de=new poly,mul=new poly;
	a = creatpoly();
	cout << "生成的第1组多项式: ";
	disp(a);
	b = creatpoly();
	cout << "生成的第2组多项式: ";
	disp(b);	
	add = padd(a, b);
	cout << "多项式相加= ";
	disp(add);
	de = derivation(add);//
	cout << "已求和多项式求导:";
	disp(de);
	mul = mult(a, b);
	cout << "多项式相乘: ";
	disp(mul);
	system("pause");
	return 0;
}

在这里插入代码片
  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值