多项式加法——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);
}
相关推荐
珹洺1 小时前
C++从入门到实战(十)类和对象(最终部分)static成员,内部类,匿名对象与对象拷贝时的编译器优化详解
java·数据结构·c++·redis·后端·算法·链表
写bug的小屁孩1 小时前
移动零+复写零+快乐数+盛最多水的容器+有效三角形的个数
c++·算法·双指针
DARLING Zero two♡1 小时前
C++底层学习精进:模板进阶
开发语言·c++·模板
飞川撸码1 小时前
【LeetCode 热题100】208:实现 Trie (前缀树)(详细解析)(Go语言版)
算法·leetcode·golang·图搜索算法
这就是编程2 小时前
自回归模型的新浪潮?GPT-4o图像生成技术解析与未来展望
人工智能·算法·机器学习·数据挖掘·回归
勘察加熊人2 小时前
c++生成html文件helloworld
开发语言·c++·html
羑悻的小杀马特2 小时前
【狂热算法篇】探寻图论幽径:Bellman - Ford 算法的浪漫征程(通俗易懂版)
c++·算法·图论·bellman_ford算法
Fantasydg6 小时前
DAY 31 leetcode 142--链表.环形链表
算法·leetcode·链表
basketball6166 小时前
C++ STL常用算法之常用排序算法
c++·算法·排序算法
moz与京6 小时前
[附C++,JS,Python题解] Leetcode 面试150题(10)——轮转数组
c++·python·leetcode