C++單鏈表實現(xiàn)大數(shù)加法
更新時間:2020年05月27日 11:57:01 作者:Alex山南水北
這篇文章主要為大家詳細介紹了C++單鏈表實現(xiàn)大數(shù)加法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了C++單鏈表實現(xiàn)大數(shù)加法,供大家參考,具體內(nèi)容如下
Input Format
輸入文件包括兩行。
- 第一行包括一個正整數(shù),保證位數(shù)不超過1000000。
- 第二行包括一個正整數(shù),保證位數(shù)不超過1000000。
Output Format
輸出文件包括一行。
- 第一行包括一個正整數(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;
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C/C++實現(xiàn)獲取系統(tǒng)時間的示例代碼
C 標準庫提供了 time() 函數(shù)與 localtime() 函數(shù)可以獲取到當前系統(tǒng)的日歷時間。本文將通過一些簡單的示例為大家講講C++獲取系統(tǒng)時間的具體方法,需要的可以參考一下2022-12-12
C++數(shù)據(jù)結(jié)構(gòu)與算法之哈夫曼樹的實現(xiàn)方法
這篇文章主要介紹了C++數(shù)據(jù)結(jié)構(gòu)與算法之哈夫曼樹的實現(xiàn)方法,簡單說明了哈夫曼樹的原理,并結(jié)合具體實例形式分析了C++實現(xiàn)哈夫曼樹的相關(guān)操作技巧,需要的朋友可以參考下2017-11-11
C語言實例真題講解數(shù)據(jù)結(jié)構(gòu)中單向環(huán)形鏈表
鏈表可以說是一種最為基礎(chǔ)的數(shù)據(jù)結(jié)構(gòu)了,而單向鏈表更是基礎(chǔ)中的基礎(chǔ)。鏈表是由一組元素以特定的順序組合或鏈接在一起的,不同元素之間在邏輯上相鄰,但是在物理上并不一定相鄰。在維護一組數(shù)據(jù)集合時,就可以使用鏈表,這一點和數(shù)組很相似2022-04-04

