解题思路:
背模板,真开心
注意事项:
参考代码:
#include <iostream> #include <algorithm> #include <string> #include <stdio.h> #include <memory.h> #define N 10000 using namespace std; class BigInt { public: BigInt(); BigInt(string numStr); void showInt(); ~BigInt(); friend BigInt operator+(BigInt& a, BigInt& b); private: int d[N]; int len; }; BigInt::BigInt() { len = 0; memset(d, 0x00, sizeof(d)); } BigInt::BigInt(string numStr) { int length = numStr.length(); this->len = length; memset(d, 0x00, sizeof(d)); for (int i = 0; i < length; i++) d[i] = numStr[length - i - 1] - '0'; } BigInt::~BigInt() { } void BigInt::showInt() { for (int i = this->len - 1; i >= 0; i--) cout << d[i]; cout << endl; } BigInt operator+(BigInt& a, BigInt& b) { BigInt c; c.len = a.len > b.len ? a.len : b.len; int i; for (i = 0; i < c.len; i++) { c.d[i] += a.d[i] + b.d[i]; c.d[i + 1] = c.d[i] / 10; c.d[i] %= 10; } c.len = (c.d[i] ? i + 1 : i); return c; } int main(void) { string strA, strB; cin >> strA >> strB; BigInt a(strA); BigInt b(strB); BigInt c = a + b; c.showInt(); return 0; }
0.0分
0 人评分
C语言训练-列出最简真分数序列* (C语言代码)浏览:614 |
C语言程序设计教程(第三版)课后习题1.6 (C语言代码)浏览:545 |
C语言程序设计教程(第三版)课后习题1.5 (C语言代码)浏览:529 |
C语言程序设计教程(第三版)课后习题3.7 (C语言代码)浏览:549 |
Tom数 (C语言代码)浏览:725 |
printf基础练习2 (C语言代码)浏览:503 |
DNA (C语言代码)浏览:741 |
1134题解(求分析)浏览:729 |
A+B for Input-Output Practice (I) (C语言代码)浏览:431 |
C语言程序设计教程(第三版)课后习题8.9 (C语言代码)浏览:508 |