70,037
社区成员
发帖
与我相关
我的任务
分享
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
typedef struct
{
float coef;
int expn;
}term,ElemType;
typedef struct LNode
{
ElemType data;
struct LNode *next;
}LNode, *LinkList;
typedef LinkList polynomail;
LinkList CreatPolyn(int m)
{
int i;
polynomail P;
ElemType newdata;
LNode *newnode;
LNode *p0;
P=(polynomail)malloc(sizeof(LNode));
p0=P;
p0->next=NULL;
for(i=0;i<m;i++)
{
newnode=(polynomail)malloc(sizeof(LNode));
printf("Please input the coef:");
scanf("%f",&newdata.coef);
printf("Please input the expn:");
scanf("%d",&newdata.expn);
p0->next=newnode;
p0->next->data=newdata;
p0=p0->next;
}
return P;
}
void PrintPolyn(polynomail P)
{
LNode *n;
ElemType data;
n=P->next;
while(n!=NULL)
{
data=n->data;
printf("%fX^%d+",data.coef,data.expn);
n=n->next;
}
}
void AddPolyn(polynomail Pa,polynomail Pb)
{
LNode *p1;
LNode *p2;
LNode *p3;
p3=Pa;
p1=Pa->next;
p2=Pb->next;
while(p1!=NULL&&p2!=NULL)
{
if(p1->data.expn<p2->data.expn&&p1->next->data.expn>p2->next->data.expn)
{
p2->next=p1->next;
p1->next=p2;
free(p2);
}
if(p1->data.expn=p2->data.expn)
{
p1->data.coef=(p1->data.coef)+(p2->data.coef);
}
if(p1->data.expn>p2->data.expn)
{
p1=p1->next;
p3->next=p2;
p2->next=p1;
}
p3=p3->next;
p2=p2->next;
p1=p1->next;
}
}
void main()
{
int number;
polynomail L;
printf("Please input the number of the items:");
scanf("%d",&number);
L=CreatPolyn(number);
PrintPolyn(L);
getchar();
}
for(i=0;i<m;i++)
{
newnode=(polynomail)malloc(sizeof(LNode));
printf("Please input the coef:");
scanf("%f",&newdata.coef);
printf("Please input the expn:");
scanf("%d",&newdata.expn);
p0->next=newnode;
p0->next->data=newdata;
p0=p0->next;
}
p0->next = NULL; //add
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
typedef struct
{
float coef;
int expn;
}term,ElemType;
typedef struct LNode
{
ElemType data;
struct LNode *next;
}LNode, *LinkList;
typedef LinkList polynomail;
LinkList CreatPolyn(int m)
{
int i;
polynomail P;
ElemType newdata;
LNode *newnode;
LNode *p0;
P=(polynomail)malloc(sizeof(LNode));
p0=P;
p0->next=NULL;
for(i=0;i<m;i++)
{
newnode=(polynomail)malloc(sizeof(LNode));
newnode->next = NULL; //尾指针置空
printf("Please input the coef:");
scanf("%f",&newdata.coef);
printf("Please input the expn:");
scanf("%d",&newdata.expn);
p0->next=newnode;
p0->next->data=newdata;
p0=p0->next;
}
return P;
}
void PrintPolyn(polynomail P)
{
LNode *n;
ElemType data;
n=P->next;
while(n!=NULL)
{
data=n->data;
printf("%fX^%d+",data.coef,data.expn);
n=n->next;
}
}
void AddPolyn(polynomail Pa,polynomail Pb)
{
LNode *p1;
LNode *p2;
LNode *p3;
p3=Pa;
p1=Pa->next;
p2=Pb->next;
while(p1!=NULL&&p2!=NULL)
{
if(p1->data.expn<p2->data.expn&&p1->next->data.expn>p2->next->data.expn)
{
p2->next=p1->next;
p1->next=p2;
free(p2);
}
if(p1->data.expn=p2->data.expn)
{
p1->data.coef=(p1->data.coef)+(p2->data.coef);
}
if(p1->data.expn>p2->data.expn)
{
p1=p1->next;
p3->next=p2;
p2->next=p1;
}
p3=p3->next;
p2=p2->next;
p1=p1->next;
}
}
void main()
{
int number;
polynomail L;
printf("Please input the number of the items:");
scanf("%d",&number);
L=CreatPolyn(number);
PrintPolyn(L);
getchar();
}