C++單鏈表實(shí)現(xiàn)大數(shù)加法
本文實(shí)例為大家分享了C++單鏈表實(shí)現(xiàn)大數(shù)加法,供大家參考,具體內(nèi)容如下
Input Format
輸入文件包括兩行。
- 第一行包括一個(gè)正整數(shù),保證位數(shù)不超過1000000。
- 第二行包括一個(gè)正整數(shù),保證位數(shù)不超過1000000。
Output Format
輸出文件包括一行。
- 第一行包括一個(gè)正整數(shù)。
Sample Input
10558
22
Sample Output
10580
#include <iostream> using 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; }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
QT通過C++線程池運(yùn)行Lambda自定義函數(shù)流程詳解
最近在接觸公司的一個(gè)QT桌面項(xiàng)目,其中里面有一個(gè)模塊是使用線程池去運(yùn)行自定義函數(shù)的,自己潛心研究那個(gè)線程池代碼一天,發(fā)現(xiàn)研究不透,看不懂,里面幾乎都是使用C++11的新特性進(jìn)行編寫2022-10-10C語(yǔ)言循環(huán)隊(duì)列的表示與實(shí)現(xiàn)實(shí)例詳解
這篇文章主要介紹了C語(yǔ)言循環(huán)隊(duì)列的表示與實(shí)現(xiàn),對(duì)于數(shù)據(jù)結(jié)構(gòu)與算法的研究很有幫助,需要的朋友可以參考下2014-07-07在C++17中實(shí)現(xiàn)無(wú)鎖數(shù)據(jù)結(jié)構(gòu)的方法詳解
在探索?C++17?中的無(wú)鎖數(shù)據(jù)結(jié)構(gòu)之前,我們首先需要理解無(wú)鎖編程的基本概念及其在現(xiàn)代軟件開發(fā)中的重要性,在這個(gè)章節(jié)中,我們將深入探討無(wú)鎖編程的概念,以及它如何滿足人類對(duì)于更高效、更可靠軟件的本能需求,文中通過代碼示例介紹的非常詳細(xì),感興趣的朋友可以參考下2023-12-12基于C語(yǔ)言實(shí)現(xiàn)的aes256加密算法示例
這篇文章主要介紹了基于C語(yǔ)言實(shí)現(xiàn)的aes256加密算法,結(jié)合具體實(shí)例形式詳細(xì)分析了C語(yǔ)言實(shí)現(xiàn)的aes256加密算法實(shí)現(xiàn)步驟與使用技巧,需要的朋友可以參考下2017-02-02詳解C語(yǔ)言整數(shù)和浮點(diǎn)數(shù)在內(nèi)存中的存儲(chǔ)
這篇文章主要介紹了C語(yǔ)言整數(shù)和浮點(diǎn)數(shù)在內(nèi)存中是如何存儲(chǔ)的,文中有詳細(xì)的代碼示例供大家參考,對(duì)大家了解學(xué)習(xí)C語(yǔ)言整數(shù)和浮點(diǎn)數(shù)在內(nèi)存中的存儲(chǔ)有一定的幫助,需要的朋友可以參考下2024-03-03