2019-12-16

         简单的降幂排序算法(C语言)

#include
#include
#include
#include
typedef struct Polynomial{
float coef;
int exp;
struct Polynomial *next;
}*Polyn,Polynomial;
void Insert(Polyn p,Polyn h){
if(p->coef0)
free§;
else{
Polyn q1,q2;
q1=h;
q2=h->next;
while(q2&&p->expexp){
q1=q2;
q2=q2->next;
}
if(q2&&p->exp
q2->exp){
q2->coef+=p->coef;
free§;
if(!q2->coef){
q1->next=q2->next;
free(q2);
}
}
else{
p->next=q2;
q1->next=p;
}
}
}
Polyn CreatePolyn(Polyn head,int m){
int i;
Polyn p;
p=head=(Polyn)malloc(sizeof(struct Polynomial));
head->next=NULL;
for(i=0;i p=(Polyn)malloc(sizeof(struct Polynomial));
printf(“请输入一元多项式第%d项的系数与指数:”,i+1);
scanf("%f %d",&p->coef,&p->exp);
Insert(p,head);
}
return head;
}
void DestroyPolyn(Polyn p){
Polyn q1,q2;
q1=p->next;
q2=q1->next;
while(q1->next){
free(q1);
q1=q2;
q2=q2->next;
}
}
void PrintPolyn(Polyn P){
Polyn q=P->next;
int n=1;
if(!q) {
putchar(‘0’);
printf("\n");
return;
}
while (q){
if(q->coef>0&&n!=1) putchar(’+’);
if(q->coef!=1&&q->coef!=-1)
{
printf("%g",q->coef);
if(q->exp1)
putchar(‘X’);
else if(q->exp)
printf(“X^%d”,q->exp);
}
else{
if(q->coef
1){
if(!q->exp)
putchar(‘1’);
else if(q->exp1)
putchar(‘X’);
else printf(“X^%d”,q->exp);
}
if(q->coef
-1){
if(!q->exp)
printf("-1");
else if(q->exp==1)
printf("-X");
else printf("-X^%d",q->exp);
}
}
q=q->next;
n++;
}
printf("\n");
}
int compare(Polyn a,Polyn b){
if(a&&b){
if(!b||a->exp>b->exp)
return 1;
else if(!a||a->expexp)
return -1;
else return 0;
}
else if(!a&&b)
return -1;
else return 1;
}
Polyn AddPolyn(Polyn pa,Polyn pb){
Polyn qa=pa->next;
Polyn qb=pb->next;
Polyn headc,hc,qc;
hc=(Polyn)malloc(sizeof(struct Polynomial));
hc->next=NULL;
headc=hc;
while(qa||qb){
qc=(Polyn)malloc(sizeof(struct Polynomial

你可能感兴趣的:(笔记)