南邮OJ 1005 多项式加法(二)

一、首先,这个多项式是一个链表,多项式的每一项是链表一个节点,那么可以想到两种情况:

1)多项式只有一项或者是多项式的最后一项,那么这个节点就只需要有系数和指数两个元素,且不需要指向下 一个节点。

2)多项式的其中一项,那么这个节点就需要有系数、指数以及指向下一个节点的指针。

<span style="font-size:18px;"><span style="font-size:18px;">class  Node
{
private:
     int  coef;
     int  exp;
     Node  *  link;
public:
     Node (int  c, int  e):coef(c),exp(e)   //多项式不含指向下一个节点的构造函数
     {
           link = 0;
     }
     Node (int  c, int  e, Node * next):coef(c),exp(e) //含有指向下一个节点的构造函数
     {
           link = next;
     }
}</span></span></span>

另外节点要有插入的功能,所以节点类中要有公有成员函数Insert()函数:

<span style="font-size:18px;"><span style="font-size:18px;">Node  *  Insert(int c, int e)
{
      link = new Node (c,e,link);
      return link;
}</span></span>

二、链表是由节点构成的,且链表要有添加节点、输出链表以及链表相加的功能。

<span style="font-size:18px;"></pre><pre name="code" class="cpp"><span style="font-size:18px;">class  nodeList
{
private:
     Node * theList;
public:
     nodeList();
     ~nodeList();
      void AddNode(istream & in);
      void Output(ostream & out);
      void ListAdd(nodeList & r);
}</span></span>

三、由于对象是类,对于输出操作符不能直接输出节点类,所以我们要做一个运算符重载。

ostream & operator << (ostream & out, const Node & val)

整个思路即为上面的三大块,现在一块一块的分开分析。

第一块,链表元素节点:

<span style="font-size:18px;">class  Node
{
private:
     int coef;   //系数
     int exp;    //指数
     Node * link;   //指向下一个节点的指针
public:
    Node (int  c, int  e):coef(c),exp(e)   //多项式不含指向下一个节点的构造函数
    {
        link = 0;
    }
    Node (int  c, int  e, Node * next):coef(c),exp(e) //含有指向下一个节点的构造函数
    {
        link = next;
    }
    Node * Insert (int c, int e) //在节点后插入一个节点
    {
        link = new Node(c,e,link);
        return link;
    }
    friend class nodeList;   //链接要操作节点
    friend  ostream & operator << (ostream &, const Node &);  //节点的输出用到节点的私有成员
}</span>

第二块:链表

