一元多项式的加法与乘法运算

题意理解

一元多项式的加法与乘法运算_第1张图片

一元多项式的加法与乘法运算_第2张图片

多项式的表示

有两种表示方法:

数组:                                           链表:
优点:编程简单、调试容易             优点:动态性强
缺点:需要事先确定数组大小          缺点:编程略为复杂、调试比较困难

数据结构设计

typedef struct PolyNode *Polynomial;
struct PolyNode {
  int coef;
  int expon;  /*指数*/
  Polynomial link;
};

程序框架搭建

int main()
{
读入多项式1
读入多项式2
乘法运算并输出
加法运算并输出
return 0;
}

代码

#include 
#include 
#include 
typedef struct PolyNode *Polynomial;
struct PolyNode {
  int coef;
  int expon;  /*指数*/
  Polynomial link;
};

void Attach(int c, int e, Polynomial* pRear)
{
	Polynomial P;
	P = (struct PolyNode*)malloc(sizeof(struct PolyNode));
	P->coef = c;  /*对新节点赋值*/ 
	P->expon = e;
	P->link = NULL;
	(*pRear)->link = P;
	(*pRear) = P;  
}
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); /* 将当前项插入多项式尾部 */
  }
  t = P; 
  P = P->link; 
  free(t); /* 删除临时生成的头结点 */
  return P;
}


Polynomial Add( Polynomial P1, Polynomial P2 )/*多项式的加法*/ 
{
	Polynomial P,Rear,t;
	P = (Polynomial)malloc(sizeof(struct PolyNode));
	P->link = NULL;
	Rear = P;
	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{
			if(P1->coef+P2->coef)  /*判断系数和是否为0*/ 
			   Attach(P1->coef+P2->coef,P1->expon,&Rear);
			P1 = P1->link;
			P2 = P2->link;
		}
	}
	while(P1)
	{
		Attach(P1->coef,P1->expon,&Rear);
	    P1 = P1->link;
	}
	while(P2)
	{
		Attach(P2->coef,P2->expon,&Rear);
		P2 = P2->link;
	}
	t = P;
	P = P->link;
	free(t);
	return P;
}

Polynomial Mult( Polynomial P1, Polynomial P2 )/*多项式的乘法*/
{
    Polynomial P, Rear, t1, t2, t;
    int c,e;
    if (P1==NULL || P2==NULL) return NULL;
    t1 = P1;
    P = (Polynomial)malloc(sizeof(struct PolyNode));
    P->link = NULL;
    while(t1)
    {
    	Rear = P;
    	t2 = P2;
    	while(t2)
    	{
    		c = t1->coef * t2->coef;
            e = t1->expon + t2->expon;
		    while (Rear->link && Rear->link->expon > e)
                Rear = Rear->link;
			if(Rear->link && Rear->link->expon == e)
			{
				if (Rear->link->coef + c)
                   Rear->link->coef += c;
                else {
                       t = Rear->link;
                       Rear->link = t->link;
                       free(t);
                     }
			}
			else
			{
			   t = (Polynomial)malloc(sizeof(struct PolyNode));
               t->coef = c; 
			   t->expon = e;
               t->link = Rear->link;
               Rear->link = t; 
			   Rear = Rear->link;
			}
			t2 = t2->link;		
		}
		t1 = t1->link;
	}
	t = P;
	P = P->link; 
	free(t);
    return P;
} 
 

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

int main()
{
  Polynomial P1, P2, PP, PS;
  P1 = ReadPoly();
  P2 = ReadPoly();
  PP = Mult( P1, P2 );
  PrintPoly( PP );
  PS = Add( P1, P2 );
  PrintPoly( PS );
  return 0;
}



你可能感兴趣的:(数据结构)