作者在 2010-05-12 21:37:22 发布以下内容
#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;
}
printf("\n");
}
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;
LNode *L ;
initlist(L);
printf("Enter how many number! n=");
scanf("%d",&n);
Creatlist(L,n);
selsort(L);
PrintfPoly(L);
printf("Enter the number X=");
scanf("%d",&X);
Addlist(L,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;
}
printf("\n");
}
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;
LNode *L ;
initlist(L);
printf("Enter how many number! n=");
scanf("%d",&n);
Creatlist(L,n);
selsort(L);
PrintfPoly(L);
printf("Enter the number X=");
scanf("%d",&X);
Addlist(L,X);
getch();
}