多项式的加法

一、多项式加法

1.完整代码

#include <stdio.h>
#include <iostream>

using namespace std;

typedef struct LinkNode
{
	int coefficient;//系数
	int exponent;//指数
	struct LinkNode *next;
}*LinkList,*NodePtr;

/*初始化*/
LinkList initLinkList()
{
	LinkList tempHeader = new LinkNode;

	tempHeader->coefficient = 0;
	tempHeader->exponent = 0;
	tempHeader->next = NULL;

	return tempHeader;
}

bool printNode(NodePtr paraPtr,char paraChar)
{
	if(!paraPtr)
		return false;

	printf("The element of %c is (%d * 10^%d)\n",paraChar,paraPtr->coefficient,paraPtr->exponent);
	return true;
}

/*在一个式子里添加项*/
void appendElement(LinkList paraHeader,int paraCoefficient,int paraExponent)
{
	NodePtr q=NULL,p=NULL;

	// Step 1. Construct a new node.
	q = new struct LinkNode;

	q->coefficient = paraCoefficient;
	q->exponent = paraExponent;
	q->next = NULL;

	// Step 2. Search to the tail.
	p = paraHeader;
	while(p->next)
	{
		p = p->next;
	}

	// Step 3. Now add/link.
	p->next = q;
}

void add(NodePtr paraList1, NodePtr paraList2)
{
	NodePtr s = NULL,p = NULL,q = NULL,r = NULL;

	// Step 1. Search to the position.
	p = paraList1->next;

	q = paraList2->next;

	r = paraList1;// Previous pointer for inserting.

	delete paraList2;//The second list is destroyed. 

	while(p && q->next)
	{
		if(p->exponent < q->exponent)
		{
			/*cout<<"case1:"<<endl;*/
			r = p;
			p = p->next;
		}else if(p->exponent > q->exponent)
		{
			/*cout<<"case2:"<<endl;*/
			r->next = q;
			r  = q;
			q = q->next;
		}else if(p->exponent == q->exponent)
		{
			/*cout<<"case3:"<<endl;*/
			p->coefficient = p->coefficient + q->coefficient;
			printf("The coefficient is: %d.\r\n", p->coefficient);
			
			if(p->coefficient == 0)
			{
				/*cout<<"case3.1"<<endl;*/
				s = p;
				p = p->next;
				delete s;
			}else
			{
				/*cout<<"case3.2"<<endl;*/
				r = p;
				p = p->next;
			}

			s = q;
			q = q->next;
			delete s;
		}

	}

	if(!p)
		r->next = q;
	else
		r->next = p;

	cout<<endl<<"Addition ends."<<endl;

}

void printList(LinkList paraHeader)
{
	NodePtr p = paraHeader;

	while(p)
	{
		printf("%d * 10^%d + ", p->coefficient, p->exponent);
		p = p->next;
	}

	cout<<endl<<endl;
}

void additionTest(){
	// Step 1. Initialize the first polynomial.
	LinkList tempList1 = initLinkList();
	appendElement(tempList1, 7, 0);
	appendElement(tempList1, 3, 1);
	appendElement(tempList1, 9, 8);
	appendElement(tempList1, 5, 17);
	appendElement(tempList1, 19, 27);
	appendElement(tempList1, 6, 21);
	printList(tempList1);

	// Step 2. Initialize the second polynomial.
	LinkList tempList2 = initLinkList();
	appendElement(tempList2, 8, 1);
	appendElement(tempList2, 22, 7);
	appendElement(tempList2, -9, 8);
	printList(tempList2);

	// Step 3. Add them to the first.
	add(tempList1, tempList2);
	printList(tempList1);
}


int main(){
	additionTest();
	printf("Finish.\r\n");

	system("pause");
	return 0;
}

2.运行结果

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值