用C語言編程,要求解決下面的問題。1 問題描述對任意輸入的兩個一元多項式完成計算:和,差,乘積,商等運算。2要求與提示(1)多項式可用鏈表表示(2)求和與差算法完全一致,多項式相加減運算規則為:若兩項指數相等 ,則系數相加減,若兩指數不等,則兩項按指數大小次序復抄。(3)求乘積時的規則為:把一個多項式中的每一項分別乘以另一個多項式的每一項,完成后再合并同類項(相乘時,系數相乘,指數相加)(4)多項式相除規則為:以被除數和除數的指數為依據相除。PS:這是寫論文哈,要詳細哦!!!還要 有注解呢!!謝謝了,拜托,早點完成哦!!!
熱心網友
#include #include #include #include typedef int datatype;typedef struct LNode{float coef; /*多項式系數*/int expn; /*多項式指數*/struct LNode *next;}LNode,*linklist;/*---------創建帶頭結點的多項式鏈表--------*/linklist creat(){ linklist head,s,p,pre;float coef;int expn;head=(linklist)malloc(sizeof(LNode)); /*表頭結點*/head-next=NULL;printf("輸入系數:");scanf("%f",&coef);printf("輸入指數:");scanf("%d",&expn);while (coef!=0。0) { printf("當系數等于零的時候結束!");s=(linklist)malloc(sizeof(LNode)); /*生成新結點*/s-coef=coef;s-expn=expn;s-next=NULL;pre=head; /*插入到有序的多項式鏈表中去*/p=head-next;while (p && p-expn expn) { pre=p;p=p-next;}s-next=p;pre-next=s;printf("讀下一項:\n");printf("輸入系數:\n");scanf("%f",&coef); printf("\n");printf("輸入指數:");scanf("%d",&expn);}return head;}/*-----------輸出多項式鏈表-------------*/void print(linklist head) { linklist p;p=head-next;while (p){ printf("%fX^%d+",p-coef,p-expn);p=p-next;}}/*-------------多項式相加----------------*/linklist add(linklist pa,linklist pb){linklist p,q,pre,r,head;float x;head=pa; p=pa-next; //p,q 指向頭接點的下一個接點,即多項式的第一個接點q=pb-next;pre=pa; //pre指向p的前驅while((p!=NULL)&&(q!=NULL)) //處理多項式的相加的問題if(p-expnexpn) {pre=p;p=p-next;}else if (p-expn==q-expn){x=p-coef+q-coef;if(x!=0) //系數相加不為0的情況{p-coef=x;pre=p;p=p-next;}else //系數相加為0的情況{pre-next=p-next;free(p);p=pre-next;}r=q;q=q-next;free(r);}else {r=q-next;q-next=p;pre-next=q;pre=q;q=r;}if(q!=NULL)pre-next=q;free(pb); return head;}/*----主程序------*/void main(){linklist a,b,c;printf("請輸入第一個多項式:");a=creat(); /*創建多項式鏈表a*/printf("請輸入第二個多項式:");b=creat(); /*創建多項式鏈表b*/cout<<"您輸入的第一個多項式為:"< 樓上的到底是c還是C++啊 #include #include #include #include typedef int datatype;typedef struct LNode{ float coef; /*多項式系數*/ int expn; /*多項式指數*/ struct LNode *next;}LNode,*linklist;/*---------創建帶頭結點的多項式鏈表--------*/linklist creat(){ linklist head,s,p,pre; float coef; int expn; head=(linklist)malloc(sizeof(LNode)); /*表頭結點*/ head-next=NULL; printf("輸入系數:"); scanf("%f",&coef); printf("輸入指數:"); scanf("%d",&expn); while (coef!=0。0) { printf("當系數等于零的時候結束!"); s=(linklist)malloc(sizeof(LNode)); /*生成新結點*/ s-coef=coef; s-expn=expn; s-next=NULL; pre=head; /*插入到有序的多項式鏈表中去*/ p=head-next;while (p && p-expn expn) { pre=p; p=p-next; } s-next=p; pre-next=s; printf("讀下一項:\n"); printf("輸入系數:\n"); scanf("%f",&coef); printf("\n"); printf("輸入指數:"); scanf("%d",&expn); }return head;}/*-----------輸出多項式鏈表-------------*/void print(linklist head) { linklist p; p=head-next; while (p) { printf("%fX^%d+",p-coef,p-expn); p=p-next; } }/*-------------多項式相加----------------*/linklist add(linklist pa,linklist pb){ linklist p,q,pre,r,head; float x; head=pa; p=pa-next; //p,q 指向頭接點的下一個接點,即多項式的第一個接點 q=pb-next; pre=pa; //pre指向p的前驅 while((p!=NULL)&&(q!=NULL)) //處理多項式的相加的問題 if(p-expnexpn) { pre=p; p=p-next; } else if (p-expn==q-expn) { x=p-coef+q-coef; if(x!=0) //系數相加不為0的情況 { p-coef=x; pre=p; p=p-next; } else //系數相加為0的情況 { pre-next=p-next; free(p); p=pre-next; } r=q; q=q-next; free(r); } else { r=q-next; q-next=p; pre-next=q; pre=q; q=r; } if(q!=NULL) pre-next=q; free(pb); return head; }/*----主程序------*/void main(){ linklist a,b,c; printf("請輸入第一個多項式:"); a=creat(); /*創建多項式鏈表a*/ printf("請輸入第二個多項式:"); b=creat(); /*創建多項式鏈表b*/ cout<<"您輸入的第一個多項式為:"< #include void main( ){ int x,a,b,c,d,e,f coutabcdefendl;}熱心網友
熱心網友
熱心網友