02-線性結構2 一元多項式的乘法與加法運算
阿新 • • 發佈:2021-08-19
02-線性結構2 一元多項式的乘法與加法運算 (20 分)
設計函式分別求兩個一元多項式的乘積與和。
輸入格式:
輸入分2行,每行分別先給出多項式非零項的個數,再以指數遞降方式輸入一個多項式非零項係數和指數(絕對值均為不超過1000的整數)。數字間以空格分隔。
輸出格式:
輸出分2行,分別以指數遞降方式輸出乘積多項式以及和多項式非零項的係數和指數。數字間以空格分隔,但結尾不能有多餘空格。零多項式應輸出0 0。
輸入樣例:
4 3 4 -5 2 6 1 -2 0
3 5 20 -7 4 3 1
結尾無空行
輸出樣例:
15 24 -25 22 30 21 -10 20 -21 8 35 6 -33 5 14 4 -15 3 18 2 -6 1
5 20 -4 4 -5 2 9 1 -2 0
結尾無空行
//寫的太亂了/(ㄒoㄒ)/~~ #include<stdio.h> #include<stdlib.h> //連結串列實現 struct node{ int cofe; int expn; struct node *next; }; typedef struct node* Node; Node Creatlist()//帶頭結點 { Node head; head=(Node)malloc(sizeof(struct node)); head->next=NULL; return head; } void AddNode(Node rear,int cofe,int expn) { Node Newp; Newp=(Node)malloc(sizeof(struct node)); Newp->cofe=cofe; Newp->expn=expn; Newp->next=NULL; rear->next=Newp; rear=Newp; } Node Findrear(Node head) { while(head->next) { head=head->next; } return head; } void print(Node head) { head=head->next; if(head==NULL)printf("0 0\n"); else { while(head) { printf("%d %d",head->cofe,head->expn); head=head->next; if(head)printf(" "); } printf("\n"); } } Node Mix(Node head1,Node head2) { Node List,rear,Newp; List=(Node)malloc(sizeof(struct node)); List->next=NULL; head1=head1->next; head2=head2->next; while(head1&&head2) { Newp=(Node)malloc(sizeof(struct node)); if(head1->expn>head2->expn) { Newp->cofe=head1->cofe; Newp->expn=head1->expn; head1=head1->next; } else if(head1->expn<head2->expn) { Newp->cofe=head2->cofe; Newp->expn=head2->expn; head2=head2->next; } else { Newp->cofe=head1->cofe+head2->cofe; Newp->expn=head1->expn; head1=head1->next; head2=head2->next; } if(Newp->cofe){ Newp->next=NULL; rear=Findrear(List); rear->next=Newp; rear=Newp; } else free(Newp);//係數和為0; } while(head1) { Newp=(Node)malloc(sizeof(struct node)); Newp->cofe=head1->cofe; Newp->expn=head1->expn; Newp->next=NULL; rear=Findrear(List); rear->next=Newp; rear=Newp; head1=head1->next; } while(head2) { Newp=(Node)malloc(sizeof(struct node)); Newp->cofe=head2->cofe; Newp->expn=head2->expn; Newp->next=NULL; rear=Findrear(List); rear->next=Newp; rear=Newp; head2=head2->next; } return List; } int insert(Node List,Node Newp,int cofe,int expn) { Node rear; Newp->cofe= cofe; Newp->expn=expn; while(List->next) { if(expn==List->next->expn){ if(cofe+(List->next->cofe)==0){ List->next=List->next->next; } else { List->next->cofe=cofe+List->next->cofe; } return 1; } else if(expn>List->next->expn){//比它大就插在它前面。 Newp->next=List->next; List->next=Newp; return 1; } List=List->next; } rear=Findrear(List); rear->next=Newp; rear=Newp; return 1; } Node Mult(Node head1,Node head2)//比指標指向的()大的就插入,小的就指標後移。 { Node List,Newp,rear,p1=head1,p2=head2; List=(Node)malloc(sizeof(struct node)); List->next=NULL; ///新連結串列 head1=head1->next; ///指標往後移動一位 head2=head2->next; int cofe,expn; while(head1) { head2=p2->next; while(head2) { Newp=(Node)malloc(sizeof(struct node));//現建一個新結點, Newp->next=NULL; cofe=(head2->cofe)*(head1->cofe); expn=head2->expn+head1->expn; if(List->next==NULL) { Newp->cofe= cofe; Newp->expn=expn; List->next=Newp; Newp->next=NULL; } else { insert(List,Newp,cofe,expn); } head2=head2->next; } head1=head1->next; } return List; } int main() { Node head1,rear1,head2,rear2,MixList,MultList; head1=Creatlist(); rear1=Findrear(head1); head2=Creatlist(); rear2=Findrear(head2); int n,m,i,cofe,expn;//n,m分別是項的個數 scanf("%d",&n); for(i=0;i<n;i++) { scanf("%d %d",&cofe,&expn); rear1=Findrear(head1); AddNode(rear1,cofe,expn); } scanf("%d",&m); for(i=0;i<m;i++) { scanf("%d %d",&cofe,&expn); rear2=Findrear(head2); AddNode(rear2,cofe,expn); } MixList=Mix(head1,head2); MultList=Mult(head1,head2); print(MultList); print(MixList); }