float coef; //系数 int exp; //指数 struct Polynode *next;
}*Poly,Polynode; //Poly为结点指针类型
void Insert(Poly p,Poly head) {
if(p->coef==0) free(p); else {
Poly q1,q2; q1=head;
q2=head->next;
while(q2&&p->exp { q1=q2; q2=q2->next; } if(q2&&p->exp==q2->exp) { q2->coef+=p->coef; free(p); if(!q2->coef) { q1->next=q2->next; free(q2); } } else { p->next=q2; q1->next=p; } } } Poly CreateList(Poly head,int m) //系数为0时释放结点 //后移查找插入位置 //将指数相同的相合并 //系数为0时释放结点 //Insert { //建立一个头指针为head、项数为m的一元多项式 int i; Poly p; Polynode *q; p=head=(Poly)malloc(sizeof(struct Polynode)); head->next=NULL; for(i=0;i Insert(p,head); } q=head->next; while(q!=NULL) { q=q->next; } return head; } void DestroyList(Poly p) { Poly q1,q2; if(p->next!=NULL) { q1=p->next; q2=q1->next; while(q1->next) { free(q1); q1=q2; q2=q2->next; } } } int OutputList(Poly P) { Poly q=P->next; int flag=1; if(!q) //建立新结点以接收数据 //调用Insert函数插入结点 //CreatePoly //销毁多项式p //指针后移 //输出多项式 //项数计数器 { //若多项式为空,输出0 printf(\"0\"); return(0); } while (q) { if(q->coef>0&&flag!=1) //系数大于0且不是第一项 printf(\"+\"); if(q->coef!=1&&q->coef!=-1) { printf(\"%.f\ if(q->exp==1) printf(\"X\"); else if(q->exp) printf(\"X^%d\} else { if(q->coef==1) { if(!q->exp) printf(\"1\"); else if(q->exp==1) printf(\"X\"); else if(q->exp) printf(\"X^%d\} if(q->coef==-1) { if(!q->exp) printf(\"-1\"); else if(q->exp==1) printf(\"-X\"); else printf(\"-X^%d\} } q=q->next; flag++; } printf(\"\\n\"); return(0); } //OutputPoly //系数非1或-1的普通情况 //while int compare(Poly a,Poly b) { if(a&&b) { if(!b||a->exp>b->exp) return 1; else if(!a||a->exp else if(!a&&b) return -1; 但b多项式非空 else return 1; } //compare Poly AddPoly(Poly pa,Poly pb) { 和多项式a+b,返回其头指针 Poly qa=pa->next; Poly qb=pb->next; Poly headc,hc,qc; hc=(Poly)malloc(sizeof(struct Polynode)); hc->next=NULL; headc=hc; while(qa||qb) { qc=(Poly)malloc(sizeof(struct Polynode)); switch(compare(qa,qb)) { case 1: { qc->coef=qa->coef; qc->exp=qa->exp; qa=qa->next; break; } case 0: { qc->coef=qa->coef+qb->coef; qc->exp=qa->exp; qa=qa->next; qb=qb->next; break; } //a多项式已空,//b多项式已空,但a多项式非空 //求解并建立 //建立头结点 case -1: { qc->coef=qb->coef; qc->exp=qb->exp; qb=qb->next; break; } } //switch if(qc->coef!=0) { qc->next=hc->next; hc->next=qc; hc=qc; } else free(qc); } //while return headc; } //AddPoly Poly SubtractPoly(Poly pa,Poly pb) { 其头指针 Poly qa=pa->next; Poly qb=pb->next; Poly headc,hc,qc; hc=(Poly)malloc(sizeof(struct Polynode)); hc->next=NULL; headc=hc; while(qa||qb) { qc=(Poly)malloc(sizeof(struct Polynode)); switch(compare(qa,qb)) { case 1: { qc->coef=qa->coef; qc->exp=qa->exp; qa=qa->next; break; } case 0: //当相加系数为0时,释放该结点 //求解并建立和多项式a-b,返回 //建立头结点 { qc->coef=qa->coef-qb->coef; qc->exp=qa->exp; qa=qa->next; qb=qb->next; break; } case -1: { qc->coef=-qb->coef; qc->exp=qb->exp; qb=qb->next; break; } } //switch if(qc->coef!=0) { qc->next=hc->next; hc->next=qc; hc=qc; } else free(qc); //当相减系数为0时,释放该结点 } //while return headc; } //AddPoly int main() { int m,n,flag=0; Poly pa=0,pb=0,pc,pd; //输出菜单 printf(\"**************************************************************************\\n\"); printf(\" 1.输入多项式 2.输出多项式a和b 3.建立多项式a+b 4.建立多项式a-b 5.退出程序 \\n\"); printf(\"**************************************************************************\\n\"); for(;;flag=0) { printf(\"执行操作为:\") ; scanf(\"%d\if(flag==1) { //定义各式的头指针,pa与pb在使用前付 初值NULL printf(\"请输入a的项数:\") ; scanf(\"%d\ pa=CreateList(pa,m); //建立多项式a printf(\"请输入b的项数:\" ); scanf(\"%d\ pb=CreateList(pb,n); //建立多项式b } if(flag==2) { printf(\"多项式a为:\"); OutputList(pa); printf(\"多项式b为:\"); OutputList(pb); continue; } if(flag==3) { pc=AddPoly(pa,pb); printf(\"多项式a+b为:\"); OutputList(pc); DestroyList(pc); continue; } if(flag==4) { pd=SubtractPoly(pa,pb); printf(\"多项式a-b为:\"); OutputList(pd); DestroyList(pd); continue; } if(flag==5) break; if(flag<1||flag>5) printf(\"输入错误!!!请重新选择!!\\n\"); continue; } DestroyList(pa); DestroyList(pb); return 0; } 因篇幅问题不能全部显示,请点此查看更多更全内容