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

📄 yuan.cpp

📁 数据结构链表-多项式
💻 CPP
字号:
#include<iostream.h>
#include<stdlib.h>
#include<math.h>
struct node *creat();
struct node 
{
	float coef;
	int exp;
    struct node *next;
};

      
struct node *creat(char c)
{
	struct node *r,*s,*head;
	float l,m;
	int n;
	head=(struct node*)malloc(sizeof(struct node));
	head->next=NULL;//将头节点指向空
	cout<<"输入多项式的系数与指数\n";
	cout<<"coef=";
	cin>>l;
	cout<<"exp=";
	cin>>n;
	c=='+'?m=l:m=-l;//如果执行减法,则将系数变为起相反数(此语句是针对第二个是、多项式的)
	while(l!=0)//当输入系数为0时结束输出
	{
		s=(struct node*)malloc(sizeof(struct node));
		s->coef=m;
		s->exp=n;
		s->next=NULL;//将当前节点的想一个节点赋为空
		if(head->next==NULL)head->next=s;//将节点插入头节点后
		else r->next=s;//将节点插入上一个节点后
		r=s;
		cout<<"输入多项式的系数与指数\n";

	cout<<"coef=";
	cin>>l;
	cout<<"exp=";
	cin>>n;
	c=='+'?m=l:m=-l;
	}

	return head;//返回头节点
}
struct node *add( struct node *ha, struct node *hb)
 {
	  struct node *p,*q,*r,*s,*head;
	  float x;
      head=(struct node*)malloc(sizeof(struct node));
	  head->next=NULL;//将头节点指向空
      p=ha;q=hb;
	  while (p!= NULL&&q!=NULL) //如果两个多项式都不为空则执行
	  {
        if (p->exp==q->exp)//当指数相等时
		{
			x=p->coef+q->coef;//系数相加
			
			if(x!=0)
			{
				s=(struct node*)malloc(sizeof(struct node));
				s->coef=x;
                s->exp=p->exp;
				s->next=NULL;
               if(head->next==NULL)head->next=s;
		       else r->next=s;
		        r=s;
			}
			p=p->next;
			q=q->next;
			
		}
		else 
			if(p->exp<q->exp)//前一个多项式相应的项的指数小于后一个多项式
			{

                 s=(struct node*)malloc(sizeof(struct node));
				 s->coef=p->coef;
                 s->exp=p->exp;
				 s->next=NULL;
                 if(head->next==NULL)head->next=s;
		         else r->next=s;
		         r=s;
				 p=p->next;
			}
		     else//后一个多项式相应的项的指数小于前一个多项式
			  {
                s=(struct node*)malloc(sizeof(struct node));
				s->coef=q->coef;
                s->exp=q->exp;
				s->next=NULL;
                if(head->next==NULL)head->next=s;
		        else r->next=s;
		        r=s;
			    q=q->next;
			  }
		
	  }
	  
	while(p!=NULL)//当只剩下第一个多项式时
	{
		           s=(struct node*)malloc(sizeof(struct node));
				  s->coef=p->coef;
                  s->exp=p->exp;
				  s->next=NULL;
                  if(head->next==NULL)head->next=s;
		          else r->next=s;
		          r=s;
				  p=p->next;
	}
	while(q!=NULL)//当只剩下第二个多项式时
	{
                  s=(struct node*)malloc(sizeof(struct node));
				  s->coef=q->coef;
                  s->exp=q->exp;
				  s->next=NULL;
                  if(head->next==NULL)head->next=s;
		          else r->next=s;
		          r=s;
				  q=q->next;
	}
	
	return head;
 }

value(struct node *pc)
{
	float x,l,sum;int m;double n;
	cout<<"输入自变量x的值\n"<<"x=";
	cin>>x;
    sum=0.0;
	while(pc!=NULL)
	{  
		l=pc->coef;
		m=pc->exp;
		n=pow(x,m);
       sum+=l*n;;
	   pc=pc->next;
	}
    return sum;
}

	 
void main()
{
	struct node *ha,*hb,*hc,*pc,*head;
	char c,d,e;
	float sum;
	c='+';       
	cout<<"                             _________________\n";
	cout<<"                             |一元多项式的运算|\n";
	cout<<"                             --————————\n";
	cout<<"********************************************************************************\n";
	cout<<"输入第一个多项式(升幂顺序输入,以输入系数为0是结束)\n";
    cout<<"********************************************************************************\n";
    ha=creat(c);  
    cout<<"********************************************************************************\n";
    cout<<"输入运算符\n";
	cin>>d;
	while(d!='+'&&d!='-')
	{
		cout<<"输入运算符\n";
		cin>>d;
	}
	cout<<"输入第二个多项式(升幂顺序输入,以输入系数为0是结束)\n";
    cout<<"********************************************************************************\n";
	hb=creat(d);
	cout<<"********************************************************************************\n";
    head=add(ha,hb);
	hc=head->next->next;
	cout<<"运算结果是:(若为0则不输出)\n";
	cout<<"********************************************************************************\n";
	while(hc!=NULL)//输出计算后的多项式的个项
	{
		cout<<"  coef="<<hc->coef;
		cout<<" exp="<<hc->exp;
		cout<<"\n";
		hc=hc->next;
	}
    cout<<"********************************************************************************\n";
    cout<<"你是否需要求值(需要请输入y,若不需要,请输入别的任何字符)\n";
	cin>>e;
	if(e=='y')
	{
	  pc=head->next->next;
	  sum=value(pc);
	  cout<<"你要求的值为:\n"<<sum<<"\n";
	}

}


⌨️ 快捷键说明

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