利用链表实现一元多项式的加减

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
typedef struct z{
	double xishu;
	int zhishu;
	struct z *next;
}Z;

Z *creat();
Z *sort_del(Z *);
void print(Z *);
void print1(Z *);                       // 输出并释放内存 
Z *add(Z *,Z *);
Z *sub(Z *,Z *);

int main(){
	int a;
	Z *head1,*head2,*res;
	while(1){
		printf("***************************************\n");
		printf("            一元多项式\n");
		printf("    0: 退出\n");
		printf("    1: 输入并建立多项式\n");
		printf("    2: 输出多项式\n"); 
		printf("    3: 两个多项式相加运算并输出\n");
		printf("    4: 两个多项式相减运算并输出\n");
		printf("***************************************\n");
		printf("请选择:"); 
		scanf("%d",&a);
		getchar();
		switch(a){
			case 0: exit(0);
			case 1: head1=creat();head2=creat();    break;
			case 2: print(head1);print(head2);	    break;
			case 3:	res=add(head1,head2);print1(res);break;
			case 4: res=sub(head1,head2);print1(res);break;
		}
	}
	return 0;
}

Z *creat(){
	Z *head = NULL,*pnew,*p;
	printf("请输入多项式(系数为实数,指数为正整数):\n");
	char ch;
	double num1;
	int flag,z,flag1=0,flag2=0,num2;
	int n = 0;
	ch=getchar();
	while(ch!='\n'){
		if(ch=='-'){
	  		flag1 = 1;
	  		ch = getchar();
			continue;	
		}
		if(ch=='+'){
			ch=getchar();
			continue;
		}
		if(ch=='x'){
			num1 = 1;
		}
		else{
			num1 = 0;flag = 0;z = 0;
    		while( ch>='0' && ch<='9' || ch=='.'){        //解析实数
    			if(ch=='.'){
    				flag = 1;
        			ch = getchar();
       				continue;
    			}
    			if(flag==0){
        			num1 = num1*10+(ch-'0');
        			ch = getchar();
    			}
    			else{
    				z++;                                   //记录循环次数
        			num1 = (num1*pow(10,z)+(ch-'0'))/pow(10,z);
       				ch = getchar();
    			}    
  			}                                               //解析完毕
		}
		if(flag1==1){
    		num1 = -num1;
    		flag1 = 0;
		} 
		if(ch!='x'){
			 num2 = 0;              //指数为零 
		}
		else{
			ch = getchar();
			if(ch!='^') num2 = 1;          //指数为1,不显示^的情况 
			else{
				ch = getchar();
				if(ch=='-'){
					flag2 = 1;
					ch = getchar();
				}
				num2 = 0;
				while( ch>='0' && ch<='9' ){
      				num2 = num2*10+(ch-'0');
      				ch = getchar();
    			}
    			if(flag2){
    				num2 = -num2;
    				flag2 = 0;
				}
			}
		}
    	
    	if(fabs(num1)<1e-5) continue;   //系数为零不保存 

    	pnew = (Z *)malloc(sizeof(Z));
    	n++;
    	pnew->xishu = num1;
    	pnew->zhishu = num2;
    	if(n==1) head = pnew;
    	else p->next = pnew;
    	p = pnew;
	}
	if(head!=NULL)
		p->next = NULL;
	head = sort_del(head);
	return head;
}

void print(Z *head){
	Z *p = head;
	while(p!=NULL){
		if(p!=head)
			if(p->xishu > 0)
				printf("+");
		if(p->zhishu == 0){
			if(fabs(p->xishu - (int)p->xishu)<1e-5)
				printf("%.f",p->xishu);
			else
				printf("%.1f",p->xishu);
		}
		else if(p->zhishu ==1){
			if(fabs(p->xishu - (int)p->xishu)<1e-5){
				if(fabs(fabs(p->xishu)-1)<1e-5){
					if(p->xishu<0) printf("-x");
					else printf("x");
				}
				else
					printf("%.fx",p->xishu);
			}
			else
				printf("%.1fx",p->xishu);
		}
		else{
			if(fabs(p->xishu - (int)p->xishu)<1e-5){
				if(fabs(fabs(p->xishu)-1)<1e-5)
					if(p->xishu<0) printf("-x^%d",p->zhishu);
					else printf("x^%d",p->zhishu);
				else
					printf("%.fx^%d",p->xishu,p->zhishu);
			}
			else
				printf("%.1fx^%d",p->xishu,p->zhishu);
		}
		p = p->next;
	}
	if(head==NULL) printf("0");
	printf("\n");
}

void print1(Z *head){
	Z *p = head,*p1;
	while(p!=NULL){
		if(p!=head)
			if(p->xishu > 0)
				printf("+");
		if(p->zhishu == 0){
			if(fabs(p->xishu - (int)p->xishu)<1e-5)
				printf("%.f",p->xishu);
			else
				printf("%.1f",p->xishu);
		}
		else if(p->zhishu ==1){
			if(fabs(p->xishu - (int)p->xishu)<1e-5){
				if(fabs(fabs(p->xishu)-1)<1e-5){
					if(p->xishu<0) printf("-x");
					else printf("x");
				}
				else
					printf("%.fx",p->xishu);
			}
			else
				printf("%.1fx",p->xishu);
		}
		else{
			if(fabs(p->xishu - (int)p->xishu)<1e-5){
				if(fabs(fabs(p->xishu)-1)<1e-5){
					if(p->xishu<0) printf("-x^%d",p->zhishu);
					else printf("x^%d",p->zhishu);
				}
				else
					printf("%.fx^%d",p->xishu,p->zhishu);
			}
			else
				printf("%.1fx^%d",p->xishu,p->zhishu);
		}
		p1 = p;
		p = p->next;
		free(p1);
	}
	if(head==NULL) printf("0");
	printf("\n");
}

