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

📄 fdksk.cpp

📁 有用的东西哦!和大家一起分享咧!以后要多交流哦!
💻 CPP
字号:
#include<iostream.h>
struct node
{
	int coef;//系数

	int expn;//指数
	node*next;
};
typedef node*pointer;

node* create()
{
	int c,e;
	node*head,
        *ps,
		*pend;
	head=new node;
	ps=head;
	head->next=NULL;
	cout<<"输入多项式的系数和指数:\n";
	cin>>c>>e;
	while(c!=0)
	{
		pend=new node;
		pend->coef=c;
		pend->expn=e;
		ps->next=pend;
	    ps=pend;
	    pend->next=NULL;
		cin>>c>>e;
	}
    pend->next=NULL;

	return head;
	//delete ps;
}
void attach(int c,int e,pointer&p)
{
	p->next=new node;
	p=p->next;
	p->coef=c;
	p->expn=e;
}
void addpolyn(pointer ha,pointer hb,pointer&hc)
{
	pointer pa,pb,pc;
	int x;
	pc=new node;
	pa=ha->next;
	pb=hb->next;
	pc=hc;
	while(pa&&pb)
	{
		if(pa->expn==pb->expn)
		{
			x=pa->coef+pb->coef;
			if(x)attach(x,pa->expn,pc);
			pa=pa->next;
			pb=pb->next;
		}
		else if(pa->expn>pb->expn)
		{
			attach(pb->coef,pb->expn,pc);
			pb=pb->next;
		}
		else
		{
			attach(pa->coef,pa->expn,pc);
			pa->next;
		}
	}
	while(pa)
	{
		attach(pa->coef,pa->expn,pc);
		pa=pa->next;
	}
	while(pb)
	{
		attach(pb->coef,pb->expn,pc);
		pb=pb->next;
	}
	pc->next=NULL;
	//delete pa,pb,pc;
}
void output(node*h)
{
	node*p;
	p=h->next;

	cout<<p->coef<<"*x^"<<p->expn<<"+";
	p=p->next;
	while(p!=NULL)
	{
		cout<<p->coef<<"*x^"<<p->expn<<"+";
		p=p->next;
	}
	//delete p;
}
void mutiply(node*ha,node*hb,node*&hc)
{
	node*pa,*pb,*pc;
	int x,y;
	pa=ha->next;
	pb=hb->next;	
	pc=new node; 
    pc=hc;
	while(pa!=NULL)
	{
		while(pb!=NULL)
		{		
	        x=pa->coef*pb->coef;
        	y=pa->expn+pb->expn;
			attach(x,y,pc);
			pb=pb->next;
		}
		pb=hb->next;
        pa=pa->next;
	//	delete pa,pb,pc;
	}
 pc->next=NULL;
}
void main()
{
	node*ha,*hb,*hc;
	ha=create();
	hb=create(); 
	cout<<"第一个多项式为:";
	output(ha);
	cout<<"第二个多项式为:";
    output(hb);  
    addpolyn(ha,hb,hc);
	cout<<"相加的多项式为:";
    output(hc);	
    mutiply(ha,hb,hc);
	cout<<"相乘的多项式为:";
	output(hc);
	 //delete ha,hb,hc;
}






⌨️ 快捷键说明

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