多项式求和求积(链表)

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


typedef struct  OrderList{ 
    float  coef;          // 系数
    int     expn;           // 指数
    struct  OrderList *next;
} ElemType, *polynomial;

int initorderlist(polynomial &L);//初始化
polynomial plusorderlist(polynomial La,polynomial Lb);//多项式求和
polynomial cultorderlist(polynomial La,polynomial Lb);//多项式求积
int insertorderlist(polynomial L,ElemType x,int i);//插入
void seeorderlist(polynomial L);//显示
polynomial locateorderlist(polynomial L,char *expn,int &i);//定位
void checkorderlist(polynomial L);//检查,删除合并后系数为0的项

int initorderlist(polynomial &L)
{
	L=new ElemType ;
	if(L==NULL)
	{printf("空间分配失败!\n");exit(0);}
	L->expn=-1;L->next=NULL;
	return 1;
}

polynomial locateorderlist(polynomial L,int expn,int &i)
{
	polynomial p=L->next; i=1;
	while(p){
		 if(expn<p->expn)break;
		else {p=p->next;i++;}
	}
	return p;
}

int insertorderlist(polynomial L,int expn,float coef,int i)
{
	polynomial p=L,newp; int j=0;
	while(p && j<i-1){
        p=p->next; j++;}
        newp=new ElemType;
	newp->expn=expn;
	newp->coef=coef;
        newp->next=p->next;
	p->next=newp;
	return 1;
}


polynomial plusorderlist(polynomial La,polynomial Lb)
{
	polynomial p1=La->next,p2=Lb->next;
	polynomial q,L,p; int i;
	if(initorderlist(L))
	{q=L;
	while(p1&&p2)	
	{
		if((p=new  ElemType)==NULL){printf("空间分配失败!\n");getch();exit(0);}
		if(p1->expn==p2->expn)
		{
			p->coef=p1->coef+p2->coef;
			p->expn=p1->expn;
			p1=p1->next;	p2=p2->next;
		}
		else if(p1->expn<p2->expn)
		{
			p->coef=p1->coef;
			p->expn=p1->expn;  p1=p1->next;
		}
		else if(p1->expn>p2->expn)
		{
			p->coef=p2->coef;
			p->expn=p2->expn;  p2=p2->next;
		}
		p->next=NULL;
		q->next=p;
		q=p;
	}
	while(p2)
	{
		locateorderlist(L,p2->expn,i);
		insertorderlist(L,p2->expn,p2->coef,i);
		p2=p2->next;
	}
	while(p1)
	{
		locateorderlist(L,p1->expn,i);
		insertorderlist(L,p1->expn,p1->coef,i);
		p1=p1->next;
	}
	}	return L;
}


polynomial cultorderlist(polynomial La,polynomial Lb)
{	polynomial p1=La->next,p2=Lb->next;
	polynomial q,L,p,Ld;
	int i;
	initorderlist(L);initorderlist(Ld);
	q=L;
	while(p1)
	{
		while(p2)
		{
			p=new ElemType;
			p->coef=p1->coef*p2->coef;
			p->expn=p1->expn+p2->expn;
			locateorderlist(L,p->expn,i);
		insertorderlist(L,p->expn,p->coef,i);
		p2=p2->next;
		}
        p1=p1->next;
		p2=Lb->next;	
	}
	return L;
}


void seeorderlist(polynomial L)
	{
		polynomial s=L->next;
		printf("%.1fx^%d",s->coef,s->expn);
        s=s->next;
		while(s)
		{
			if(s->coef!=0)
			{
				if(s->expn==0)
					if(s->coef>0)
						printf("+%.1f",s->coef );
					else
						printf("%.1f",s->coef );
				else 
					if(s->coef>0)
						printf("+%.1fx^%d",s->coef,s->expn );
					else
						printf("%.1fx^%d",s->coef ,s->expn);
			}s=s->next;
		}printf("\n");
}

void checkorderlist(polynomial L)
{
	polynomial p,q,s;
	s=L->next;
	while(s->next)
	{   p=s;
	while(p->next)
	{
	  if((p->next->expn)==s->expn||p->next->coef==0)
	  {q=p->next;
       s->coef=(q->coef)+(s->coef);
       p->next=q->next;
       delete q;
	  }
	  p=p->next;
	}
	s=s->next;
	}
}



