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

📄 一元多项式相加.c

📁 一元多项式相加程序
💻 C
字号:
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<math.h>
typedef struct PNode{
	float coef;
	int expn;
	struct PNode * next;
}Pnode;

typedef struct{
    struct PNode * head;
	struct PNode * tail;
	int len;
}Polynomail;

void Initlist(Polynomail * P){
	 struct PNode * K;
     K=(struct PNode *)malloc(sizeof(Pnode));
	 P->head=K;
	 if(!P->head) printf("ERROR");
	 P->tail=P->head;
	 P->len=0;
	 P->head->next=NULL;
}

struct PNode * Gethead(Polynomail * P){
     return(P->head);
}

int Listempty(Polynomail * P){
    if(!(P->len)) return 1;
	else return 0;
}

void Delfirst(struct PNode * h,struct PNode * q,Polynomail * P){
	h->next=q->next;
	P->len=(P->len)-1;}

void Insfirst(struct PNode * h,struct PNode * q,Polynomail * P){
    q->next=h->next;
	h->next=q;
	P->len=P->len+1;
}

void Freenode(struct PNode * e){
     free(e);
}

void Append(Polynomail * Pa,Polynomail * Pb,struct PNode * ha,struct PNode * qb){
     ha->next=qb;
	 Pa->len=Pa->len+Pb->len;
	 Pa->tail=Pb->tail;
}

void creatpolyn(Polynomail * P,int m){
	 struct PNode * h;
	 struct PNode * e;
	 float ecoef;
	 int eexpn,i;
	 Initlist(P);
	 h=Gethead(P);
	 e=h;
	 e->coef=0.0;
	 e->expn=-1;
	 for(i=1;i<=m;i++){
		 printf("输入第%d个元素的系数和指数:",i);
	    printf("ecoef=");
		scanf("%f",&ecoef);
		printf("eexpn=");
		scanf("%d",&eexpn);
		printf("\n");
		e=(struct PNode *)malloc(sizeof(Pnode)); 
		if(e){
			e->coef=ecoef;
		    e->expn=eexpn;
			e->next=NULL;
		    h->next=e;
			h=e;
			P->tail=e;
			P->len=P->len+1;}
      }
}

void Addpolyn(Polynomail * Pa,Polynomail * Pb){
     struct PNode * ha;
	 struct PNode * hb;
	 struct PNode * qa;
	 struct PNode * qb;
	 Pnode a,b;
	 float sum;
	 ha=Gethead(Pa);
	 hb=Gethead(Pb);
	 qa=ha->next;
	 qb=hb->next;
	 while(qa&&qb){
		 a.coef=qa->coef;
		 a.expn=qa->expn;
		 b.coef=qb->coef;
		 b.expn=qb->expn;
		 if(a.expn<b.expn){
			ha=qa;qa=qa->next;
		 }
		 else if(a.expn==b.expn){
			sum=a.coef+b.coef;
			{
				if(sum!=0.0){
					qa->coef=sum;
					ha=qa;
					Delfirst(hb,qb,Pb);
					Freenode(qb);
					qb=hb->next;
					qa=ha->next;
				}
				else {
					Delfirst(ha,qa,Pa);
					Freenode(qa);
					Delfirst(hb,qb,Pb);
					Freenode(qb);
					qb=hb->next;
					qa=ha->next;
				}
			}
		 }
		 else if(a.expn>b.expn){
		        Delfirst(hb,qb,Pb);
				Insfirst(ha,qb,Pa);
				qb=hb->next;
				ha=ha->next;}
      }
	  if(!Listempty(Pb)) Append(Pa,Pb,ha,qb);
	  Freenode(hb);
}

void main(){
    Polynomail * Pa,* Pb;
	int ma;
	int mb;
	int i;
	struct PNode * M;
	printf("输入PA的元素个数。\n");
	printf("ma=");
	scanf("%d",&ma);
	printf("\n");
	Pa=( Polynomail *)malloc(sizeof(Polynomail));
	creatpolyn(Pa,ma);
	printf("\n");
	printf("输入PB的元素个数。\n");
	printf("mb=");
	scanf("%d",&mb);
	printf("\n");
	Pb=(Polynomail *)malloc(sizeof(Polynomail));
    creatpolyn(Pb,mb);
	printf("\n");
	Addpolyn(Pa,Pb);
	M=Pa->head->next;
	printf("相加结果(只显示系数和指数对):\n");
	for(i=1;i<=Pa->len;i++){
	     printf("%f",M->coef);
		 printf("exp");
         printf("%d",M->expn);
		 printf("+");
		 M=M->next;}
}

⌨️ 快捷键说明

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