⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 1.cpp

📁 多项式的加减乘运算
💻 CPP
字号:
#include<iostream.h>
//using namespace std;

class Term
{
public:
	Term(int c,int e);
	Term(int c,int e,Term* nxt);
	Term* InsertAfter(int c,int e);
	friend class poly;
	friend ostream& operator << (ostream &,const Term &);
private:
	int coef;
	int exp;
	Term * link;
	friend ostream& operator << (ostream &,const Term &);
	
};

Term::Term(int c,int e):coef(c),exp(e)
{
	link=0;
}
Term::Term(int c,int e,Term * nxt):coef(c),exp(e)
{
	link=nxt;
}
Term* Term::InsertAfter(int c,int e)
{
	link=new Term(c,e,link);
	return link;
}
ostream &operator << (ostream &out,const Term & val)
{
	if(val.coef ==0)
		return out;
	out<<val.coef;
	switch(val.exp)
	{
	case 0:break;
	case 1:out<<"X";break;
	default:out<<"X^"<<val.exp;break;
	}
	return out;
}
class poly
{
public:
	poly();
	~poly();
	void AddTerms(istream & in);
	void Output(ostream& out)const;
	void PolyAdd(poly & r);
    void Polyjian(poly & r);
    void Polycheng(poly & r);
private:
	Term* thelist;
	friend ostream & operator << (ostream & out,const poly & x);
	friend istream & operator >> (istream & in,poly & x);
	friend poly & operator + (poly & a,poly & b);
	friend poly & operator - (poly & a,poly & b);
	friend poly & operator * (poly & a,poly & b);
};
poly::poly()
{
	thelist =new Term(0,-1);
	thelist->link=thelist;
}
poly::~poly()
{ 
	Term *p=thelist->link;
	while(p!=thelist)
	{
		thelist->link=p->link;
		delete p;
		p=thelist->link;
	}
	delete thelist;
}
void poly::AddTerms(istream & in)
{
	Term *q=thelist;
	int c,e;
	for(;;){
		cout<<"请输入一个多项式(coef,exp):"<<endl;
		cin>>c>>e;
		if(e<0)break;
		q=q->InsertAfter(c,e);
	}
}
void poly::Output(ostream& out)const
{
	int first=1;Term*p=thelist->link;
	cout<<"输出多项式为:\n"<<endl;
	for(;p!=thelist;p=p->link){
		if(!first&&(p->coef>0))out<<"+";
		first=0;
		out<<*p;
	}
	cout<<"\n"<<endl;
}
void poly::PolyAdd(poly & r)
{
	Term*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=q->coef+p->coef;
		//	p=p->link;
			if(q->coef==0)
			{
				q1->link=q->link;delete(q);
				q=q1->link;
			}
			else
			{
				q1=q;q=q->link;
                p=p->link;
			}
		}
		else
		{
			q1=q1->InsertAfter(p->coef,p->exp);
			p=p->link;
		}
    
	}
}
void poly::Polyjian(poly & r)
{
Term*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=q->coef-p->coef;
		if(q->coef==0)
			{
				q1->link=q->link;delete(q);
				q=q1->link;
			}
			else
			{
				q1=q;q=q->link;
			}
		}
		else
		{
			q1=q1->InsertAfter(p->coef,p->exp);
		//	p=p->link;
		}
    p=p->link;
	}
}
void poly::Polycheng(poly & r)
{
	Term*q=thelist,*p,*q1;
	poly k;
	p=r.thelist->link;
	q1=k.thelist->link;
//	m->link=new Term(m->coef,m->exp,m->link);

	while(p->exp>=0){
		while(q->exp>=0){
		    q1->coef=p->coef*q->coef;
			q1->exp=p->exp+q->exp;
          	q1=q1->InsertAfter(q1->coef,q1->exp);
			q1=q1->link;
			q=q->link;
		}
		p=p->link;
	}
	for(q1=k.thelist->link;q1->exp>=0;q1=q1->link)   //有问题
{     
		for(q=thelist->link;q->exp>=0;q=q->link){
			q->coef=q1->coef;
			q->exp=q1->exp;
		}
	}

}
ostream & operator << (ostream & out,const poly & x)
{
	x.Output(out);return out;
}
istream & operator >> (istream & in,poly & x)
{
	x.AddTerms(in);return in;
}
poly & operator + (poly & a,poly & b)
{
	a.PolyAdd(b);return a;
}
poly & operator - (poly & a,poly & b)
{
	a.Polyjian(b);return a;
}
poly & operator * (poly & a,poly & b)
{
 a.Polycheng(b);return a;
}
void main()
{
	poly a,b;
	cin>>a;cout<<a;
	cin>>b;cout<<b;
	
	cout<<"请输入要进行的运算:1.a+b  2.a-b  3.a*b"<<endl;
	int k;
	cin>>k;
	if(k==1)
	{a=a+b;cout<<a;}
    else
	   if(k==2)
	   {a=a-b;cout<<a;}
	   else
	        if(k==3)
			{a=a*b;
			cout<<a;}
	           else
		      cout<<"无法进行所需要的运算"<<endl;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -