作者在 2010-05-12 21:28:45 发布以下内容
#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 * fg;
LNode *t,*q,*s,*r;
float m;
t=L->next;
q=Pb->next;
fg=r=(LNode*)malloc(sizeof(LNode));
fg->next=NULL;
while(t&&q)
{
if(t->expn==q->expn)
{
m=t->coef+q->coef;
if(m!=0)
{
s=(LNode *)malloc(sizeof(LNode));
s->coef=m;
s->expn=t->expn;
s->next=NULL;
}
t=t->next;
q=q->next;
}
else
if(t->expn<q->expn)
{
s=(LNode *)malloc(sizeof(LNode));
s->coef=t->coef;
s->expn=t->expn;
s->next=NULL;
t=t->next;
}
else
{
s=(LNode *)malloc(sizeof(LNode));
s->coef=q->coef;
s->expn=q->expn;
s->next=NULL;
q=q->next;
}
if(fg->next==NULL)
{
fg->next=s;
r=s;
}
else
{
r->next=s;
r=s;
}
}
r->next=t?t:q;
return fg;
}
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;
textbackground(2);
clrscr();
highvideo();
textcolor(4);
clrscr();
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();
}
#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 * fg;
LNode *t,*q,*s,*r;
float m;
t=L->next;
q=Pb->next;
fg=r=(LNode*)malloc(sizeof(LNode));
fg->next=NULL;
while(t&&q)
{
if(t->expn==q->expn)
{
m=t->coef+q->coef;
if(m!=0)
{
s=(LNode *)malloc(sizeof(LNode));
s->coef=m;
s->expn=t->expn;
s->next=NULL;
}
t=t->next;
q=q->next;
}
else
if(t->expn<q->expn)
{
s=(LNode *)malloc(sizeof(LNode));
s->coef=t->coef;
s->expn=t->expn;
s->next=NULL;
t=t->next;
}
else
{
s=(LNode *)malloc(sizeof(LNode));
s->coef=q->coef;
s->expn=q->expn;
s->next=NULL;
q=q->next;
}
if(fg->next==NULL)
{
fg->next=s;
r=s;
}
else
{
r->next=s;
r=s;
}
}
r->next=t?t:q;
return fg;
}
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;
textbackground(2);
clrscr();
highvideo();
textcolor(4);
clrscr();
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();
}