1002 A+B for Polynomials (25 分)

1002 A+B for Polynomials (25 分)

难点分析

1.要考虑到当输入项的指数相同的时候,其系数相加有可能为零
2.当你对两个链表继续提取的时候,在循环处理中,你要处理好有可能有一个链表提前就移动到尾部了
3.在这里有一个特殊的语法,也就是在c++中如何保留到小数点后一位

头部库是 #include<iomanip>  具体的语法是 cout.setf(ios::fixed);cout << " " << setprecision(1) << p->exp << " " << setprecision(1)<<p->cof;

具体的代码展示如下所示

#include<iostream>
#include<malloc.h>
#include<iomanip>
using namespace std;
//精确到一位小数
typedef struct node* polynominal;

struct node
{
	int exp;//表示的是指数值 
	float cof;//表示的是参数值 
	polynominal next;//下一个节点 
};

polynominal insert(polynominal L,polynominal p)
{
	polynominal tmp = L;
	while ((tmp->next) && (tmp->next->exp > p->exp))
	{
		tmp = tmp->next;
	}
	if (tmp->next == NULL)
	{
		tmp->next = p;
		return L;
	}
	else
	{
		if (tmp->next->exp == p->exp)
		{
			if (tmp->next->cof + p->cof == 0)
			{
				polynominal t = tmp->next;
				tmp->next = t->next;
				delete t;
				return L;
			}
			else
			{
				tmp->next->cof = tmp->next->cof + p->cof;
				return L;
			}
		}
		else
		{
			p->next = tmp->next;
			tmp->next = p;
			return L;
		}
	}
}


polynominal create()//建立一串多项式链表 
{
	int number;//多项式的点子个数 
	polynominal L, p;
	L = new struct node;
	L->next = NULL;//创建一个新的头节点 
	p = L;//移动的标记 
	cin >> number;//输入总体非零的项数 
	for (int i = 1; i <= number; i++)
	{
		polynominal tmp = new struct node;
		cin >> tmp->exp;
		cin >> tmp->cof;
		tmp->next = NULL;
		L = insert(L, tmp);//与其他环节共用的一个函数 
	}
	return L;
}

void print(polynominal p)
{
	int count = 0;
	polynominal tmp = p;
	while (tmp->next)
	{
		tmp = tmp->next;
		count++;
	}
	cout << count;
	while (p->next)
	{
		p = p->next;
		//在这里有一个精确到小数点后一位的常用语法 
		cout.setf(ios::fixed);
		cout << " " << setprecision(1) << p->exp << " " << setprecision(1)<<p->cof;
	}
}

polynominal sum(polynominal L1, polynominal L2)
{
	polynominal p1, p2, L3, p3;
	p1 = L1->next;
	p2 = L2->next;
	L3 = new struct node;
	L3->next = NULL;
	p3 = L3;
	while (p1 && p2)
	{
		if (p1->exp > p2->exp)
		{
			polynominal s = new struct node;
			s->cof = p1->cof;
			s->exp = p1->exp;
			s->next = NULL;
			p3->next = s;
			p3 = s;
			p1 = p1->next;
		}
		else if (p1->exp < p2->exp)
		{
			polynominal s = new struct node;
			s->cof = p2->cof;
			s->exp = p2->exp;
			s->next = NULL;
			p3->next = s;
			p3 = s;
			p2 = p2->next;
		}
		else
		{
			if (p1->cof + p2->cof != 0)
			{
				polynominal s = new struct node;
				s->cof = p2->cof + p1->cof;
				s->exp = p2->exp;
				s->next = NULL;
				p3->next = s;
				p3 = s;
			}
			//如果对应相同指数的系数和为零,那么两个链表都往后移动 
			p2 = p2->next;
			p1 = p1->next;
		}
	}
	//对于剩余部分的处理 
	if (p1) p3->next = p1;
	else p3->next = p2;
	return L3;
}

int main()
{
	polynominal p1 = create();
	//print(p1);
	polynominal p2 = create();
	//print(p2);
	polynominal p3 = sum(p1, p2);
	print(p3);
	system("pause");
	return 0;
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值