栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

两个一元多项式的运算(封面与内容无关)

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

两个一元多项式的运算(封面与内容无关)

《数据结构 (C语言版)》洪国运 编著
『上海交通大学出版社』
线性表
实训2-4  一元多项式的运算

这里结点写三部分:

1.系数

2.指数

3.结点指针

\懒得写注释了,主要是也没人看

//一元多项式的运算
#include 
#include 

//定义结点类型
typedef struct Node
{
	float coef;
	int exp;
	struct Node* next;
}Node;

//建立链表
Node* create(int n)
{
	Node* head, * s, * rear;
	int i;
	head = (Node*)malloc(sizeof(Node));
	head->next = NULL;
	rear = head;
	for (i = 1; i <= n; i++)
	{
		s = (Node*)malloc(sizeof(Node));
		printf("请输入第%d项的系数和指数:", i);
		scanf("%f %d", &s->coef, &s->exp);
		rear->next = s;
		rear = s;
	}
	rear->next = NULL;
	return head;
}

//输出链表中的数据元素
void display(Node* head)
{
	Node* p = NULL;
	p = head->next;
	while (p)
	{
		printf("(%f,%d) ", p->coef, p->exp);
		p = p->next;
	}
	printf("n");
}

//两个多项式相加,返回多项式链表的头指针
Node* addPolyn(Node* head1, Node* head2)
{
	Node* p, * q, * head, * rear, * s;
	int temp;
	float sum;
	p = head1->next;
	q = head2->next;
	head = (Node*)malloc(sizeof(Node));
	rear = head;

	while (p && q)
	{
		if (p->exp == q->exp)
			temp = 0;
		else if (p->exp < q->exp)
			temp = 1;
		else
			temp = -1;

		switch (temp)
		{
		case 0:
			sum = p->coef + q->coef;
			if (sum != 0.0)
			{
				s = (Node*)malloc(sizeof(Node));
				s->coef = sum;
				s->exp = p->exp;
				rear->next = s;
				rear = s;
			}
			p = p->next;
			q = q->next;
			break;
		case 1:
			s = (Node*)malloc(sizeof(Node));
			s->coef = p->coef;
			s->exp = p->exp;
			rear->next = s;
			rear = s;
			p = p->next;
			break;
		case -1:
			s = (Node*)malloc(sizeof(Node));
			s->coef = q->coef;
			s->exp = q->exp;
			rear->next = s;
			rear = s;
			q = q->next;
			break;
		}
	}
	while (p)
	{
		s = (Node*)malloc(sizeof(Node));
		s->coef = p->coef;
		s->exp = p->exp;
		rear->next = s;
		rear = s;
		p = p->next;
	}
	while (q)
	{
		s = (Node*)malloc(sizeof(Node));
		s->coef = q->coef;
		s->exp = q->exp;
		rear->next = s;
		rear = s;
		q = q->next;
	}
	rear->next = NULL;
	return head;
}

int main()
{
	Node* h1, * h2, * h;
	int m;
	printf("请输入第一个多项式的项数:");
	scanf("%d", &m);
	h1 = create(m);
	display(h1);
	printf("请输入第二个多项式的项数:");
	scanf("%d", &m);
	h2 = create(m);
	display(h2);
	printf("输出和多项式的系数和指数:");
	h = addPolyn(h1, h2);
	display(h);

	return 0;
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/312097.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号