多项式加法——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);
}
相关推荐
背影疾风几秒前
C++之路:类基础、构造析构、拷贝构造函数
linux·开发语言·c++
程序员弘羽26 分钟前
C++ 第四阶段 内存管理 - 第二节:避免内存泄漏的技巧
java·jvm·c++
爱思德学术39 分钟前
中国计算机学会(CCF)推荐学术会议-B(交叉/综合/新兴):BIBM 2025
算法
冰糖猕猴桃1 小时前
【Python】进阶 - 数据结构与算法
开发语言·数据结构·python·算法·时间复杂度、空间复杂度·树、二叉树·堆、图
lifallen1 小时前
Paimon vs. HBase:全链路开销对比
java·大数据·数据结构·数据库·算法·flink·hbase
liujing102329292 小时前
Day04_刷题niuke20250703
java·开发语言·算法
DolphinDB2 小时前
如何在C++交易系统中集成高性能回测与模拟撮合
c++
筏.k2 小时前
C++ 网络编程(14) asio多线程模型IOThreadPool
网络·c++·架构
2401_881244403 小时前
Treap树
数据结构·算法
乌萨奇也要立志学C++3 小时前
二叉树OJ题(单值树、相同树、找子树、构建和遍历)
数据结构·算法