编程题03 一元多项式的乘法与加法运算

#include<iostream>
using namespace std;

typedef int ElementType;
typedef struct Node* PtrToNode;
typedef PtrToNode LinkList;
typedef PtrToNode Position;
struct Node {
	ElementType Coefficient;
	ElementType Exponent;
	Position Next;
};

LinkList CreatList(int n) {
	LinkList head;
	Position rear, p;
	ElementType coe, exp;
	head = (LinkList)malloc(sizeof(struct Node));
	rear = head;
	while (n--) {
		cin >> coe >> exp;
		p = (LinkList)malloc(sizeof(struct Node));
		p->Coefficient = coe;
		p->Exponent = exp;
		rear->Next = p;
		rear = p;
	}
	rear->Next = NULL;
	return head;
}

LinkList Add_List(LinkList a, LinkList b) {
	LinkList ha, hb, hc;
	Position rear, ptr;
	ElementType temp;

	ha = a->Next;
	hb = b->Next;
	hc = (LinkList)malloc(sizeof(struct Node));
	rear = hc;
	while (ha && hb) {
		ptr = (LinkList)malloc(sizeof(struct Node));
		if (ha->Exponent > hb->Exponent) {
			ptr->Exponent = ha->Exponent;
			ptr->Coefficient = ha->Coefficient;
			ha = ha->Next;
			rear->Next = ptr;
			rear = ptr;
		}
		else if (ha->Exponent < hb->Exponent) {
			ptr->Exponent = hb->Exponent;
			ptr->Coefficient = hb->Coefficient;
			hb = hb->Next;
			rear->Next = ptr;
			rear = ptr;
		}
		else {
			temp = ha->Coefficient + hb->Coefficient;
			if (temp) {
				ptr->Exponent = ha->Exponent;
				ptr->Coefficient = temp;
				rear->Next = ptr;
				rear = ptr;
			}
			ha = ha->Next;
			hb = hb->Next;
		}
	}
	if (ha == NULL) {
		while (hb) {
			ptr = (LinkList)malloc(sizeof(struct Node));
			ptr->Exponent = hb->Exponent;
			ptr->Coefficient = hb->Coefficient;
			hb = hb->Next;
			rear->Next = ptr;
			rear = ptr;
		}
	}
	if (hb == NULL) {
		while (ha) {
			ptr = (LinkList)malloc(sizeof(struct Node));
			ptr->Exponent = ha->Exponent;
			ptr->Coefficient = ha->Coefficient;
			ha = ha->Next;
			rear->Next = ptr;
			rear = ptr;
		}
	}
	rear->Next = NULL;
	return hc;
}

LinkList Mul_List(LinkList a, LinkList b) {
	LinkList ha, hb, hc;
	Position rear, ptr, c;
	ha = a->Next;
	hb = b->Next;
	hc = CreatList(0);
	if (ha == NULL || hb == NULL) return hc;
	while (ha) {
		c = (LinkList)malloc(sizeof(struct Node));
		rear = c;
		hb = b->Next;
		while (hb) {
			ptr = (LinkList)malloc(sizeof(struct Node));
			ptr->Exponent = ha->Exponent + hb->Exponent;
			ptr->Coefficient = ha->Coefficient * hb->Coefficient;
			hb = hb->Next;
			rear->Next = ptr;
			rear = ptr;
		}
		rear->Next = NULL;
		hc = Add_List(hc, c);
		ha = ha->Next;
	}
	return hc;
}

void Print(LinkList L) {
	LinkList hc;
	int f = 0;
	hc = L->Next;
	if (hc == NULL) cout << "0 0";
	while (hc) {
		if (f) cout << " ";
		else f = 1;
		cout << hc->Coefficient << " " << hc->Exponent;
		hc = hc->Next;
	}
	cout << endl;
}

int main() {
	int n1, n2;
	LinkList L1, L2, L3, L4;
	cin >> n1;
	L1 = CreatList(n1);
	cin >> n2;
	L2 = CreatList(n2);

	L3 = Mul_List(L1, L2);
	L4 = Add_List(L1, L2);

	Print(L3);
	Print(L4);

	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值