# 一元多项式的表示及加减乘除运算

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>

typedef struct polyn
{
float coef;
int expn;
struct polyn* next;

#include "test.h"

{
PolyNode *p,*q,*s;
int expn2;
float coef2;
{
return NULL;
}
do
{
printf("输入系数coef(系数和指数都为0结束)");
scanf("%f",&coef2);
printf("输入指数数exp(系数和指数都为0结束)");
scanf("%d",&expn2);
if((long)coef2 == 0 && expn2 == 0)
{
break;
}
if(!s)
{
return NULL;
}
s->expn = expn2;
s->coef = coef2;
while(q && expn2 < q->expn)
{
p = q;
q = q->next ;
}
if(q == NULL || expn2 > q->expn)
{
p->next = s;
s->next = q;
}
else
{
q->coef += coef2;
}
}while(1);
}

{
while(p)
{
printf("%1.1f",p->coef);
if(p->expn)
{
printf("*x^%d",p->expn);
}
if(p->next && p->next->coef > 0)
{
printf("+");
}
p = p->next ;
}
}

{
{
return NULL;
}
L2 = Reverse(L2);
pa = L1->next ;
pb = L2->next ;
while(pa != NULL && pb != NULL)
{
if(pa->expn == pb->expn)
{
if(!u)
{
return NULL;
}
u->coef = pa->coef + pb->coef ;
u->expn = pa->expn ;
pa = pa->next ;
pb = pb->next ;
u->next = pc->next ;
pc->next = u;
pc = u;
}
else if(pa->expn > pb->expn)
{
if(!u)
{
return NULL;
}
u->coef = pa->coef ;
u->expn = pa->expn ;
pa = pa->next ;
u->next = pc->next ;
pc->next = u;
pc = u;
}
else
{
if(!u)
{
return NULL;
}
u->coef = pb->coef ;
u->expn = pb->expn ;
pb = pb->next ;
u->next = pc->next ;
pc->next = u;
pc = u;
}
}
L2 = Reverse(L2);
}

{
{
return NULL;
}
pa = L1->next ;
pb = L2->next ;
while(pa != NULL && pb != NULL)
{
if(pa->expn == pb->expn)
{
if(!u)
{
return NULL;
}
u->coef = pa->coef - pb->coef ;
u->expn = pa->expn ;
pa = pa->next ;
pb = pb->next ;
u->next = pc->next ;
pc->next = u;
pc = u;
}
else if(pa->expn > pb->expn)
{
if(!u)
{
return NULL;
}
u->coef = pa->coef ;
u->expn = pa->expn ;
pa = pa->next ;
u->next = pc->next ;
pc->next = u;
pc = u;
}
else
{
if(!u)
{
return NULL;
}
u->coef = pb->coef ;
u->expn = pb->expn ;
pb = pb->next ;
u->next = pc->next ;
pc->next = u;
pc = u;
}
}
}

{
PolyNode *q,*r,*p = NULL;
while(q)
{
r = q->next ;
q->next = p;
p = q;
q = r;
}
}

{
int k,maxExp;
float coef;
{
return NULL;
}
if(L1->next != NULL && L2->next != NULL)
{
maxExp = L1->next->expn +L2->next->expn ;
}
else
{
}
L2 = Reverse(L2);
for(k = maxExp;k >= 0;k--)
{
pa = L1->next ;
while(pa != NULL && pa->expn > k)
{
pa = pa->next ;
}
pb = L2->next ;
while(pb != NULL && pa != NULL && pa->expn+pb->expn < k)
{
pb= pb->next ;
}
coef = 0.0;
while(pa != NULL && pb != NULL)
{
if(pa->expn +pb->expn == k)
{
coef += pa->coef *pb->coef ;
pa = pa->next ;
pb = pb->next ;
}
else if(pa->expn +pb->expn > k)
{
pa = pa->next ;
}
else
{
pb = pb->next ;
}
}
if(coef != 0.0)
{
u->coef = coef;
u->expn = k;
u->next = pc->next ;
pc->next = u;
pc = u;
}
}
L2 = Reverse(L2);
}

#include "test.h"

int main(void)
{
A = CreatePolyn();
printf("A(x) =");
OutPut(A);
printf("\n");
B = CreatePolyn();
printf("B(x) =");
OutPut(B);
printf("\n");
C = MultiplyPolyn(A,B);
printf("C(x) = A(x)*B(x) =");
OutPut(C);
printf("\n");
printf("D(x) = A(x)+B(x) =");
OutPut(D);
printf("\n");
E = Subtraction(A,B);
printf("E(x) = A(x)-B(x) =");
OutPut(E);
printf("\n");
return 0;
}


• 本文已收录于以下专栏：

举报原因： 您举报文章：一元多项式的表示及加减乘除运算 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)