?? 多項式.c
字號:
#include<stdio.h>
#include<malloc.h>
typedef int ExpType;
typedef float CoefType;
typedef struct ploy{
CoefType coef;
ExpType exp;
struct ploy * next;
}NodeType,* LinkType;
void ploy_creat(LinkType head);/*創(chuàng)建一個結(jié)點*/
ExpType ploy_cmp(ExpType a,ExpType b);/*比較相對應(yīng)項的指數(shù)*/
void ploy_add(LinkType head_a,LinkType head_b,LinkType head_c);/*加法*/
void ploy_sub(LinkType head_a,LinkType head_b,LinkType head_d);/*減法*/
void ploy_derivtive(LinkType head,LinkType h);/*求導(dǎo)數(shù)*/
void ploy_orderinsert(LinkType head,LinkType s);/*排序*/
void print(LinkType head);/*輸出最終結(jié)果*/
void ploy_del(LinkType head);/*刪除鏈表*/
void main()
{
ExpType choice=0;
LinkType head_a, head_b,head_c,head_d,head_e,head_f;
do{
head_a=(LinkType)malloc(sizeof(NodeType));
head_a->next=NULL;
head_b=(LinkType)malloc(sizeof(NodeType));
head_b->next=NULL;
head_c=(LinkType)malloc(sizeof(NodeType));
head_c->next=NULL;
head_d=(LinkType)malloc(sizeof(NodeType));
head_d->next=NULL;
head_e=(LinkType)malloc(sizeof(NodeType));
head_e->next=NULL;
head_f=(LinkType)malloc(sizeof(NodeType));
head_f->next=NULL;
printf("*************************多項式加減法************************\n");
printf("請輸入a多項式的項數(shù)\n");
ploy_creat(head_a);
printf("請輸入b多項式的項數(shù)\n");
ploy_creat(head_b);
printf("************************************************************\n");
printf("********輸出的多項式A:*********\n");
print(head_a);
printf(" \n");
printf("********輸出的多項式B:*********\n");
print(head_b); printf(" \n");
do{
printf("請選擇進行按1**加法或2**減法或3**求導(dǎo)數(shù)0***推出\n");
scanf("%d",&choice);
printf("************************************************************\n");
switch(choice)
{
case 1:ploy_add(head_a,head_b,head_c); print(head_c);ploy_del(head_c);break;
case 2:ploy_sub(head_a,head_b,head_d); print(head_d);ploy_del(head_d);break;
case 3:ploy_derivtive(head_a,head_e);ploy_derivtive(head_b,head_f);printf("A:");print(head_e);ploy_del(head_e);printf("\n");printf("B:");
print(head_f);ploy_del(head_f);break;
case 0:break;
default :printf("WRONG ENTER!\n");
}
}while(choice);
}while(choice);
}
void print(LinkType head)
{
LinkType link=head->next;
if(!link)
{
printf("0\n");
}else
{
printf("%g",link->coef);
if(link->exp!=0)
{
printf("^%d",link->exp);
link=link->next;
}
while(link)
{
if(link->coef>0)
printf("%+g",link->coef);
if(link->coef<0)
printf("%g",link->coef);
if(link->exp!=0)
printf("^%d",link->exp);
link=link->next;
}
}
}
void ploy_orderinsert(LinkType head,LinkType s)
{
NodeType *LinkType=head->next;
while(LinkType&&LinkType->exp>s->exp)
{
head=LinkType;
LinkType=LinkType->next;
}
if(LinkType&&LinkType->exp==s->exp)
LinkType->coef+=s->coef;
else
{
s->next=LinkType;
head->next=s;
}
}
void ploy_creat(LinkType head)
{
ExpType num,i;
NodeType *pre;
scanf("%d",&num);
for(i=1;i<=num;i++)
{
if(!(pre=(LinkType)malloc(sizeof(NodeType))))
{
printf("ERROR");
return;
}
printf("請輸入多項式的第");
printf("%d",i);
printf("項的系數(shù) 和 指數(shù)\n");
scanf("%f%d",&((*pre).coef),&((*pre).exp));
ploy_orderinsert( head,pre);
}
}
ExpType ploy_cmp(ExpType a,ExpType b)
{
if(a<b )
return -1;
else if(a==b)
return 0;
else
return 1;
}
void ploy_add(LinkType head_a,LinkType head_b,LinkType head_c)
{
CoefType sum=0;
LinkType hc,qa,qb,qc;
ExpType a,b;
hc=head_c;
qa=head_a->next;
qb=head_b->next;
while(qa&&qb)
{
a=qa->exp;
b=qb->exp;
switch(ploy_cmp(a,b))
{
case -1:qc=(LinkType)malloc(sizeof(NodeType));hc->next=qc;qc->coef=qb->coef;qc->exp=qb->exp;qc->next=NULL;hc=qc;qb=qb->next;break;
case 0:qc=(LinkType)malloc(sizeof(NodeType));sum=(qa->coef)+(qb->coef);
if(sum!=0)
{
hc->next=qc;qc->coef=sum;qc->exp=qa->exp;qc->next=NULL;hc=qc;
}
qb=qb->next;qa=qa->next;break;
case 1:qc=(LinkType)malloc(sizeof(NodeType));hc->next=qc;qc->coef=qa->coef;qc->exp=qa->exp;qc->next=NULL;hc=qc;qa=qa->next;break;
}
}
if(qa)
while(qa)
{
qc=(LinkType)malloc(sizeof(NodeType));hc->next=qc;
qc->coef=qa->coef;qc->exp=qa->exp;qc->next=NULL;hc=qc;qa=qa->next;
}
if(qb)
while(qb)
{
qc=(LinkType)malloc(sizeof(NodeType));hc->next=qc;
qc->coef=qb->coef;qc->exp=qb->exp;qc->next=NULL;hc=qc;qb=qb->next;
}
}
void ploy_sub(LinkType head_a,LinkType head_b,LinkType head_d)
{
CoefType sum=0;
LinkType hc,qa,qb,qc;
ExpType a,b;
hc=head_d;
qa=head_a->next;
qb=head_b->next;
while(qa&&qb)
{
a=qa->exp;
b=qb->exp;
switch(ploy_cmp(a,b))
{
case -1:qc=(LinkType)malloc(sizeof(NodeType));hc->next=qc;qc->coef=-(qb->coef);qc->exp=qb->exp;qc->next=NULL;hc=qc;qb=qb->next;break;
case 0:qc=(LinkType)malloc(sizeof(NodeType));sum=(qa->coef)-(qb->coef);
if(sum!=0)
{
hc->next=qc;qc->coef=sum;qc->exp=qa->exp;qc->next=NULL;hc=qc;
}
qb=qb->next;qa=qa->next;break;
case 1:qc=(LinkType)malloc(sizeof(NodeType));hc->next=qc;qc->coef=qa->coef;qc->exp=qa->exp;qc->next=NULL;hc=qc;qa=qa->next;break;
}
}
if(qa)
while(qa)
{
qc=(LinkType)malloc(sizeof(NodeType));hc->next=qc;
qc->coef=qa->coef;qc->exp=qa->exp;qc->next=NULL;hc=qc;qa=qa->next;
}
if(qb)
while(qb)
{
qc=(LinkType)malloc(sizeof(NodeType));hc->next=qc;
qc->coef=qb->coef;qc->exp=qb->exp;qc->next=NULL;hc=qc;qb=qb->next;
}
}
void ploy_derivtive(LinkType head,LinkType h)
{
LinkType link=head->next;
LinkType q;
while(link)
{
if(link->exp)
{
q=(LinkType)malloc(sizeof(NodeType));
h->next=q;q->coef=link->coef*link->exp;
q->exp=(link->exp-1);h=q;link=link->next;
q->next=NULL;
}else
{
link=link->next;
}
}
}
void ploy_del(LinkType head)
{
while(head)
{
LinkType link=head->next;
free(head);
head=link;
}
printf("鏈表刪除成功\n");
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -