這篇文章主要為大家展示了C++單鏈表如何實(shí)現(xiàn)大數(shù)加法,內(nèi)容簡(jiǎn)而易懂,希望大家可以學(xué)習(xí)一下,學(xué)習(xí)完之后肯定會(huì)有收獲的,下面讓小編帶大家一起來(lái)看看吧。
創(chuàng)新互聯(lián)建站是一家專注于成都網(wǎng)站建設(shè)、網(wǎng)站制作與策劃設(shè)計(jì),惠濟(jì)網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)建站做網(wǎng)站,專注于網(wǎng)站建設(shè)10余年,網(wǎng)設(shè)計(jì)領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:惠濟(jì)等地區(qū)。惠濟(jì)做網(wǎng)站價(jià)格咨詢:18982081108Input Format
輸入文件包括兩行。
Output Format
輸出文件包括一行。
Sample Input
10558
22
Sample Output
10580
#includeusing namespace std; class BigData { friend ostream &operator<<(ostream &os, const BigData &x); friend istream &operator>>(istream &is, BigData &x); friend BigData operator+(BigData a, BigData b); private: struct node { int data; node *next; node(const short &x, node *n = NULL) { data = x; next = n; } }; node *num; void clear(); public: BigData(node *p = NULL) { if (p == NULL) { num = new node(0); } else { num = p; }; } BigData(const BigData &); ~BigData() { clear(); } BigData &operator=(const BigData &); }; BigData::BigData(const BigData &x) { num = new node(x.num->data); node *p = num, *q = x.num; while (q->next != NULL) { q = q->next; p->next = new node(q->data); p = p->next; } } void BigData::clear() { node *p = num, *q; while (p != NULL) { q = p; p = p->next; delete q; } num = NULL; } BigData operator+(BigData a, BigData b) { BigData tmp; BigData::node *p, *q, *end; int carry; tmp.num = end = new BigData::node(a.num->data + b.num->data); carry = tmp.num->data / 10; tmp.num->data %= 10; p = a.num->next; q = b.num->next; end = tmp.num; while (p != NULL && q != NULL) { end->next = new BigData::node(p->data + q->data + carry); end = end->next; carry = end->data / 10; end->data %= 10; p = p->next; q = q->next; } if (p == NULL)p = q; while (p != NULL) { end->next = new BigData::node(p->data + carry); end = end->next; carry = end->data / 10; end->data %= 10; p = p->next; } if (carry != 0) { end->next = new BigData::node(carry); return tmp; } } BigData &BigData::operator=(const BigData &x) { if (&x == this)return *this; clear(); num = new node(x.num->data); node *p = num, *q = x.num; while (q->next != NULL) { q = q->next; p->next = new node(q->data); p = p->next; } return *this; } istream &operator>>(istream &is, BigData &x) { char ch; x.clear(); while ((ch = is.get()) != '\n') { x.num = new BigData::node(ch - '0', x.num); } return is; } ostream &operator<<(ostream &os, const BigData &x) { string s; BigData::node *p = x.num; while (p != NULL) { s = char(p->data + '0') + s; p = p->next; } for (int i = 0; i < s.size(); ++i)os << s[i]; return os; } int main() { BigData a, b, c; cin >> a >> b; c = a + b; cout << c; }
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)建站www.cdcxhl.com,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。