02-线性结构2 一元多项式的乘法与加法运算

#pragma warning(disable:4996)

#include <stdio.h>
#include <stdlib.h>

typedef struct Node *List;
struct Node
{
	int coef;//系数
	int expon;//指数
	List next;
};

List CreateList(int n);
List Mul(List list1,List list2);
List Add(List list1,List list2);
void Print(List L);

int main()
{
	List list1, list2,mul,add;
	int number1, number2;
	scanf("%d",&number1);
	list1 = CreateList(number1);
	scanf("%d",&number2);
	list2 = CreateList(number2);
	
	mul = Mul(list1,list2);
	add = Add(list1,list2);

	Print(mul);
	printf("\n");
	Print(add);

	return 0;
}

List CreateList(int n)
{
	List L,t,cycle;
	L = (List)malloc(sizeof(struct Node));
	L->next = NULL;
	t = L;
	for (int i=0;i<n;i++)
	{
		List temp;
		temp = (List)malloc(sizeof(struct Node));
		scanf("%d",&temp->coef);
		scanf("%d",&temp->expon);
		t->next = temp;
		t = temp;
		temp->next = NULL;
	}
	t->next = NULL;
	cycle = L;
	L = L->next;
	free(cycle);
	return L;
}

void Print(List L)
{
	if (!L)
	{
		printf("0 0");
	}
	else
	{
		printf("%d %d", L->coef, L->expon);
		L = L->next;
	}
	while (L)
	{
		printf(" %d %d",L->coef,L->expon);
		L = L->next;
	}
}

List Mul(List list1,List list2)
{
	List L,t,l1,l2;
	int flag;
	L = (List)malloc(sizeof(struct Node));
	L->next = NULL;
	flag = 0;
	l1 = list1;


	while (l1)
	{	
		List t2,t3,cycle;
		t2= (List)malloc(sizeof(struct Node));
		t2->next = NULL;
		t3 = t2;
		t = L;
		l2 = list2;
		while (l2)
		{
			List temp;
			temp = (List)malloc(sizeof(struct Node));
			temp->next = NULL;
			temp->coef = l1->coef * l2->coef;
			temp->expon = l1->expon + l2->expon;
			t3->next = temp;
			t3 = temp;
			l2 = l2->next;
		}
		cycle = t2;
		t2 = t2->next;
		free(cycle);

		if (flag == 0)
		{

			flag = 1;
			cycle = t;
			t = t ->next;
			free(cycle);
		}
		L = Add(t, t2);
		l1 = l1->next;
	}

	return L;
}

List Add(List list1, List list2)
{
	List L, t, cycle,l1,l2;
	L = (List)malloc(sizeof(struct Node));
	L->next = NULL;
	t = L;
	l1 = list1;
	l2 = list2;

	while (l1 && l2)
	{
		if (l1->expon>l2->expon)
		{
			List temp;
			temp = (List)malloc(sizeof(struct Node));
			temp->expon = l1->expon;
			temp->coef = l1->coef;
			if (temp->coef!=0)
			{
				t->next = temp;
				t = temp;
			}
			l1 = l1->next;
			temp->next = NULL;
		}
		else if (l1->expon < l2->expon)
		{
			List temp;
			temp = (List)malloc(sizeof(struct Node));
			temp->expon = l2->expon;
			temp->coef = l2->coef;
			if (temp->coef != 0)
			{
				t->next = temp;
				t = temp;
			}
			l2 = l2->next;
			temp->next = NULL;
		}
		else
		{
			List temp;
			temp = (List)malloc(sizeof(struct Node));
			temp->expon = l1->expon;
			temp->coef = l1->coef+l2->coef;
			if (temp->coef != 0)
			{
				t->next = temp;	
				t = temp;
			}
			l1 = l1->next;
			l2 = l2->next;
			temp->next = NULL;
		}
	}

	while (l1)
	{
		List temp;
		temp = (List)malloc(sizeof(struct Node));
		temp->expon = l1->expon;
		temp->coef = l1->coef;
		if (temp->coef != 0)
		{
			t->next = temp;
			t = temp;
		}
		l1 = l1->next;
		temp->next = NULL;
	}
	while (l2)
	{
		List temp;
		temp = (List)malloc(sizeof(struct Node));
		temp->expon = l2->expon;
		temp->coef = l2->coef;
		if (temp->coef != 0)
		{
			t->next = temp;
			t = temp;
		}
		l2 = l2->next;
		temp->next=NULL;
	}
	cycle = L;
	L = L->next;
	free(cycle);
	return L;
}

题源中国mooc

p.s:这个练习里变量还挺多的,大家多加注意。(来自一个debug,de了几个小时,最后发现是变量的区分后缀打错了人的自我感言。)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值