多项式加法——C语言

cpp 复制代码
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>

typedef struct poly
{
	int coeff;
	int exp;
	struct poly* next;
}*link,Node;

void InputPoly(link head);
void Print(link head);
bool Insert(link head, int coeff, int exp);
void Combin2List(link heada, link headb, link headab);
void Clear(link head);

int main()
{
	link heada, headb;
	link headab;

	heada = (link)malloc(sizeof(Node));
	heada->next = NULL;
	headb = (link)malloc(sizeof(Node));
	headb->next = NULL;
	headab = (link)malloc(sizeof(Node));
	headab->next = NULL;

	printf("请输入第一个多项式的系数和指数,以(0,0)结束:\n");
	InputPoly(heada);
	printf("第一个");
	Print(heada);

	printf("请输入第二个多项式的系数和指数,以(0,0)结束:\n");
	InputPoly(headb);
	printf("第二个");
	Print(headb);

	Combin2List(heada, headb, headab);
	printf("合并为:");
	Print(headab);

	Clear(heada);
	Clear(headb);
	Clear(headab);

	return 0;
}

void InputPoly(link head)
{
	int coeff, exp;
	printf("请输入系数和指数(如:\"2 3\"表示2x^3):");
	scanf("%d%d", &coeff, &exp);
	while (coeff != 0 || exp != 0)
	{
		Insert(head, coeff, exp);
		printf("请输入系数和指数:");
		scanf("%d%d", &coeff, &exp);
	}
}

void Print(link head)
{
	link p = head->next;
	while (p != NULL)
	{
		if (p != head->next && p->coeff > 0)
			printf("+");
		if (p->coeff == -1 && p->exp != 0)
			printf("-");
		if ((p->coeff != 1 && p->coeff != -1) || p->exp == 0)
			printf("%d", p->coeff);
		if (p->exp != 1 && p->exp != 0)
			printf("x^%d", p->exp);
		if (p->exp == 1)
			printf("x");
		p = p->next;
	}
	puts("");
}

bool Insert(link head, int coeff, int exp)
{
	link node;
	link q, p;
	q = head;
	p = head->next;

	node = (link)malloc(sizeof(Node));
	node->coeff = coeff;
	node->exp = exp;
	node->next = NULL;

	if (head->next == NULL)
	{
		head->next = node;
	}
	else
	{
		while (p != NULL)
		{
			if (node->exp > p->exp)
			{
				node->next = p;
				q->next = node;
				break;
			}
			else if(node->exp < p->exp)
			{
				q = p;
				p = p->next;
			}
			else
			{
				p->coeff = p->coeff + node->coeff;
				free(node);
				break;
			}
		}
		if (p == NULL)
			q->next = node;
	}
	return true;
}

void Combin2List(link heada, link headb, link headab)
{
	link pa, pb;
	pa = heada->next;
	pb = headb->next;

	while (pa != NULL && pb != NULL)
	{
		if (pa->exp > pb->exp)
		{
			Insert(headab, pa->coeff, pa->exp);
			pa = pa->next;
		}
		else if (pa->exp == pb->exp)
		{
			Insert(headab, pa->coeff + pb->coeff, pb->exp);
			pa = pa->next;
			pb = pb->next;
		}
		else
		{
			Insert(headab, pb->coeff, pb->exp);
			pb = pb->next;
		}
	}

	while (pa != NULL)
	{
		Insert(headab, pa->coeff, pa->exp);
		pa = pa->next;
	}

	while (pb != NULL)
	{
		Insert(headab, pb->coeff, pb->exp);
		pb = pb->next;
	}
}

void Clear(link head)
{
	link p = head->next;
	while (p != NULL)
	{
		head->next= p->next;
		free(p);
		p = head->next;
	}
	free(head);
}
相关推荐
weixin_432702262 分钟前
代码随想录算法训练营第五十五天|图论理论基础
数据结构·python·算法·深度优先·图论
小冉在学习4 分钟前
day52 图论章节刷题Part04(110.字符串接龙、105.有向图的完全可达性、106.岛屿的周长 )
算法·深度优先·图论
Repeat7155 分钟前
图论基础--孤岛系列
算法·深度优先·广度优先·图论基础
小冉在学习7 分钟前
day53 图论章节刷题Part05(并查集理论基础、寻找存在的路径)
java·算法·图论
武子康20 分钟前
大数据-212 数据挖掘 机器学习理论 - 无监督学习算法 KMeans 基本原理 簇内误差平方和
大数据·人工智能·学习·算法·机器学习·数据挖掘
passer__jw7671 小时前
【LeetCode】【算法】283. 移动零
数据结构·算法·leetcode
Ocean☾1 小时前
前端基础-html-注册界面
前端·算法·html
顶呱呱程序1 小时前
2-143 基于matlab-GUI的脉冲响应不变法实现音频滤波功能
算法·matlab·音视频·matlab-gui·音频滤波·脉冲响应不变法
爱吃生蚝的于勒1 小时前
深入学习指针(5)!!!!!!!!!!!!!!!
c语言·开发语言·数据结构·学习·计算机网络·算法
羊小猪~~1 小时前
数据结构C语言描述2(图文结合)--有头单链表,无头单链表(两种方法),链表反转、有序链表构建、排序等操作,考研可看
c语言·数据结构·c++·考研·算法·链表·visual studio