<span style="font-size:18px;">class nodeList
{
    friend ostream & operator << (ostream &, const nodeList & );
    friend istream & operator >> (istream &, nodeList &);
    friend nodeList & operator + (nodeList &, nodeList &);
private:
	Node * theList;
public:
	nodeList();
	~nodeList();
	void AddNode (istream & in);
	void Output (ostream & out) const;
	void Listadd (nodeList & r);
};
nodeList :: nodeList()
{
	theList = new Node (0,-1);
	theList ->link = theList;
}
nodeList::~nodeList()
{
	Node * p = theList->link;
	while (p != theList)
	{
		theList->link = p->link;
		delete p;
		p = theList->link;
	}
	delete theList;
}
void nodeList:: AddNode(istream & in)
{
	Node * q = theList;
	int count = 0;
	int c,e;
	for (;;)
	{
		cin >> c >>e;
		if (c==0 && e ==-1)
		{
			break;
		}
		else
		{
			q = q->Insert(c,e);
		}
		count ++;
	}
	if (count == 0)
	{
		q = q->Insert(0,0);
		cout << 0;
	}
}
void nodeList:: Output (ostream & out )const
{
	int i=0,j,k=0;
	Node * m = theList->link;
	for (; m!=theList & m->coef == 0;m=m->link)
    {
        i++;
    }
    m = theList->link;
	for (;m!= theList; m=m->link)
	{
		k++;
	}
	m = theList->link;
	if (k==1&& m->coef == 0)
	{
		cout << 0 << endl;
		return;
	}
	if (k==i)
    {
        cout << 0<< endl;
        return ;
    }
    for (j =0; j<i;j++)
    {
       m=m->link;
    }
	out << *m;
	m=m->link;
	for (;m!=theList;m=m->link)
	{
		if (m->coef > 0)
		{
			cout << "+";
			out << *m;
		}
		else if (m->coef < 0)
		{
			out << *m;
		}
	}
	cout << endl;
}
void nodeList::Listadd(nodeList & r)
{
	Node * q,*q1 = theList,*p;
	p = r.theList->link;
	q = q1->link;
	while(p->exp >= 0)
	{
	   while (p->exp < q->exp)
		{
			q1=q;
			q=q->link;
		}
		if (p->exp == q->exp)
		{
			q->coef = p->coef + q->coef;
			if (p->coef == 0)
			{
				q1->link = q->link;
				delete q;
				q = q1->link;
			}
			else
			{
				q1=q;
				q=q->link;
			}
	    }
		else
			q1 = q1->Insert(p->coef,p->exp);
			p=p->link;
	}
}
ostream & operator <<(ostream & out, const nodeList & x)
{
	x.Output(out);
	return out;
}
istream & operator >> (istream & in, nodeList & x)
{
	x.AddNode (in);
	return in;
}
nodeList & operator + (nodeList & a, nodeList & b )
{
	a.Listadd (b);
	return a;
}</span>

第三块:运算符重载,输出对象是节点类

<span style="font-size:18px;">ostream & operator << (ostream & out, const Node & val)
{
	if (val.coef == 1)
	{
		switch (val.exp)
		{
			case 0:out << 1;break;
			case 1:out << "X";break;
			default : out << "X^" << val.exp; break;
		}
		return out;
	}
	if (val.coef == -1)
	{
		switch (val.exp)
		{
			case 0:out << -1;break;
			case 1:out << "-X"; break;
			default : out << "-X^"<< val.exp;break;
		}
		return out;
	}
	else
	{
		out << val.coef;
		switch (val.exp)
		{
			case 0:break;
			case 1:out <<"X"; break;
			default : out<<"X^" << val.exp;break;
		}
		return out;
	}
}</span>
时间: 2024-08-10 06:06:05

南邮OJ 1005 多项式加法(二)的相关文章

南邮OJ 1005 多项式加法

题目描述:线性表是一种最简单.最基本,也是最常用的数据结构,其用途十分广泛,例如,用带表头结点的单链表求解一元整系数多项式加法和乘法运算.现给两个一元整系数多项式,请求解两者之和. 题目链接:点击打开链接 代码是借鉴别人的,链接:点击打开链接 <span style="font-size:18px;">#include <iostream> using namespace std; class nodeList; class Node //单链表节点 { pri

[python]南邮OJ代码备份爬虫

之前看过Python学习的经验,说以工程为导向学习. 自己分析了一下,一般接触Python的都有一定的其他语言基础,对于程序设计的基本逻辑,语法都有一个大概的了解.而Python这种脚本语言,没有过于独特的语法,在一定的其他语言的基础上,更是可以直接上手的. 之前看Python简明教程,半天没有进度.正好遇上Python爬虫项目,直接上手,方便快捷. 网站:http://acm.njupt.edu.cn/welcome.do?method=index,正值系统更新,于是写一个备份代码的爬虫. 使

【OJ】多项式加法

