十年網(wǎng)站開發(fā)經(jīng)驗 + 多家企業(yè)客戶 + 靠譜的建站團(tuán)隊
量身定制 + 運營維護(hù)+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
先上代碼再解釋
#includeusing namespace std;
//鏈表的應(yīng)用-進(jìn)行多項式的相加
//創(chuàng)建一個多項式節(jié)點:
typedef struct Node {int coeff;
int exp;
struct Node* next;
}Poly, * Polyptr;
//有以下幾個函數(shù)
//初始化,打印,測試,添加,相加。
Polyptr Poly_Init()
{Polyptr temp = new Poly;
if (!temp)
{cout<< "申請內(nèi)存失敗!"<< endl;
return NULL;
}
temp->coeff = temp->exp = 0;
temp->next=NULL;
return temp;
}
void Print_Poly(Polyptr temp)
{Polyptr p = temp;
for (; p->next != NULL; p = p->next)
{cout<< p->coeff<< "*10^"<< p->exp<< (p->next->coeff >= 0 ? "+":"");
}
cout<< p->coeff<< "*10^"<< p->exp<< endl;
}
void Poly_Append(Polyptr Zpow, int paracoeff, int paraexp)
{int flag = 10;
if (paraexp< 0)
{cout<< "暫不支持處理冪次小于0的多項式"<< endl;
return;
}
Polyptr p = Zpow, q = NULL;
Polyptr temp = new Poly;
if (!temp)
{cout<< "申請內(nèi)存失敗"<< endl;
return;
}
temp->coeff = paracoeff;
temp->exp = paraexp;
while (p != NULL&¶exp >p->exp)
{q = p;
p = p->next;
}
if (p == NULL) {flag = 0; }
else if (paraexp == p->exp) {flag = 1; }
else if (paraexp< p->exp) {flag = 2;}
switch (flag)
{case 0:
if (q) { q->next = temp;
temp->next = NULL;
}
break;
case 1:
p->coeff += temp->coeff;
delete temp;
if (temp != NULL)
temp = NULL;
break;
case 2:
temp->next = p;
q->next = temp;
break;
default:
cout<< "給我整不會了"<< endl;
break;
}
}
Polyptr Polys_Add(Polyptr poly1, Polyptr poly2)
{Polyptr q = poly2;
if (poly1 == NULL)
return poly2;
else if (poly2 == NULL)
return poly1;
while (q!= NULL)
{Poly_Append(poly1, q->coeff, q->exp);
q = q->next;
}
return poly1;
}
void test()
{Polyptr Zpow1 = Poly_Init();
cout<< "初始化"<< endl;
Poly_Append(Zpow1, 10, 5);
Poly_Append(Zpow1, 6, 3);
Poly_Append(Zpow1, 6, 3);
Poly_Append(Zpow1, -666, 3);
Poly_Append(Zpow1, -666, 13);
Print_Poly(Zpow1);
Polyptr Zpow2 = Poly_Init();
cout<< "初始化"<< endl;
Poly_Append(Zpow2, 10, 5);
Poly_Append(Zpow2, 6, 3);
Poly_Append(Zpow2, 6, 3);
Poly_Append(Zpow2, 5, 3);
Poly_Append(Zpow2, 1, 13);
Print_Poly(Zpow2);
Zpow1=Polys_Add(Zpow1, Zpow2);
Print_Poly(Zpow1);
}
int main()
{test();
return 0;
}
多項式相加是數(shù)據(jù)結(jié)構(gòu)中鏈表的一個擴(kuò)展,通過每一個鏈表來存儲不同次冪的不同系數(shù),達(dá)到一個鏈表存儲一個多項式的效果,既然是鏈表,那么這里面的操作大致都是從鏈表中衍生出來的,所以我們這里只講一下最難的部分:在多項式中添加一個項。
這里是具體代碼:
void Poly_Append(Polyptr Zpow, int paracoeff, int paraexp)
{int flag = 10;
if (paraexp< 0)
{cout<< "暫不支持處理冪次小于0的多項式"<< endl;
return;
}
Polyptr p = Zpow, q = NULL;
Polyptr temp = new Poly;
if (!temp)
{cout<< "申請內(nèi)存失敗"<< endl;
return;
}
temp->coeff = paracoeff;
temp->exp = paraexp;
while (p != NULL&¶exp >p->exp)
{q = p;
p = p->next;
}
if (p == NULL) {flag = 0; }
else if (paraexp == p->exp) {flag = 1; }
else if (paraexp< p->exp) {flag = 2;}
switch (flag)
{case 0:
if (q) { q->next = temp;
temp->next = NULL;
}
break;
case 1:
p->coeff += temp->coeff;
delete temp;
if (temp != NULL)
temp = NULL;
break;
case 2:
temp->next = p;
q->next = temp;
break;
default:
cout<< "給我整不會了"<< endl;
break;
}
}
看完了代碼,我們來細(xì)說這些代碼的作用。
首先是數(shù)據(jù)的合法性檢測,由于本人能力有限,目前只能先寫出冪次為不小于0的多項式,所以這里做了一個多項式冪次的判斷。
if (paraexp< 0)
{cout<< "暫不支持處理冪次小于0的多項式"<< endl;
return;
}
隨后則是老生常談的申請內(nèi)存加初始化了
Polyptr temp = new Poly;
if (!temp)
{cout<< "申請內(nèi)存失敗"<< endl;
return;
}
temp->coeff = paracoeff;
temp->exp = paraexp;
接下來,則是這部分的難點,添加一個項。
首先,我們來談?wù)勎覀兲砑禹椀姆诸惖倪壿嫛?br />第一種,假如所要添加的項的冪次大于原來多項式的所有項,那么我們進(jìn)行一個尾插法即可。
第二種,如果恰好在多項式中有一項的冪次和需要添加的項的冪次相等,那么我們只需要進(jìn)行系數(shù)的相加即可。
第三種,也是最后一種,我們所要添加的項的冪次既不等于多項式中的項的冪次,也不是大的,這時候就要用到我們之前在單鏈表中運用到的根據(jù)位置來插入的方法來添加項了。
講完了大概邏輯,我們可以來看看代碼了。
首先我定義了一個flag變量來標(biāo)志我們要進(jìn)行哪一種操作:int flag = 10;
然后是進(jìn)行迭代判斷,這里我們用q變量作為p的前驅(qū),方便待會的插入操作 :
while (p != NULL&¶exp >p->exp)
{q = p;
p = p->next;
}
if (p == NULL) {flag = 0; }
else if (paraexp == p->exp) {flag = 1; }
else if (paraexp< p->exp) {flag = 2;}
首先我們先判斷循環(huán)是否遍歷完全,如果我們遍歷完全并且需添加項的冪次沒有小于多項式中的話,我們就記錄為0,進(jìn)行尾插法。后面兩個判斷方法也是基于這個。
隨后我們進(jìn)行相關(guān)的操作:
switch (flag)
{case 0:
if (q) { q->next = temp;
temp->next = NULL;
}
break;
case 1:
p->coeff += temp->coeff;
delete temp;
if (temp != NULL)
temp = NULL;
break;
case 2:
temp->next = p;
q->next = temp;
break;
default:
cout<< "給我整不會了"<< endl;
break;
}
以上
你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級服務(wù)器適合批量采購,新人活動首月15元起,快前往官網(wǎng)查看詳情吧