void main()
{
	polynomial p,La,Lb,Lc,Ld,q;
	char a[100],b[100],c[20],choice;
	int i,num,t;
	system("cls");
	printf("系统输入的多项式系数与指数(指数必须为正数)都为个位数,且如+2x^2的格式输入。\n");
	printf("----1.求和求积------2.插入项-----0.退出----\n");
	while(1)
	{
	if(initorderlist(La))
	{
	printf("请输入多项式A(以回车结束):\n");
	gets(a);
	for(i=0;a[i]!='\0';i=i+5)
	{
		q=new  ElemType;
					if(a[i]=='-')
				q->coef=-(a[i+1]-'0');
			else
				q->coef=a[i+1]-'0';
			q->expn=a[i+4]-'0';
			locateorderlist(La,q->expn,t);
			insertorderlist(La,q->expn,q->coef,t);
	}
	printf("A=");
	seeorderlist(La);
	}

	if(initorderlist(Lb))
	{
		printf("请输入多项式B(以回车结束):\n");
	gets(b);
	for(i=0;b[i]!='\0';i=i+5)
	{
		q=new  ElemType;
					if(b[i]=='-')
				q->coef=-(b[i+1]-'0');
			else
				q->coef=b[i+1]-'0';
			q->expn=b[i+4]-'0';
			locateorderlist(Lb,q->expn,t);
			insertorderlist(Lb,q->expn,q->coef,t);
	}
	printf("B=");
	seeorderlist(Lb);}

     while(1){
		printf("请输入你选项:\n");
	scanf("%d",&num);
	fflush(stdin);
	if(num<0||num>2){
		  printf("重新选择!按任意键继续!");
          getch();}
	   else break;
	   }
		switch(num)
	{
	case 0:exit(0);
	case 1:Lc=plusorderlist(La,Lb);
		checkorderlist(Lc);
		printf("A+B=");
		seeorderlist(Lc);
		Ld=cultorderlist(La,Lb);
		checkorderlist(Ld);
		printf("A*B=");
		seeorderlist(Ld);
		break;
	case 2:printf("输入要插入的项:\n");
	    gets(c);
		printf("请问您要插入到a式还是b式:\n");
	    scanf("%c",&choice);
     	for(i=0;c[i]!='\0';i=i+5)
		{
		p=new  ElemType;
					if(c[i]=='-')
				p->coef=-(c[i+1]-'0');
			else
				p->coef=c[i+1]-'0';
			p->expn=c[i+4]-'0';
		}
		if(choice=='a'){
		locateorderlist(La,p->expn,i);
	    insertorderlist(La,p->expn,p->coef,i);
		checkorderlist(La);
		seeorderlist(La);
		}
		if(choice=='b')
		{
		locateorderlist(Lb,p->expn,i);
	    insertorderlist(Lb,p->expn,p->coef,i);
		checkorderlist(Lb);
		seeorderlist(Lb);
		}break;	
	}
	}		
}





	
	  

	
	








  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用链表实现一元多项式求和的代码,需要输入多项式项数和各项系数和指数。 #include<stdio.h> #include<stdlib.h> struct PolyNode{ int coef;//系数 int expon;//指数 struct PolyNode *link;//链指针 }; typedef struct PolyNode *Polynomial; Polynomial readPoly();//读取多项式 void attach(int c, int e, Polynomial *pRear);//新建节点 Polynomial add(Polynomial p1, Polynomial p2);//多项式加法 void printPoly(Polynomial P);//输出多项式 int main(){ Polynomial P1, P2, PP; P1 = readPoly(); P2 = readPoly(); PP = add(P1, P2); printPoly(PP); return 0; } Polynomial readPoly(){ Polynomial P, Rear, t; int c, e, N; scanf("%d", &N); P = (Polynomial)malloc(sizeof(struct PolyNode));//头节点 P->link = NULL; Rear = P; while(N--){ scanf("%d %d", &c, &e); attach(c, e, &Rear);//在Rear之后插入一个节点 } t = P; P = P->link; free(t);//释放头节点 return P; } void attach(int c, int e, Polynomial *pRear){ Polynomial P; P = (Polynomial)malloc(sizeof(struct PolyNode)); P->coef = c; P->expon = e; P->link = NULL; (*pRear)->link = P; *pRear = P; } Polynomial add(Polynomial p1, Polynomial p2){ Polynomial front, rear, temp; int sum; rear = (Polynomial)malloc(sizeof(struct PolyNode)); front = rear; while(p1 && p2){ if(p1->expon > p2->expon){ attach(p1->coef, p1->expon, &rear); p1 = p1->link; } else if(p1->expon < p2->expon){ attach(p2->coef, p2->expon, &rear); p2 = p2->link; } else{ sum = p1->coef + p2->coef; if(sum) attach(sum, p1->expon, &rear); p1 = p1->link; p2 = p2->link; } } for(; p1; p1 = p1->link) attach(p1->coef, p1->expon, &rear); for(; p2; p2 = p2->link) attach(p2->coef, p2->expon, &rear); rear->link = NULL; temp = front; front = front->link; free(temp);//释放头节点 return front; } void printPoly(Polynomial P){ int flag = 0; if(!P){ printf("0 0\n"); return; } while(P){ if(!flag) flag = 1; else printf(" "); printf("%d %d", P->coef, P->expon); P = P->link; } printf("\n"); }

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值