复习了一下STL,写完才想起来可以用map,代码量*3,orz 提交时遇到一次Presentation Error,OJ的空格输出实在是太随性. 思路是分别对两组输入进行排序,再通过k1,k2两个迭代器导入新的容器 注意: 容器为空时,begin()与end()返回相同 容器为空与容器不为空时,begin()返回不一致 需要进行操作的多项式中,存在0(即输入仅包含一个系数和一个负项数,如:9 -7)和多组相同幂数的情况(12 7 -7 5 3 17 23 4 15 10 -10 5 13 5 2

“Wishare杯”南邮第八届大学生程序设计竞赛之现场决赛 题解报告

A.爆炸吧,现充 (红) 时间限制:1000ms           内存限制:65536K 题目描述: a协有部分脱团分子,日复一日,年复一年地进行秀恩爱虐狗行为,对其他成员持续造成着精神伤害.Kojimai君表示在异端分子长期惨无人道的精神攻击下,早早的患上了少年痴呆症.为了应对这一症状,不得不经常把日常琐事记录下来,时间一长整本笔记本都记完了,他现在好奇自己一共记下了多少字,已知笔记本共n页,每页m行,因为心理因素,他排斥偶数页号,所以只在奇数页号的页面写字,又因为痴呆他在奇数页的第i行都

[DataStructure]多项式加法与乘法--B.链表存储(适用于零元系数多的多项式)

数据结构大作业…… 发出来大家乐呵乐呵…… 一.问题描述 给出N个多项式,求他们的和与积 二.解题报告 基本思想:加法和乘法都是把得数项直接链接在链表后面,最后统一做一个Merge&Sort工作即可.方便又快捷. (1)建立存储结构 1 struct _Poly 2 { 3 int factor;//系数 4 int Index;//幂 5 struct _Poly* next;//下一节点 6 }; 7 _Poly* poly[MAXTIMES+1]; 8 int Sum[MAXTIMES+1

[DataStructure]多项式加法与乘法--A.数组存储(适用于零元系数少的多项式)

数据结构大作业…… 发出来大家乐呵乐呵…… 一.问题描述 给出N个多项式,求他们的和与积 二.解题报告 (1)建立存储结构 1 struct _Poly 2 { 3 double Data[MAXTIMES+1]; 4 int Times; 5 }; 6 struct _Poly Poly[N+1]; (2)主程序架构 1 int main() 2 { 3 int Sum; 4 cout<<"请输入要做运算的多项式数量"<<endl; 5 cin>>

“亚信科技杯”南邮第七届大学生程序设计竞赛之网络预赛 (部分题解)

“亚信科技杯”南邮第七届大学生程序设计竞赛之网络预赛 F 自动售货机 时间限制(普通/Java) : 1000 MS/ 3000 MS          运行内存限制 : 65536 KByte总提交 : 60            测试通过 : 13  题目描述 教学楼有一台奇怪的自动售货机,它只售卖一种饮料,单价5元,并且只收5元.10元面值的货币,但是同学们都很喜欢喝.这个售货机里没有多余的找零,也就是说如果一个持有10元的同学第一个购买,则他不能获得5元找零,但是如果在他之前有一个持有5

算法练习--多项式加法

JS 实现 多项式加法 Array.prototype.existKey = function(propVal){ var i = 0; for(var i = 0;i < this.length; i++){ if(this[i].k == propVal){return i;} } return -1; } function polynAdd(a,b){ //1. parse out each exp var strA = a[0] == '-' ? a : '+' + a; var str

程序设计入门——C语言 第7周编程练习 1多项式加法(5分)

第7周编程练习 依照学术诚信条款,我保证此作业是本人独立完成的. 温馨提示: 1.本次作业属于Online Judge题目,提交后由系统即时判分. 2.学生可以在作业截止时间之前不限次数提交答案,系统将取其中的最高分作为最终成绩. 1 多项式加法(5分) 题目内容: 一个多项式可以表达为x的各次幂与系数乘积的和,比如: 2x6+3x5+12x3+6x+20 现在,你的程序要读入两个多项式,然后输出这两个多项式的和,也就是把对应的幂上的系数相加然后输出. 程序要处理的幂最大为100. 输入格式: