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

📄 poly88.cpp

📁 1、 应用程序 直接可以实现多项式的各项操作。 2、 查看原代码VC++6.0打开“多项式poly88.dsp”或者用记事本打开“多项式poly88.cpp” 3、 代码简单说明:
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// poly8.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "math.h"

#define NULL 0
#include<iostream.h>
#define OK 1
#define FLASE 0
const MAXSIZE=100;

struct polynelem
{
	float coef;
	int expn;
	polynelem *next;
};
//////////////////////////////////////////////////////////////////////////
struct term
{
	float coef;//多项式系数
    int exp;//多项式指数
};
typedef struct   //定义多项式结构体
{
	struct term List[MAXSIZE];
	int n;//多项式项数
}Ploynomial;
	
      Ploynomial pa,pb,p;
//////////////////////////////////////////////////////////////////////////
class polynomail  
{
public:
	double Resultpolyn(double r);
	void daoshu(polynomail  &pe);
	polynomail  * itemlist(polynomail  * p1,polynelem *e1);
	 polynomail * Mulpolyn( polynomail *);
	void freeNode(polynelem*e);
	void DelFirst(polynelem*e1, polynelem *e2);
	int polynomail ::compare(polynelem *e1, polynelem *e2);
	polynelem* makenode(polynelem * e);
   	polynomail* Copy();
	polynomail *Substractpolyn(polynomail*pb);
	polynomail *Addpolyn(polynomail *pb);
	//int cmp(polynelem ea,polynelem eb);
	void polyncreat();
	void printpolyn();
	void deastroypolyn();
	void polyndelete(polynelem *p);
	int polnprior(polynelem *po,polynelem *&p);
	polynelem *head;
	polynomail();
	virtual ~polynomail();
};
//////////////////////////////////////////////////////////////////////////////////
void PrintPloyn(Ploynomial p)  
{
	int k;
	for(k=0;k<p.n;k++)
	{
		if(p.List[k].coef==0)
		{
		}
		else if(p.List[k].coef==1)
		{
			if(p.List[k].exp==1)
                cout<<"x";
			else if(p.List[k].exp==0)
				cout<<"1";
			else
			    cout<<"x^"<<p.List[k].exp;
			if(k!=p.n-1&&p.List[k+1].coef>0)
				cout<<"+";
		}
		else if(p.List[k].coef==-1)
		{
			if(p.List[k].exp==1)
                cout<<"-x";
			else if(p.List[k].exp==0)
				cout<<"-1";
			else
			    cout<<"-x^"<<p.List[k].exp;
			if(k!=p.n-1&&p.List[k+1].coef>0)
				cout<<"+";
		}
		else 
		{
			if(p.List[k].exp==1)
                cout<<"x";
			else if(p.List[k].exp==0)
				cout<<p.List[k].coef;
            else
			    cout<<p.List[k].coef<<"x^"<<p.List[k].exp;
			if(k!=p.n-1&&p.List[k+1].coef>0)
				cout<<"+";
		}
	}
}

//生成多项式
void CreatPloyn(Ploynomial &p)
{
	int k=0;
	p.n=0;//输入提示 
    cin>>p.List[k].coef>>p.List[k].exp;
	while(p.List[k].coef!=0)
	{
		k++;
		p.n++;
		cin>>p.List[k].coef>>p.List[k].exp;
	}
            cout<<"y=";
			 PrintPloyn(p);
			 cout<<endl;
}       //多项式加法运算
Ploynomial AddPloyn(Ploynomial pa,Ploynomial pb)
{
	Ploynomial pc;
	float coef;
	int a=0,b=0,c=0;
	pc.n=0;
	while(a!=pa.n && b!=pb.n)
	{
		if(pa.List[a].exp==pb.List[b].exp)
		{
			coef=pa.List[a].coef+pb.List[b].coef;
			if(coef)
			{
				pc.List[c].coef=coef;
				pc.List[c].exp=pa.List[a].exp;
				pc.n++;
				a++;
				b++;
				c++;
			}
		}
		else if(pa.List[a].exp<pb.List[b].exp)
		{
			pc.List[c].coef=pa.List[a].coef;
			pc.List[c].exp=pa.List[a].exp;
			pc.n++;
			a++;
			c++;
		}
		else
		{
			pc.List[c].coef=pb.List[b].coef;
			pc.List[c].exp=pb.List[b].exp;
			pc.n++;
			b++;
			c++;
		}
	}
	while(a!=pa.n)
	{
		pc.List[c].coef=pa.List[a].coef;
		pc.List[c].exp=pa.List[a].exp;
		pc.n++;
		a++;
		c++;
	}
	while(b!=pb.n)
	{
		pc.List[c].coef=pb.List[b].coef;
		pc.List[c].exp=pb.List[b].exp;
		pc.n++;
		b++;
		c++;
	}
         return pc;
}
//多项式减法运算
void SubtractPloyn(Ploynomial pa,Ploynomial pb)
{
	Ploynomial pc;
	float coef;
	int a=0,b=0,c=0;
	pc.n=0;
	while (a!=pa.n&&b!=pb.n)
	{
		if(pa.List[a].exp==pb.List[b].exp)
		{
			coef=pa.List[a].coef-pb.List[b].coef;
				pc.List[c].coef=coef;
				pc.List[c].exp=pa.List[a].exp;
				pc.n++;
				a++;
				b++;
				c++;
		}
		else if(pa.List[a].exp<pb.List[b].exp)
		{
			pc.List[c].coef=pa.List[a].coef;
			pc.List[c].exp=pa.List[a].exp;
			pc.n++;
			a++;
			c++;
		}
		else
		{
			pc.List[c].coef=-pb.List[b].coef;
			pc.List[c].exp=pb.List[b].exp;
			pc.n++;
			b++;
			c++;
		}
	}
	while(a!=pa.n)
	{
		pc.List[c].coef=pa.List[a].coef;
		pc.List[c].exp=pa.List[a].exp;
		pc.n++;
		a++;
		c++;
	}
	while(b!=pb.n)
	{
		pc.List[c].coef=-pb.List[b].coef;
		pc.List[c].exp=pb.List[b].exp;
		pc.n++;
		b++;
		c++;
	}
	PrintPloyn(pc);
}

//多项式乘法运算
void MultiPloyn(Ploynomial pa,Ploynomial pb)
{
	Ploynomial pc,temp;
	int i,j;
	for(i=0;i<pa.n*pb.n;i++)
	{
		pc.List[i].coef=0.0;
	    pc.List[i].exp=0;
		temp.List[i].coef=0.0;
		temp.List[i].exp=0;
	}
	pc.n=0;
	temp.n=0;
	for(i=0;i<pa.n;i++)
	{
		for(j=0;j<pb.n;j++)
		{
			temp.List[j].coef=pa.List[i].coef*pb.List[j].coef;
			temp.List[j].exp=pa.List[i].exp+pb.List[j].exp;
			temp.n++;
		}
		pc=AddPloyn(temp,pc);
	}
	PrintPloyn(pc);
}//多项式求值
double ValuePloyn(Ploynomial p,double x)
{
	double sum=0;
	for(int i=0;i<p.n;i++)
	sum+=p.List[i].coef*pow(x,p.List[i].exp);
	return sum;
}//多项式求导运算
void DerivativePloyn(Ploynomial p,int e)
{
	int i,power,counter;
	i=0;
    counter=0;
	while(counter!=p.n)
	{
		power=e;
		while(power)
		{
			p.List[counter].coef=p.List[counter].coef*p.List[counter].exp;
			power--;
			p.List[counter].exp--;
		}
		counter++;
	}
	cout<<"多项式"<<power<<"阶导数为:"<<endl;
	PrintPloyn(p);
	cout<<endl;
}////////////////////////
polynomail::polynomail()
{         //多项式的初始化
	head=new polynelem;
	head->next=NULL;

}
polynomail::~polynomail()
{//销毁多项式
	deastroypolyn();
}
void polynomail::daoshu(polynomail &pe)
{//对当的多项式求导
    polynelem *p;
	p=pe.head;
	polynelem *p1;
	p1=head->next;
	while(p1)
	{
		polynelem *p2;
		p2=new polynelem;
		p2->expn=p1->expn-1;
		p2->coef=p1->expn*p1->coef;
		p2->next=NULL;
		p->next=p2;
		p=p2;
		p1=p1->next;
	}//while
}//daoshu
void polynomail::freeNode(polynelem *e)
{
delete e;
}

