解题思路:
深夜背模板真开心~
有了对象还是要面向对象啊~
我对象最好!
注意事项:
参考代码:
#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语言程序设计教程(第三版)课后习题6.5 (Java代码)浏览:1122 |
C语言训练-数字母 (C语言代码)浏览:585 |
C语言程序设计教程(第三版)课后习题6.4 (C语言代码)浏览:553 |
C语言程序设计教程(第三版)课后习题6.8 (C语言代码)浏览:527 |
C语言程序设计教程(第三版)课后习题8.8 (C语言代码)浏览:639 |
C语言程序设计教程(第三版)课后习题3.7 (C语言代码)浏览:262 |
C语言程序设计教程(第三版)课后习题5.5 (C语言代码)浏览:556 |
1024题解浏览:808 |
Tom数 (C语言代码)浏览:555 |
小O的数字 (C语言代码)浏览:1411 |