多项式求和C语言实现

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_38250032/article/details/79336336

思路:用单链表来存储多形式,每一个结点存储多项式中的一项。用两个单链表分别存储两个多项式,结果再用新建一个单链表来存储。

数据结构:

typedef struct Node *PNode;		                            //节点指针类型
struct Node{
	int exp;						    //存储每一项的指数
	float coe;						    //存储每一项的系数
	PNode next;						    //指向下一项(下一个节点)
};

函数:

创建多项式:
PNode createpoly()										
{
	PNode h = NULL, p = NULL, q;                        //h为指向链表第一个结点的指针(即指向链表的指针)
	int e;
	float c;                                            //e,c相当于一个中介,通过e,c向结点中存储数据,
	printf("请输入系数和指数(指数从大到小):\n");
	scanf("%f,%d",&c,&e);
	while (e != 0 || c != 0)
	{
		p = (PNode)malloc(sizeof(struct Node));      //先创建一个结点
		p->coef = c;                                 
		p->exp = e;
		p->next = NULL;                               //指向下一个结点的指针先定义为空
		if (h == NULL)                                
			h = p;                                
		else
			q->next = p;
		q = p;
		printf("请输入系数和指数:\n");
		scanf("%f,%d",&c,&e);
	}
	return h;                                            //返回头指针
}

求和函数:

PNode addpoly(PNode h1, PNode h2)
{
	PNode p, r = NULL, s1, s2, s = NULL;
	float c;
	int e;
	s1 = h1;
	s2 = h2;
	while (s1 != NULL&&s2 != NULL)
	{
		if (s1->exp == s2->exp)
		{
			c = s1->coef+s2->coef;
			e = s1->exp;
			s1 = s1->next;
			s2 = s2->next;
		}
		else if (s1->exp > s2->exp)
		{
			c = s1->coef;
			e = s1->exp;
			s1 = s1->next;
		}
		else
		{
			c = s2->coef;
			e = s2->exp;
			s2 = s2->next;
		}
		if (c != 0)
		{
			p = (PNode)malloc(sizeof(struct Node));
			p->coef = c;
			p->exp = e;
			p->next = NULL;
			if (s == NULL)
				s = p;
			else
				r->next = p;
			r = p;
		}
	}
	while (s1 != NULL)
	{
		c = s1->coef;
		e = s1->exp;
		s1 = s1->next;
		if (c != 0)
		{
			p = (PNode)malloc(sizeof(struct Node));
			p->coef = c;
			p->exp = e;
			p->next = NULL;
			if (s == NULL)
				s = p;
			else
				r->next = p;
			r = p;
		}
	}
	while (s2 != NULL)
	{
		c = s2->coef;
		e = s2->exp;
		s2 = s2->next;
		if (c != 0)
		{
			p = (PNode)malloc(sizeof(struct Node));
			p->coef = c;
			p->exp = e;
			p->next = NULL;
			if (s == NULL)
				s = p;
			else
				r->next = p;
			r = p;
		}
	}
	return s;
}

打印多项式:

void  outputpoly(PNode h)
{
	PNode p;
	p = h;
	while (p != NULL)
	{
		if (p->exp == 0)
			printf("%.2f",p->coef);
		else
		{
			printf("%.2fx^%d",p->coef,p->exp);
		}
		p = p->next;
		if (p != NULL)
			printf("+");
	}
	printf("\n");
}

删除:

void deletepoly(PNode h)
{
	PNode p, r = h;
	while (r != NULL)
	{
		p = r->next;
		free(r);
		r = p;
	}
}

完整程序:

#include<stdio.h>
#include<stdlib.h>
struct Node;
typedef struct Node *PNode;
struct Node{
	float coef;
	int exp;
	PNode next;
};

PNode createpoly()										
{
	PNode h = NULL, p = NULL, q;
	int e;
	float c;
	printf("请输入系数和指数(指数从大到小):\n");
	scanf("%f,%d",&c,&e);
	while (e != 0 || c != 0)
	{
		p = (PNode)malloc(sizeof(struct Node));
		p->coef = c;
		p->exp = e;
		p->next = NULL;
		if (h == NULL)
			h = p;
		else
			q->next = p;
		q = p;
		printf("请输入系数和指数:\n");
		scanf("%f,%d",&c,&e);
	}
	return h;
}

void  outputpoly(PNode h)
{
	PNode p;
	p = h;
	while (p != NULL)
	{
		if (p->exp == 0)
			printf("%.2f",p->coef);
		else
		{
			printf("%.2fx^%d",p->coef,p->exp);
		}
		p = p->next;
		if (p != NULL)
			printf("+");
	}
	printf("\n");
}

PNode addpoly(PNode h1, PNode h2)
{
	PNode p, r = NULL, s1, s2, s = NULL;
	float c;
	int e;
	s1 = h1;
	s2 = h2;
	while (s1 != NULL&&s2 != NULL)
	{
		if (s1->exp == s2->exp)
		{
			c = s1->coef+s2->coef;
			e = s1->exp;
			s1 = s1->next;
			s2 = s2->next;
		}
		else if (s1->exp > s2->exp)
		{
			c = s1->coef;
			e = s1->exp;
			s1 = s1->next;
		}
		else
		{
			c = s2->coef;
			e = s2->exp;
			s2 = s2->next;
		}
		if (c != 0)
		{
			p = (PNode)malloc(sizeof(struct Node));
			p->coef = c;
			p->exp = e;
			p->next = NULL;
			if (s == NULL)
				s = p;
			else
				r->next = p;
			r = p;
		}
	}
	while (s1 != NULL)
	{
		c = s1->coef;
		e = s1->exp;
		s1 = s1->next;
		if (c != 0)
		{
			p = (PNode)malloc(sizeof(struct Node));
			p->coef = c;
			p->exp = e;
			p->next = NULL;
			if (s == NULL)
				s = p;
			else
				r->next = p;
			r = p;
		}
	}
	while (s2 != NULL)
	{
		c = s2->coef;
		e = s2->exp;
		s2 = s2->next;
		if (c != 0)
		{
			p = (PNode)malloc(sizeof(struct Node));
			p->coef = c;
			p->exp = e;
			p->next = NULL;
			if (s == NULL)
				s = p;
			else
				r->next = p;
			r = p;
		}
	}
	return s;
}

void deletepoly(PNode h)
{
	PNode p, r = h;
	while (r != NULL)
	{
		p = r->next;
		free(r);
		r = p;
	}
}

void main()
{
	PNode head1, head2, head;
	printf("创建第一个多项式:\n");
	head1 = createpoly();
	printf("创建第二个多项式:\n");
	head2 = createpoly();
	printf("将两个多项式相加:\n");
	head =addpoly(head1, head2);
	outputpoly(head);
	deletepoly(head);
}

运行结果:

展开阅读全文

没有更多推荐了,返回首页