您好,登錄后才能下訂單哦!
這篇文章主要介紹了如何用C語言代碼實現(xiàn)多項式相加的相關(guān)知識,內(nèi)容詳細(xì)易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇如何用C語言代碼實現(xiàn)多項式相加文章都會有所收獲,下面我們一起來看看吧。
具體代碼如下
//多項式的相加和相乘 #include<stdio.h> #include<stdlib.h> #pragma warning(disable:4996)//兼容scanf typedef struct node { int coef; int expon; struct node* link; }Polynode,*Polynomial; Polynomial InsertPolyLinklist(Polynomial in,Polynomial Pread) { Pread->link = in; Pread = in; in->link = NULL; return Pread; } Polynomial ReadPoly(void) { Polynomial Pread = (Polynomial)malloc(sizeof(Polynode)); Pread->link = NULL; Polynomial H = Pread; int N; scanf("%d ", &N); while (N--) { Polynomial p = (Polynomial)malloc(sizeof(Polynode)); scanf("%d %d", &p->coef, &p->expon); Pread= InsertPolyLinklist(p,Pread); } Polynomial F; F = H->link; free(H); return F; } void PrintPoly(Polynomial F) { while(F != NULL) { printf("%d %d ", F->coef, F->expon); F = F->link; } printf("\n"); } Polynomial Add(Polynomial p1, Polynomial p2) { Polynomial t1=p1,t2=p2; Polynomial p=(Polynomial)malloc(sizeof(Polynode)); p->link = NULL; Polynomial q = p; Polynomial read; while (t1&&t2) { if (t1->expon == t2->expon) { if (t1->coef + t2->coef) { t1->coef = t1->coef + t2->coef; t1->expon = t1->expon; read = t1; q->link = read; q = read; t1 = t1->link; t2 = t2->link; } } else { if (t1->expon > t2->expon){ read = t1; q->link = read; q = read; t1 = t1->link; } else { if (t1->expon < t2->expon) { read = t2; q->link = read; q = read; t2 = t2->link; } } } } if (t1) { q->link = t1; } if (t2) { q->link = t2; } Polynomial F = p->link; free(p); return F; } int main(void) { Polynomial p1, p2, pp, ps; p1 = ReadPoly(); PrintPoly(p1); p2 = ReadPoly(); PrintPoly(p2); pp = Add(p1, p2); PrintPoly(pp); // ps = Mult(p1, p2); // PrintPoly(ps); return 0; }
關(guān)于“如何用C語言代碼實現(xiàn)多項式相加”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對“如何用C語言代碼實現(xiàn)多項式相加”知識都有一定的了解,大家如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。