数据结构 - 一元多项式相加减(C语言)

通常情况下,一元多项式可写成: an(x) = a1x^e1 + a2x^e2 + a3x^e3 + ... + amx^em (数字,m为下标),其中:pi是指数为ei的项的非零系数,0<= e1 < e2 <...< em <=n.可以用线性表表示为:((p1,e1),(p2,e2),...(pm,em)).

例如:

An(x) = 5x^3 - 2x^12 + 13x^99。可用线性表:((5,3),(-2,12),(13,99))表示。

为了操作方便,采用单链表作存储结构,结点有三个域:系数,指数,指针。存储结构如下:

typedef struct Polynomial
{
	float coef;		//系数.
	int expn;		//指数:常见的次幂都是整数,所以这里不考虑小数.
	struct Polynomial *next; 
}Polynomial,*Polyn;
算法实现的具体思路在这里就不敖述了,下面放代码,哪里看不懂的可以评论区评论,看到第一时间回复。完整代码如下:

#include 
#include 

typedef struct Polynomial
{
	float coef;		//系数.
	int expn;		//指数:常见的次幂都是整数,所以这里不考虑小数.
	struct Polynomial *next; 
}Polynomial,*Polyn;

Polyn Create_Polyn();	//尾插法建立多项式.

void Print_Polyn(Polynomial *head);	//打印多项式.

Polyn Add_Polyn(Polynomial *pa,Polynomial *pb);	//多项式相加. 

Polyn Suberact_Polyn(Polynomial *pa,Polynomial *pb);	//多项式相减. 
 
int main()
{
	Polynomial *pa,*pb,*pc,*pd;
	
	/*--------------------一元多项式的创建---------------------*/ 
	printf("请输入多项式pa的各项系数和指数(输入0结束): \n");
	pa=Create_Polyn();
	printf("请输入多项式pb的各项系数和指数(输入0结束): \n");
	pb=Create_Polyn();
	
	/*------------------一元多项式的格式输出-------------------*/
	printf("\n多项式: pa = ");
	Print_Polyn(pa);
	printf("\n多项式: pb = ");
	Print_Polyn(pb);
	
	/*--------------------一元多项式相加-----------------------*/
	printf("\n多项式pa和pb的和为:pa+pb = ");
	pc=Add_Polyn(pa,pb);
	Print_Polyn(pc);
	
	/*--------------------一元多项式相减-----------------------*/
	printf("\n多项式pa和pb的差为:pa-pb = ");
	pd=Suberact_Polyn(pa,pb);
	Print_Polyn(pd);
	printf("\n");
	
	return 0;
}

Polyn Create_Polyn()	//尾插法建立多项式.
{
	Polynomial *head,*rear,*s;
	int c,e;
	head = (Polyn)malloc(sizeof(Polynomial));
	rear = head;				/*rear始终指向单链表的尾部,便于尾插法建表。*/ 
	scanf("%d,%d",&c,&e);	 	/*键入多项式的系数和指数项*/
	while(c!=0){				/*若c=0,代表多项式输入结束*/ 
		s = (Polyn)malloc(sizeof(Polynomial));
		s->coef = c;
		s->expn = e;
		rear->next = s;		 /*尾插*/
		rear = s;
		scanf("%d,%d",&c,&e); 
	} 
	rear->next = NULL;	/*将表中最后一个结点的next置为NULL*/
	
	return head; 
} 

void Print_Polyn(Polynomial *head)	/*打印多项式.*/
{
	Polyn q=head->next;
	int flag=1;
	if(!q){
		putchar('0');
		printf("\n");
		return;
	}
	while(q){
		if(q->coef > 0 && flag!=1){		/*多项式中某一项系数大于0,输出+号*/ 
			putchar('+');
		}
		if(q->coef!=1 && q->coef!=-1){		/*多项式系数不是正负1*/ 
			printf("%g",q->coef);			/*%g表示以%f%e中较短的输出宽度输出单双精度实数*/ 
			if(q->expn==1) putchar('X');	/*若指数为1,输出X*/ 
			else if(q->expn) printf("X^%d",q->expn); /*指数不为1,输出x^%d格式*/ 
		}
		else{
			if(q->coef==1){				/*系数为1*/ 
				if(!q->expn) putchar('1');
				else if(q->expn==1) putchar('X');
				else printf("X^%d",q->expn);
			}
			if(q->coef==-1){			/*系数为-1*/ 
				if(!q->expn) printf("-1");
				else if(q->expn==1) printf("-X");
				else printf("-X^%d",q->expn);
			}
		}
		q=q->next;
		flag++;
	}
	printf("\n");
}

Polyn Add_Polyn(Polynomial *pa,Polynomial *pb)	//多项式相加.
{
	Polyn qa=pa->next;
	Polyn qb=pb->next;
	Polyn headc,pc,qc;
	pc = (Polyn)malloc(sizeof(Polynomial));		/*单链表pc用来存放pa,pb的和*/
	pc->next = NULL;
	headc = pc;
	while(qa!=NULL && qb!=NULL)	/*当两个多项式均未扫描结束时*/
	{
		qc = (Polyn)malloc(sizeof(Polynomial));
		if(qa->expn < qb->expn) /*pa的指数项小于pb的指数项*/
		{
			qc->coef = qa->coef;
			qc->expn = qa->expn;
			qa = qa->next;
		}
		else if(qa->expn == qb->expn) /*指数项相同时*/
		{
			qc->coef = qa->coef + qb->coef;
			qc->expn = qa->expn;		/*当然也可以是qb->expn*/
			qa=qa->next;
			qb=qb->next; 
		}
		else{				/*pb的指数项小于pa的指数项*/ 
			qc->coef=qb->coef;
			qc->expn=qb->expn;
			qb=qb->next;
		}
		if(qc->coef!=0){
			qc->next = pc->next;
			pc->next = qc;
			pc = qc;
		}
		else free(qc);
	}
	while(qa!=NULL){ 	/*pa存在剩余项,将剩余项插入到pc当中*/ 
		qc = (Polyn)malloc(sizeof(Polynomial));
		qc->coef = qa->coef;
		qc->expn = qa->expn;
		qa = qa->next;
		qc->next = pc->next;
		pc->next = qc;
		pc = qc;
	}
	while(qb!=NULL){	/*pb存在剩余项,将剩余项插入到pc当中*/ 
		qc = (Polyn)malloc(sizeof(Polynomial));
		qc->coef = qb->coef;
		qc->expn = qb->expn;
		qb = qb->next;
		qc->next = pc->next;
		pc->next = qc;
		pc = qc;
	}
	return headc;
}

Polyn Suberact_Polyn(Polynomial *pa,Polynomial *pb)	//多项式相减.
{
	Polyn h=pb;
	Polyn p=pb->next;
	Polyn pd;
	while(p){			/*pa-pb就等于pa+(-pb),所以将pb多项式的各项符号循环遍历变号*/ 
		p->coef*=-1;
		p=p->next;
	}
	
	pd=Add_Polyn(pa,h);		/*利用已创函数Add_Polyn()执行pa+(-pb),即减法运算*/
	 
	for(p=h->next;p;p=p->next){	/*执行完pa+(-pb)后,循环遍历,将pb的符号全部更改为之前的状态*/ 
		p->coef*=-1;
	}
	return pd;
}
随便输入一组数据,显示结果如下:

数据结构 - 一元多项式相加减(C语言)_第1张图片


谢谢浏览,如果哪里写的有问题,感谢指出。

参考:数据结构与算法 - 王曙燕主编版。

你可能感兴趣的:(数据结构)