Z *add(Z *head1,Z *head2){
	Z *res = NULL,*p1=head1,*p2=head2,*p,*pnew;
	int n = 0;
	while(p1!=NULL && p2!=NULL){
		if(p1->zhishu == p2->zhishu){
			if(fabs(p1->xishu+p2->xishu)<1e-5){
				p1 = p1->next;
				p2 = p2->next;
				continue;
			}
			else{
				n++;
				pnew = (Z *)malloc(sizeof(Z));
				pnew->xishu = p1->xishu+p2->xishu;
				pnew->zhishu = p1->zhishu;
				if(n==1) res = pnew;
				else p->next = pnew;
				p = pnew;
				p1 = p1->next;
				p2 = p2->next;
			}
		}
		else if(p1->zhishu < p2->zhishu){
			n++;
			pnew = (Z *)malloc(sizeof(Z));
			pnew->xishu = p1->xishu;
			pnew->zhishu = p1->zhishu;
			if(n==1) res = pnew;
			else p->next = pnew;
			p = pnew;
			p1 = p1->next;
		}
		else{
			n++;
			pnew = (Z *)malloc(sizeof(Z));
			pnew->xishu = p2->xishu;
			pnew->zhishu = p2->zhishu;
			if(n==1) res = pnew;
			else p->next = pnew;
			p = pnew;
			p2 = p2->next;
		}
	}
	while(p1!=NULL){
		n++;
		pnew = (Z *)malloc(sizeof(Z));
		pnew->zhishu = p1->zhishu;
		pnew->xishu = p1->xishu;
		if(n==1) res = pnew;
		else p->next = pnew;
		p = pnew;
		p1 = p1->next;
	}
	while(p2!=NULL){
		n++;
		pnew = (Z *)malloc(sizeof(Z));
		pnew->zhishu = p2->zhishu;
		pnew->xishu = p2->xishu;
		if(n==1) res = pnew;
		else p->next = pnew;
		p = pnew;
		p2 = p2->next;
	}
	return res;
}

Z *sub(Z *head1,Z *head2){
	Z *res = NULL,*p1=head1,*p2=head2,*p,*pnew;
	int n = 0;
	while(p1!=NULL && p2!=NULL){
		if(p1->zhishu == p2->zhishu){
			if(fabs(p1->xishu-p2->xishu)<1e-5){
				p1 = p1->next;
				p2 = p2->next;
				continue;
			}
			else{
				n++;
				pnew = (Z *)malloc(sizeof(Z));
				pnew->xishu = p1->xishu-p2->xishu;
				pnew->zhishu = p1->zhishu;
				if(n==1) res = pnew;
				else p->next = pnew;
				p = pnew;
				p1 = p1->next;
				p2 = p2->next;
			}
		}
		else if(p1->zhishu < p2->zhishu){
			n++;
			pnew = (Z *)malloc(sizeof(Z));
			pnew->xishu = p1->xishu;
			pnew->zhishu = p1->zhishu;
			if(n==1) res = pnew;
			else p->next = pnew;
			p = pnew;
			p1 = p1->next;
		}
		else{
			n++;
			pnew = (Z *)malloc(sizeof(Z));
			pnew->xishu = -p2->xishu;
			pnew->zhishu = p2->zhishu;
			if(n==1) res = pnew;
			else p->next = pnew;
			p = pnew; 
			p2 = p2->next;
		}
	}
	while(p1!=NULL){
		n++;
		pnew = (Z *)malloc(sizeof(Z));
		pnew->zhishu = p1->zhishu;
		pnew->xishu = p1->xishu;
		if(n==1) res = pnew;
		else p->next = pnew;
		p = pnew;
		p1 = p1->next;
	}
	while(p2!=NULL){
		n++;
		pnew = (Z *)malloc(sizeof(Z));
		pnew->zhishu = p2->zhishu;
		pnew->xishu = -p2->xishu;
		if(n==1) res = pnew;
		else p->next = pnew;
		p = pnew;
		p2 = p2->next;
	}
	return res;
}

Z *sort_del(Z *head)
{
	int t1;
	double t2;
	Z *p1 = head,*p2;
	if(head==NULL) return head;
	while(p1->next != NULL)
	{
		p2 = p1->next;
    	while(p2 != NULL)
    	{
			if(p1->zhishu > p2->zhishu)
    		{
        		t1 = p1->zhishu;
        		p1->zhishu = p2->zhishu;
       	 		p2->zhishu = t1;
        
        		t2 = p1->xishu;
        		p1->xishu = p2->xishu;
        		p2->xishu = t2;
    		}
      		p2 = p2->next;
    	}
    	p1 = p1->next;
	}
	p1 = head->next;
  	p2 = head;
  	while(p1!=NULL){
		if(p1->zhishu == p2->zhishu){
			p2->xishu += p1->xishu;
			p2->next = p1->next;
			free(p1);
			p1 = p2->next;
		}
		else{
			p2 = p1;
			p1 = p1->next;
		}
	}
	p1 = head;
	while(p1->xishu==0 && p1!=NULL){
		p1 = p1->next;
		head = p1;
	}
	p1 = head->next;
	p2 = head;
  	while(p1!=NULL){
  		if(p1->xishu == 0){
  			p2->next = p1->next;
  			free(p1);
  			p1 = p2->next;
		}
		else{
			p2 = p1;
			p1 = p1->next;
		}
	}
	return head;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值