多项式求和

#include <stdio.h>
#include <stdlib.h>

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;
} 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

哩吇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值