主题:自己写的作业题3,不知道哪里错了……请帮忙看看
三 二项式
#include<stdio.h>
#include<math.h>
#include<malloc.h>
typedef struct
{float coef;/*系数*/
int exp;/*指数*/
}ElemType;
typedef struct LNode
{float coef; /*系数*/
int exp; /*指数*/
struct Lnode *next; /*指针域*/
}LNode,*linklist;
void creatpoly(LinkList * L,int n)/*产生二项式链表*/
{int i;
LinkList p,q;
*L=(LinkList)malloc(sizeof(LNode));/*建立一个新结点*/
(*L)->next=NULL;
q=*L;
printf("请输入%d个数",2*n);
for(i=1;i<=n;i++)
{p=(LinkList)malloc(sizeof(LNode));
scanf("%f%d",&p->data.coef,&p->data.exp);
q->next=p;/*把*p链接到*q后面*/
q=p;
}
q->next=NULL;
}
/*creatpoly*/
void SortPoly(LinkList L)
{LinkList pa,pb,qa,qb;
if(L->next!=0)
{pa=L->next;
L->next=NULL;
while(pa)
{pb=p1->next;
qa=L;
qb=L->next;
while(qb&&qb->data.exp<pa->data.exp)
{qa=qb;
qb=qb->next;
}
if(qb&&qb->data.exp==pa->data.exp)
{qb->data.coef+=pa->data.coef;
free(pa);
pa=pb;
}
else
{pa->next=qa->next;
qa->next=pa;
pa=pb;
}
}
}
}
/*SortPoly*/
double CountPoly(LinkList L,float x)
{LinkList p;
double sum=0;
p=L->next;
while(p)
{ sum+=p->data.coef*pow(x,p->data.exp);
p=p->next;
}
return sum;
}
/*CountPoly*/
void PrintPoly(LinkList L)
{ LinkList p;
p=L->next;
while(p)
{
if(L->next==p)
printf("%fx^%d",p->data.coef,p->data.exp);
else
{if(p->data.coef>0)
{printf("+");
if(p->data.exp!=0)
printf("%fx^%d",p->data.coef,p->data.expn);
else
printf("%f",p->data.coef);
}
else if(p->data.coef<0)
{ if(p->data.exp!=0)
printf("%fx^%d",p->data.coef,p->data.exp);
else
printf("%f",p->data.coef);
}
else{}
}
p=p->next;
}
printf("\n");
}
/*PrintPoly*/
void plusPoly(LinkList La,LinkList Lb,LinkList *Lc)
{
float c;
LinkList pa,pb,r,s;
*Lc=(LinkList)malloc(sizeof(LNode));
s=*Lc;
pa=La->next;
pb=Lb->next;
while(pa!=NULL&&pb!=NULL)
{if(pa->data.exp==pb->data.exp) /*两结点指数相等时*/
{ c=pa->data.coef+pb->data.coef;/*系数相加*/
if(c!=0)
{ r=(LinkList)malloc(sizeof(LNode));
r->data.exp=pa->data.exp;
r->data.coef=c;
s->next=r;
s=r;
}
pa=pa->next;
pb=pb->next;
}
else /*两结点指数不相等时*/
if(pa->data.exp<pb->data.exp)
{r=(LinkList)malloc(sizeof(LNode));
r->data=pa->data;
s->next=r;
s=r;
pa=pa->next;
}
else
{r=(LinkList)malloc(sizeof(LNode));
r->data=pb->data;
s->next=r;
s=r;
pb=pb->next;
}
}
while(pa!=NULL)/*pa!=NULL,复制a的剩余部分*/
{r=(LinkList)malloc(sizeof(LNode));
r->data=pa->data;
s->next=r;
s=r;
pa=pa->next;
}
while(pb!=NULL)/*pb!=NULL,复制b的剩余部分*/
{r=(LinkList)malloc(sizeof(LNode));
r->data=pb->data;
s->next=r;
s=r;
pb=pb->next;
}
s->next=NULL;/*链成单链表*/
}
/*plusPoly*/
void SubPoly(LinkList La,LinkList Lb,LinkList *Lc)
{LinkList p;
p=Lb->next;
while(p)
{p->data.coef=-p->data.coef;
p=p->next;
}
AddPolyn(La,Lb,Lc);
}
/*SubPoly*/
main()
{
int n,m;
LinkList La,Lb,Lc;
float x;
printf("请输入多项式项数: \n");
scanf("%d%d",&n,&m);
CreatPoly(&La,n);
CreatPoly(&Lb,m);
SortPoly(La);
SortPoly(Lb);
printf("请输入x的值: \n");
scanf("%f",&x);
printf("f(x)=%f\n",CountPoly(La,x));
PrintPoly(La);
PrintPoly(Lb);
plusPoly(La,Lb,&Lc);
SubPoly(La,Lb,&Lc);
PrintPoly(Lc);
}
#include<stdio.h>
#include<math.h>
#include<malloc.h>
typedef struct
{float coef;/*系数*/
int exp;/*指数*/
}ElemType;
typedef struct LNode
{float coef; /*系数*/
int exp; /*指数*/
struct Lnode *next; /*指针域*/
}LNode,*linklist;
void creatpoly(LinkList * L,int n)/*产生二项式链表*/
{int i;
LinkList p,q;
*L=(LinkList)malloc(sizeof(LNode));/*建立一个新结点*/
(*L)->next=NULL;
q=*L;
printf("请输入%d个数",2*n);
for(i=1;i<=n;i++)
{p=(LinkList)malloc(sizeof(LNode));
scanf("%f%d",&p->data.coef,&p->data.exp);
q->next=p;/*把*p链接到*q后面*/
q=p;
}
q->next=NULL;
}
/*creatpoly*/
void SortPoly(LinkList L)
{LinkList pa,pb,qa,qb;
if(L->next!=0)
{pa=L->next;
L->next=NULL;
while(pa)
{pb=p1->next;
qa=L;
qb=L->next;
while(qb&&qb->data.exp<pa->data.exp)
{qa=qb;
qb=qb->next;
}
if(qb&&qb->data.exp==pa->data.exp)
{qb->data.coef+=pa->data.coef;
free(pa);
pa=pb;
}
else
{pa->next=qa->next;
qa->next=pa;
pa=pb;
}
}
}
}
/*SortPoly*/
double CountPoly(LinkList L,float x)
{LinkList p;
double sum=0;
p=L->next;
while(p)
{ sum+=p->data.coef*pow(x,p->data.exp);
p=p->next;
}
return sum;
}
/*CountPoly*/
void PrintPoly(LinkList L)
{ LinkList p;
p=L->next;
while(p)
{
if(L->next==p)
printf("%fx^%d",p->data.coef,p->data.exp);
else
{if(p->data.coef>0)
{printf("+");
if(p->data.exp!=0)
printf("%fx^%d",p->data.coef,p->data.expn);
else
printf("%f",p->data.coef);
}
else if(p->data.coef<0)
{ if(p->data.exp!=0)
printf("%fx^%d",p->data.coef,p->data.exp);
else
printf("%f",p->data.coef);
}
else{}
}
p=p->next;
}
printf("\n");
}
/*PrintPoly*/
void plusPoly(LinkList La,LinkList Lb,LinkList *Lc)
{
float c;
LinkList pa,pb,r,s;
*Lc=(LinkList)malloc(sizeof(LNode));
s=*Lc;
pa=La->next;
pb=Lb->next;
while(pa!=NULL&&pb!=NULL)
{if(pa->data.exp==pb->data.exp) /*两结点指数相等时*/
{ c=pa->data.coef+pb->data.coef;/*系数相加*/
if(c!=0)
{ r=(LinkList)malloc(sizeof(LNode));
r->data.exp=pa->data.exp;
r->data.coef=c;
s->next=r;
s=r;
}
pa=pa->next;
pb=pb->next;
}
else /*两结点指数不相等时*/
if(pa->data.exp<pb->data.exp)
{r=(LinkList)malloc(sizeof(LNode));
r->data=pa->data;
s->next=r;
s=r;
pa=pa->next;
}
else
{r=(LinkList)malloc(sizeof(LNode));
r->data=pb->data;
s->next=r;
s=r;
pb=pb->next;
}
}
while(pa!=NULL)/*pa!=NULL,复制a的剩余部分*/
{r=(LinkList)malloc(sizeof(LNode));
r->data=pa->data;
s->next=r;
s=r;
pa=pa->next;
}
while(pb!=NULL)/*pb!=NULL,复制b的剩余部分*/
{r=(LinkList)malloc(sizeof(LNode));
r->data=pb->data;
s->next=r;
s=r;
pb=pb->next;
}
s->next=NULL;/*链成单链表*/
}
/*plusPoly*/
void SubPoly(LinkList La,LinkList Lb,LinkList *Lc)
{LinkList p;
p=Lb->next;
while(p)
{p->data.coef=-p->data.coef;
p=p->next;
}
AddPolyn(La,Lb,Lc);
}
/*SubPoly*/
main()
{
int n,m;
LinkList La,Lb,Lc;
float x;
printf("请输入多项式项数: \n");
scanf("%d%d",&n,&m);
CreatPoly(&La,n);
CreatPoly(&Lb,m);
SortPoly(La);
SortPoly(Lb);
printf("请输入x的值: \n");
scanf("%f",&x);
printf("f(x)=%f\n",CountPoly(La,x));
PrintPoly(La);
PrintPoly(Lb);
plusPoly(La,Lb,&Lc);
SubPoly(La,Lb,&Lc);
PrintPoly(Lc);
}