一元稀疏多项式的运算

一元稀疏多项式的运算

1.问题描述:设有两个带头指针的单链表表示两个一元稀疏多项式A、B,实现两个一元稀疏多项式的处理。

2.实现要求:
(1)输入并建立多项式;(最好通过文件输入,多项式最好不少于3项)
(2)输出多项式,输出形式为整数序列:n,c1,e1,C2,ez…cn,en,其中n是多项式的项数,ci,e;分别为第i项的系数和指数。序列按指数降序排列;(注意系数为0的项需要从链表中删除)
(3)多项式A和B相加,建立多项式A+B,输出相加的多项式;
(4)多项式A和B相减,建立多项式A-B,输出相减的多项式;
(5)多项式A和B相乘,建立多项式AxB,输出相乘的多项式;
(6)设计一个菜单,至少具有上述操作要求的基本功能。

#include
#include
typedef struct node
{
	float coef;   //系数 
	int expn;     //指数 
	struct node *next;
}PolyNode;      //多项式节点 polynomial node 
typedef PolyNode* Polynomial;
Polynomial createPolynomial()   //创建多项式 
{
	PolyNode *p, *q, *head = (PolyNode *)malloc(sizeof(PolyNode));   //头节点 
	head->next = NULL;
	float coef;
	int expn;
	while (scanf("%f %d", &coef, &expn) && coef)    // 默认,按指数递减排列 
	{
		if (head->next)
		{
			p = head;
			while (p->next && expn < p->next->expn)
				p = p->next;
			if (p->next)
			{
				if (expn == p->next->expn)  //有相同指数的直接把系数加到原多项式 
				{
					p->next->coef += coef;
					//若是相加后系数为0,则舍弃该节点 
					if (p->next->coef > -0.000001 && p->next->coef < 0.000001)
					{
						q = p->next;
						p->next = q->next;
						free(q);
					}
				}
				else
				{
					q = (PolyNode*)malloc(sizeof(PolyNode));
					q->coef = coef;
					q->expn = expn;
					q->next = p->next;
					p->next = q;
				}
			}
			else
			{
				p->next = (PolyNode*)malloc(sizeof(PolyNode));
				p = p->next;
				p->coef = coef;
				p->expn = expn;
				p->next = NULL;
			}
		}
		else
		{
			head->next = (PolyNode*)malloc(sizeof(PolyNode));
			head->next->coef = coef;
			head->next->expn = expn;
			head->next->next = NULL;
		}
	}
	return head;
}
//多项式与指定单项式相乘,该单项式为 coefx^expn 
Polynomial multiply(Polynomial poly, float coef, int expn)
{
	PolyNode *p, *q, *Poly = (PolyNode*)malloc(sizeof(PolyNode));
	p = Poly;
	q = poly->next;
	while (q)
	{
		p->next = (PolyNode*)malloc(sizeof(PolyNode));
		p = p->next;
		p->coef = (q->coef*coef);
		p->expn = (q->expn + expn);
		q = q->next;
	}
	p->next = NULL;
	return Poly;
}
void add(Polynomial poly1, Polynomial poly2)   //把 poly2 加到 poly1 上
{
	PolyNode *p, *q, *r;
	r = poly1;
	p = poly1->next;  //指向第一个节点
	q = poly2->next;
	poly2->next = NULL;
	while (p && q)
	{
		if (p->expn > q->expn)
		{
			r->next = p;
			p = p->next;
			r = r->next;
		}
		else if (p->expn < q->expn)
		{
			r->next = q;
			q = q->next;
			r = r->next;
		}
		else
		{
			PolyNode *t;
			p->coef += q->coef;
			if (!(p->coef > -0.000001 && p->coef < 0.000001)) //系数不为0
			{
				r->next = p;
				r = r->next;
				p = p->next;
			}
			else
			{
				t = p;
				p = p->next;
				free(t);
			}
			t = q;
			q = q->next;
			free(t);
		}
	}
	if (p)
		r->next = p;
	if (q)
		r->next = q;
}
//多项式减法 poly1-poly2形成一个新的多项式
Polynomial polySubtract(Polynomial poly1, Polynomial poly2)
{
	//把poly2的系数取相反数,形成一个新的多项式
	Polynomial poly = (PolyNode*)malloc(sizeof(PolyNode)); //构造头节点
	PolyNode *p, *q;
	p = poly;
	q = poly2->next;
	while (q)
	{
		p->next = (PolyNode*)malloc(sizeof(PolyNode));
		p = p->next;
		p->coef = -(q->coef);  //系数取反
		p->expn = q->expn;
		q = q->next;
	}
	p->next = NULL;
	add(poly, poly1);  //利用加法
	return poly;
}
//多项式相加 poly1+poly2形成一个新的多项式 
Polynomial polyAdd(Polynomial poly1, Polynomial poly2)
{
	Polynomial poly = (PolyNode*)malloc(sizeof(PolyNode));  //和多项式的头节点 
	poly->next = NULL;
	PolyNode *p, *q, *r;
	r = poly;
	p = poly1->next;
	q = poly2->next;
	while (p&&q)
	{
		if (p->expn > q->expn)
		{
			r->next = (PolyNode*)malloc(sizeof(PolyNode));
			r = r->next;
			r->coef = p->coef;
			r->expn = p->expn;
			p = p->next;
		}
		else if (p->expn < q->expn)
		{
			r->next = (PolyNode*)malloc(sizeof(PolyNode));
			r = r->next;
			r->coef = q->coef;
			r->expn = q->expn;
			q = q->next;
		}
		else
		{
			float m = p->coef + q->coef;
			if (!(m > -0.000001 && m < 0.000001))
			{
				r->next = (PolyNode*)malloc(sizeof(PolyNode));
				r = r->next;
				r->coef = m;
				r->expn = p->expn;
			}
			q = q->next;
			p = p->next;
		}
	}
	while (p)
	{
		r->next = (PolyNode*)malloc(sizeof(PolyNode));
		r = r->next;
		r->coef = p->coef;
		r->expn = p->expn;
		p = p->next;
	}
	while (q)
	{
		r->next = (PolyNode*)malloc(sizeof(PolyNode));
		r = r->next;
		r->coef = q->coef;
		r->expn = q->expn;
		q = q->next;
	}
	r->next = NULL;
	return poly;
}
Polynomial polyMultiply(Polynomial poly1, Polynomial poly2)   //多项式相乘  
{
	Polynomial poly = (PolyNode*)malloc(sizeof(PolyNode));  //创建多项式和的头节点 
	poly->next = NULL;
	PolyNode *p;
	p = poly2->next;
	while (p)
	{
		add(poly, multiply(poly1, p->coef, p->expn));
		p = p->next;
	}
	return poly;
}
void printPoly(Polynomial poly)    //打印多项式 
{
	if (poly && poly->next)
	{
		PolyNode *p = poly->next;  //p指向第一个节点
		while (p->next)
		{
			if(p->coef)
			{
			printf("%gx^%d", p->coef, p->expn);
		
		}
			p = p->next;
			if (p && (p->coef > 0))
				printf("+");
		}
		if (p->expn == 0 &&p->coef!=0)
			{
			printf("%g", p->coef);   //打印常数项 
			 
		}
		else if(p->coef!=0)
			{
			printf("%gx^%d", p->coef, p->expn);
		 
		}
		printf("\n");
	}
}
void clear(Polynomial poly)   //释放内存
{
	if (poly && poly->next)
	{
		PolyNode *p, *q;
		p = poly;
		while (p)
		{
			q = p->next;
			free(p);
			p = q;
		}
	}
	poly = NULL;
}
int main()
{
	printf("用链表实现多项式的加减法\n");
	Polynomial poly1, poly2, poly;
	printf("创建多项式一\n");
	poly1 = createPolynomial();
	printf("多项式一:\n");
	printPoly(poly1);
	printf("创建多项式二\n");
	poly2 = createPolynomial();
	printf("多项式二:\n");
	printPoly(poly2);
	printf("两多项式相加,和为:\n");
	poly = polyAdd(poly1, poly2);
	printPoly(poly);
	clear(poly);
	printf("两个多项式相乘,积为:\n");
	poly = polyMultiply(poly1, poly2);
	printPoly(poly);
	clear(poly);
	printf("两多项式相减,差为:\n");
	poly = polySubtract(poly1, poly2);
	printPoly(poly);
	clear(poly1);
	clear(poly2);
	clear(poly);
	system("pause");
	return 0;
}

你可能感兴趣的:(算法,数据结构,算法)