标签:des style blog http io ar os 使用 sp
/* <span style="font-size:18px;">代码里的使用的链表请见<a target=_blank href="http://blog.csdn.net/u013303425/article/details/41320861"> http://blog.csdn.net/u013303425/article/details/41320861</a></span> */ #include<stdio.h> #include<stdlib.h> #include "Link.h" typedef LinkList Polynominal; typedef struct{ float coef; int expn; } term,ElemType; Status CreatePolyn(Polynominal **poly,int m){ if(!InitList(poly)) return ERROR; ElemType e; Link * p; for(int i=0;i<m;i++){ scanf("%f-%d",&e.coef,&e.expn); MakeNode(&p,e); Append(*poly,p); } return OK; } Status DestroyPoly(Polynominal *poly){ DestroyList(poly); return OK; } void PrintPoly(Polynominal *poly){ //printf("%x",poly); Link * temp = poly->head->next; while(temp){ printf(" %0.2fx^%d ",temp->data.coef,temp->data.expn); if(temp->next != NULL && temp->next->data.coef >0.0){ printf("+"); } temp = temp->next; } printf("\n"); } int PolyLength(Polynominal *poly){ return ListLength(poly); } void AddPoly(Polynominal *poly1,Polynominal *poly2,int (* compare)(ElemType,ElemType)){ //将poly2 合并到Poly1中 Link *ha,*hb,*pa,*pb; ha = poly1->head; hb = poly2->head; pa = NextPos(poly1,ha); pb = NextPos(poly2,hb); while(pa && pb){ if((*compare)(pa->data,pb->data) == 1){ //pa->data.expn > pb->data.expn DelFirst(poly2,hb,pb); InsBefore(poly1,pa,pb); pb= NextPos(poly2,hb); }else if((*compare)(pa->data,pb->data) == 0){ DelFirst(poly2,hb,pb); pa->data.coef += pb->data.coef; Link * tempprior = PriorPos(poly1,pa); Link * tempnext = NextPos(poly1,pa); if(pa->data.coef == 0){ FreeNode(poly1,&pa); tempprior->next = tempnext; } FreeNode(poly2,&pb); pa = tempnext; pb = NextPos(poly2,hb); }else{ pa= NextPos(poly2,pa); } } if(pb !=NULL){Append(poly1,pb);} free(hb); } void SubtractPoly(Polynominal *poly1,Polynominal *poly2,int (* compare)(ElemType,ElemType)){ Link *ha,*hb,*pa,*pb; ha = poly1->head; hb = poly2->head; pa = NextPos(poly1,ha); pb = NextPos(poly2,hb); while(pa && pb){ if((*compare)(pa->data,pb->data) == 1){ //pa->data.expn > pb->data.expn DelFirst(poly2,hb,pb); pb->data.coef = -pb->data.coef; InsBefore(poly1,pa,pb); pb= NextPos(poly2,hb); }else if((*compare)(pa->data,pb->data) == 0){ DelFirst(poly2,hb,pb); pa->data.coef -= pb->data.coef; Link * tempprior = PriorPos(poly1,pa); Link * tempnext = NextPos(poly1,pa); if(pa->data.coef == 0){ FreeNode(poly1,&pa); tempprior->next = tempnext; } FreeNode(poly2,&pb); pa = tempnext; pb = NextPos(poly2,hb); }else{ pa= NextPos(poly2,pa); } } while(pb){ DelFirst(poly2,hb,pb); pb->data.coef = -pb->data.coef; Append(poly1,pb); pb=NextPos(poly2,pb); } free(hb); } void Multip(Polynominal *poly1,Polynominal *poly2,Polynominal *poly3){ //将poly1和Poly2相乘的结果保存到poly3中 Link *pa,*pb; pb = NextPos(poly2,poly2->head); ElemType etemp; Link * temp = NULL; while(pb){ pa = NextPos(poly1,poly1->head); while(pa){ etemp.coef = pa->data.coef * pb->data.coef; etemp.expn = pa->data.expn + pb->data.expn; MakeNode(&temp,etemp); Append(poly3,temp); pa = NextPos(poly1,pa); //FreeNode(poly1,&temp); } pb = NextPos(poly2,pb); } } int compare(ElemType e1,ElemType e2){ if(e1.expn>e2.expn){ return 1; }else if(e1.expn==e2.expn){ return 0; }else{ return -1; } } int main(){ Polynominal *poly1=NULL; Polynominal *poly2=NULL; Polynominal *poly3=NULL; InitList(&poly3); CreatePolyn(&poly1,2); printf("多项式1:"); PrintPoly(poly1); CreatePolyn(&poly2,2); printf("多项式2:"); PrintPoly(poly2); //SubtractPoly(poly1,poly2,compare); //printf("合并后的多项式:"); //PrintPoly(poly1); Multip(poly1,poly2,poly3); printf("相乘后的多项式"); PrintPoly(poly3); return 0; }
标签:des style blog http io ar os 使用 sp
原文地址:http://blog.csdn.net/u013303425/article/details/41357143