南邮OJ 1005 多项式加法

题目描述: 线性表是一种最简单、最基本,也是最常用的数据结构,其用途十分广泛,例如,用带表头结点的单链表求解一元整系数多项式加法和乘法运算。 现给两个一元整系数多项式,请求解两者之和。

题目链接:点击打开链接

代码是借鉴别人的,链接:点击打开链接

<span style="font-size:18px;">#include <iostream>
using namespace std;
class nodeList;
class Node   //单链表节点
{
private:
	int coef;
	int exp;
	Node * link;
	friend class nodeList;
	friend ostream & operator << (ostream &,const Node &);
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;
	}
};

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;
	}
}
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;
	}

int main()
{
	nodeList p,q,q1;
    cin>>p,cout<<p;
    cin>>q,cout<<q;
    q1=q+p;
    cout<<q1;
}</span>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值