一元多项式的表示及相加 数据结构

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/liying_1234/article/details/46403297
#include<stdio.h>
#include<malloc.h>
#include<math.h>

#define TRUE   1
#define FALSE  0
#define OK     1
#define ERROR  0


typedef struct  DataType
{
    float coef;
	int   expn;
}ELemType;

typedef struct List
{
    ELemType   data;
	struct List *prior;
	struct List *next;
}LNode,*LinkList;

int  CreatePoly(LinkList L,int m)//创建多项式
{
   LinkList  p;
   LinkList q;
   int i;
   float f=0.0;
   q=L;

   L->prior=L->next=L;
   for(i=0;i<m;i++)
   {
       p=(LinkList)malloc(sizeof(LNode));
	   printf("请输入第%d项的系数和指数(用空格隔开):",i+1);
	   scanf("%f %d",&p->data.coef,&p->data.expn);
	   q->next=p;
	   p->prior=q;
	   q=q->next;
   }
   q->next=L;
   L->prior=q;
   return OK;
}

int PolynLength(LinkList L)//求多项式的项数
{
    int i;
	LinkList p;
	p=L;
	i=0;
	while(p->next!=L)
	{
	    i++;
		p=p->next;
	}
	return i;
}

void AddPolyn(LinkList La,LinkList Lb)//两个多项式的相加
{
    LinkList Pa;
	LinkList Pb;
	LinkList tmp;
	Pa=La->next;
	Pb=Lb->next;
	while(Pa!=La&&Pb!=Lb)
	{
	     if(Pa->data.expn<Pb->data.expn)
			 Pa=Pa->next;
		 else if(Pa->data.expn>Pb->data.expn)
		 {
		       Pb->prior=Pa->prior;
			   Pa->prior->next=Pb;
			   Pb->next=Pa;
			   Pa->prior=Pb;
			   Pb=Pb->next;
		 }
		 else
		 {
			 if((Pa->data.coef+Pb->data.coef)!=0)
			 {
			      Pa->data.coef=Pa->data.coef+Pb->data.coef;
                  Pa=Pa->next;
				  Pb=Pb->next;
			 }
		 }

	}
	if(Pa==La&&Pb==Lb)
	{}
	else if(Pa==La)
	{
	   Pa->prior->next=Pb;
	   Pb->prior=Pa->prior;
	   while(Pb!=Lb)
	   {
	     Pb=Pb->next; 
	   }
	   Pb->prior->next=La;
	   La->prior=Pb->prior;
	}
	else
	{}
}

double GetPolynValue(LinkList L,float x)//求多项式的值,变量为x
{
     LinkList p;
	 double sum=0.0;
	 p=L;
	 while(p->next!=L)
	 {
	    sum+=(p->next->data.coef)*pow(x,p->next->data.expn);
		p=p->next;
	 }
	 return sum;
}

void DestroyPolyn(LinkList L)//销毁多项式链表
{
  LinkList p;
  LinkList tmp;
  p=L->next;
  while(p)
  {
     tmp=p->next;
	 free(p);
	 p=tmp;
  }
}

void PrintPolyn(LinkList L)//输出多项式
{
        LinkList p;
		float f;
		p=L;
		while(p->next!=L)
		{
		  printf("%.2f*x^%d",p->next->data.coef,p->next->data.expn);
          f=p->next->data.coef;
		  p=p->next;
		  if(p->next!=L&&f>0)
			  printf("+");
		}
		printf("\n");
}
void main()
{
      LNode La;
	  LNode Lb;
	  int length;
	  double value;
	  float x;
	  int m;
	  int n;
	  printf("请输入第一个多项式的项数:");
	  scanf("%d",&m);
	  CreatePoly(&La,m);
	  length=PolynLength(&La);
	  printf("The length of the Poly is :%d\n",length);
	  PrintPolyn(&La);
      
      printf("请输入第二个多项式的项数:");
	  scanf("%d",&n);
	  CreatePoly(&Lb,n);
	  length=PolynLength(&Lb);
	  printf("The length of the Poly is :%d\n",length);
	  PrintPolyn(&Lb);

	  printf("两多项式相加得到:");
	  AddPolyn(&La,&Lb);
	  PrintPolyn(&La);
	  length=PolynLength(&La);
	  printf("The length of the Poly is:&d\n",length);

	  printf("输入多项式中变量的值:");
	  scanf("%f",&x);
      value=GetPolynValue(&La,x);
      printf("多项式的值= %.2f\n",value);<span style="font-family: Arial, Helvetica, sans-serif;">}</span>

展开阅读全文

没有更多推荐了,返回首页