大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
这篇文章主要为大家展示了C++单链表如何实现大数加法,内容简而易懂,希望大家可以学习一下,学习完之后肯定会有收获的,下面让小编带大家一起来看看吧。
创新互联建站是一家专注于成都网站建设、网站制作与策划设计,惠济网站建设哪家好?创新互联建站做网站,专注于网站建设10余年,网设计领域的专业建站公司;建站业务涵盖:惠济等地区。惠济做网站价格咨询: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; }
另外有需要云服务器可以了解下创新互联建站www.cdcxhl.com,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。