程序代码:#include<stdio.h>
#include<conio.h>
#include<math.h>
#define OVERFLOW -2
#define OK 1
typedef struct LNode
{
float coef;
int expn;
struct LNode *next;
}LNode,*Linklist;
int initlist(LNode *L)
{ L=(LNode *)malloc(sizeof(LNode));
if(!L)exit(OVERFLOW);
L->next=NULL;
return OK;
}
int Creatlist(LNode *L ,int n)
{ LNode *p,*q;
int i;
q=L;
for(i=0;i<n;i++)
{
p=(LNode *)malloc(sizeof(LNode));
printf("Enter the num of coef and tne num of expn:");
scanf("%f,%d",&p->coef,&p->expn);
q->next=p;
q=p;
}
p->next=NULL;
return OK;
}
LNode* selsort(LNode *L) {
LNode *g, *p, *q;
float f;
int i, fini = 1;
if(!L) return NULL;
for(g = L->next;g->next&&fini;g = g->next) {
fini = 0;
for(p = L->next,q = L->next->next;q;p = p->next,q = q->next)
if (p->expn < q->expn) {
f = p->coef;i = p->expn;
p->coef = q->coef;p->expn = q->expn;
q->coef = f;q->expn = i;
fini = 1;
}
}
for(g = L->next,p = g->next;p;)
if(g->expn==p->expn) {
g->coef += p->coef;
g->next = p->next;
q = p;
p = p->next;
free(q);
}
else if(g->next) {
g = g->next;
p = p->next;
}
return L;
}
void PrintfPoly(LNode *L) {
LNode *q = L->next;
if(!q) {
putchar('0');
return;
}
if(q->coef!=1) {
printf("%g",q->coef);
if(q->expn==1) putchar('X');
else if(q->expn) printf("X^%d",q->expn);
}
else if(!q->expn) putchar('1');
else if(q->expn==1) putchar('X');
else printf("X^%d",q->expn);
q = q->next;
while (q) {
if(q->coef > 0) putchar('+');
if(q->coef!=1) {
printf("%g",q->coef);
if(q->expn==1) putchar('X');
else if(q->expn) printf("X^%d",q->expn);
}
else if(!q->expn) putchar('1');
else if(q->expn==1) putchar('X');
else printf("X^%d",q->expn);
q = q->next;
}
}
Compare(LNode *L, LNode *b) {
if (L->next->expn < b->expn) return -1;
if (L->next->expn > b->expn) return 1;
return 0;
}
LNode* APolyn(LNode *L, LNode *Pb) {
LNode *h, *qa = L, *qb = Pb, *p, *q;
float sum;
h=p = (LNode*)malloc(sizeof(LNode));
p->next= NULL;
while (qa && qb) {
switch (Compare(qa,qb)) {
case -1:
p->next= qb;
p = qb;
qb = qb->next;
break;
case 0:
sum = qa->coef + qb->coef;
if (sum != 0.0) {
p ->next= qa;
qa->coef = sum;
p= qa;
qa = qa->next;
}
else {
q = qa;
qa = qa->next;
free(q);
}
q = qb;
qb = qb->next;
free(q);
break;
case 1:
p->next= qa;
p = qa;
qa = qa->next;
break;
}
}
if (qa) p->next= qa;
if (qb) p->next= qb;
q=h;
h=h->next;
free(q);
return h;
}
LNode* A(LNode *L, LNode *Pb) {
LNode *Pa;
PrintfPoly(L);
if(Pb->next && Pb->next->coef>0) printf("+");
PrintfPoly(Pb);
Pa= APolyn(L,Pb);
printf(" = ");
selsort(Pa);
PrintfPoly(Pa);
return Pa;
}
void Addlist(LNode *L ,int X)
{
float sum=0.00;
LNode *p;
p=L->next;
while(p!=NULL)
{
sum+=(p->coef)*pow(X,p->expn);
p=p->next;
}
printf("sum=%f",sum);
}
void main()
{
int X,n,m;
LNode *L,*Pa ,*Pb;
initlist(L);
printf("Enter how many number! n=");
scanf("%d",&n);
Creatlist(L,n);
selsort(L);
PrintfPoly(L);printf("\n");
initlist(Pb);
printf("Enter how many number! m=");
scanf("%d",&m);
Creatlist(Pb,m);
selsort(Pb);
PrintfPoly(Pb);printf("\n");
Pa=A(L,Pb);
printf("\n");
printf("Enter the number X=");
scanf("%d",&X);
Addlist(Pa,X);
getch();
}
这个程序是求两个多项式相加。这个程序看起来有点长,但是我自己感觉 错误应该在 LNode* APolyn(LNode *L, LNode *Pb) 这一块#include<conio.h>
#include<math.h>
#define OVERFLOW -2
#define OK 1
typedef struct LNode
{
float coef;
int expn;
struct LNode *next;
}LNode,*Linklist;
int initlist(LNode *L)
{ L=(LNode *)malloc(sizeof(LNode));
if(!L)exit(OVERFLOW);
L->next=NULL;
return OK;
}
int Creatlist(LNode *L ,int n)
{ LNode *p,*q;
int i;
q=L;
for(i=0;i<n;i++)
{
p=(LNode *)malloc(sizeof(LNode));
printf("Enter the num of coef and tne num of expn:");
scanf("%f,%d",&p->coef,&p->expn);
q->next=p;
q=p;
}
p->next=NULL;
return OK;
}
LNode* selsort(LNode *L) {
LNode *g, *p, *q;
float f;
int i, fini = 1;
if(!L) return NULL;
for(g = L->next;g->next&&fini;g = g->next) {
fini = 0;
for(p = L->next,q = L->next->next;q;p = p->next,q = q->next)
if (p->expn < q->expn) {
f = p->coef;i = p->expn;
p->coef = q->coef;p->expn = q->expn;
q->coef = f;q->expn = i;
fini = 1;
}
}
for(g = L->next,p = g->next;p;)
if(g->expn==p->expn) {
g->coef += p->coef;
g->next = p->next;
q = p;
p = p->next;
free(q);
}
else if(g->next) {
g = g->next;
p = p->next;
}
return L;
}
void PrintfPoly(LNode *L) {
LNode *q = L->next;
if(!q) {
putchar('0');
return;
}
if(q->coef!=1) {
printf("%g",q->coef);
if(q->expn==1) putchar('X');
else if(q->expn) printf("X^%d",q->expn);
}
else if(!q->expn) putchar('1');
else if(q->expn==1) putchar('X');
else printf("X^%d",q->expn);
q = q->next;
while (q) {
if(q->coef > 0) putchar('+');
if(q->coef!=1) {
printf("%g",q->coef);
if(q->expn==1) putchar('X');
else if(q->expn) printf("X^%d",q->expn);
}
else if(!q->expn) putchar('1');
else if(q->expn==1) putchar('X');
else printf("X^%d",q->expn);
q = q->next;
}
}
Compare(LNode *L, LNode *b) {
if (L->next->expn < b->expn) return -1;
if (L->next->expn > b->expn) return 1;
return 0;
}
LNode* APolyn(LNode *L, LNode *Pb) {
LNode *h, *qa = L, *qb = Pb, *p, *q;
float sum;
h=p = (LNode*)malloc(sizeof(LNode));
p->next= NULL;
while (qa && qb) {
switch (Compare(qa,qb)) {
case -1:
p->next= qb;
p = qb;
qb = qb->next;
break;
case 0:
sum = qa->coef + qb->coef;
if (sum != 0.0) {
p ->next= qa;
qa->coef = sum;
p= qa;
qa = qa->next;
}
else {
q = qa;
qa = qa->next;
free(q);
}
q = qb;
qb = qb->next;
free(q);
break;
case 1:
p->next= qa;
p = qa;
qa = qa->next;
break;
}
}
if (qa) p->next= qa;
if (qb) p->next= qb;
q=h;
h=h->next;
free(q);
return h;
}
LNode* A(LNode *L, LNode *Pb) {
LNode *Pa;
PrintfPoly(L);
if(Pb->next && Pb->next->coef>0) printf("+");
PrintfPoly(Pb);
Pa= APolyn(L,Pb);
printf(" = ");
selsort(Pa);
PrintfPoly(Pa);
return Pa;
}
void Addlist(LNode *L ,int X)
{
float sum=0.00;
LNode *p;
p=L->next;
while(p!=NULL)
{
sum+=(p->coef)*pow(X,p->expn);
p=p->next;
}
printf("sum=%f",sum);
}
void main()
{
int X,n,m;
LNode *L,*Pa ,*Pb;
initlist(L);
printf("Enter how many number! n=");
scanf("%d",&n);
Creatlist(L,n);
selsort(L);
PrintfPoly(L);printf("\n");
initlist(Pb);
printf("Enter how many number! m=");
scanf("%d",&m);
Creatlist(Pb,m);
selsort(Pb);
PrintfPoly(Pb);printf("\n");
Pa=A(L,Pb);
printf("\n");
printf("Enter the number X=");
scanf("%d",&X);
Addlist(Pa,X);
getch();
}
求和之后 总是多出 一项 很大的数?