double polynomail::Resultpolyn(double r)
{
	polynelem*m;
    double sum=0;
    m=head->next;
while(m!=NULL)
{
	sum=sum+m->coef*pow(r,m->expn);
    m=m->next;
}
return sum;
}
void polynomail::deastroypolyn()
{//销毁多项式   保留头节点
	while(head->next)
	{
		polynelem *p;
		p=head->next;
		head->next=head->next->next;
		delete p;
	}//while
}//destroypolyn
void polynomail::DelFirst(polynelem *e1, polynelem *e2)
{
   e1->next=e2->next;
}
int polynomail::compare(polynelem *e1, polynelem *e2)
{ if(e1->expn>e2->expn)
       return 1;
  else 
	  if (e1->expn==e2->expn)
		 return 0;
      else return -1;
}
polynelem* polynomail::makenode(polynelem * e2)
{
polynelem *e1;
e1=new polynelem;
e1->coef=e2->coef;
e1->expn=e2->expn;
e1->next=NULL;
return e1;
}
polynomail* polynomail::Copy()
{     polynomail *pb;
      pb=new polynomail;
      polynelem*e1,*e2,*e3;
      e2=e3=pb->head ;
      e2=e2->next;
      e1=head->next;
       while(e1!=NULL)
	   {e2=makenode(e1);
        e3->next=e2;
        e3=e2;
        e1=e1->next;
	   }
      return pb;
}
polynomail* polynomail::Substractpolyn(polynomail*pb)
{
  polynelem*m;
  polynomail* pc=pb->Copy();
  m=pc->head;
 while (m!=NULL)
 {m->coef=-m->coef;
  m=m->next;
 }
 return(Addpolyn(pc));
}
int polynomail::polnprior(polynelem *po, polynelem *&p)
{                                                        //pos指向多项的某项
     polynelem *p1;                                                 //用p返回pos的前区;否则返回FLASE
     p1=head;
	while(p1)
	{
		if(p1->next==po)
		{
			p=p1;
			return OK;
		}
		p1=p1->next;
	}//while
		return FLASE;
}//polnpriir
void polynomail::polyndelete(polynelem *p)
{//p指多项式的一项    销毁p指向的项
 	polynelem *po;
	polnprior(p,po);
	po->next=po->next->next;
	delete p;
}//polyndelete
polynomail* polynomail::Addpolyn(polynomail *pb)
{ 
	    polynelem *ha,*hb,*qa,*qb;
         polynomail *pc=Copy();
          polynomail *pd=pb->Copy();

           float sum;
        ha=pc->head;
         hb=pd->head ;
         qa=ha->next;
      qb=hb->next;
  while(ha->next!=NULL&&hb->next!=NULL)

  {
	  switch(compare(qa,qb))

	  {  
	  case -1:
         ha=qa;
	     qa=qa->next;
	    break;
     case 0 :
	     sum=qa->coef+qb->coef;
	 if (sum!=0)
	 {   qa->coef=sum;
	     ha=qa;
		 qa=qa->next;
	 }
	 	 else 
     {DelFirst(ha,qa);
	  freeNode(qa);
	  qa=ha->next;
	 }

⌨️ 快捷键说明

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