#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <string>
#include <vector>
#include <list>
using namespace std;
const int maxn = 1000;
struct bign
{
int len, s[maxn]; //s是逆序存储
bign(){ memset(s, 0 , sizeof(s)); len = 1 ; } //构造函数
bign(int num) { *this = num ; } //初始化
bign(const char* num) { *this = num ; }
string str() const
{
int i;
string res = "";
for( i = 0 ; i < len ; i++ )
res = (char)( s[i] + '0' ) + res ;
if (res == "") res = "0";
return res;
}
bign operator = (const char* num)
{
int i;
len = strlen(num);
for( i = 0 ; i < len ; i++ )
s[i] = num[len-i-1] - '0' ;
return *this;
}
bign operator = (int num)
{
char s[maxn];
sprintf(s , "%d" , num);
*this = s;
return *this;
}
bign operator + (const bign& b) const
{
bign c;
c.len = 0;
int i, g;
for( i = 0 , g = 0 ; g || i < ( len > b.len ? len : b.len ) ; i++ )
{
int x = g ;
if( i < len ) x += s[i] ;
if( i < b.len ) x += b.s[i] ;
c.s[c.len++] = x % 10 ;
g = x / 10 ;
}
return c;
}
};
istream& operator >> (istream &in , bign& x)
{
string s;
in >> s;
x = s.c_str();
return in;
}
ostream& operator << (ostream &out , const bign& x)
{
out << x.str();
return out;
}
int main()
{
bign a,b;
cin >> a;
cin >> b;
cout << (a + b) << endl;
return 1;
}
第7章例题--2981--大整数加法
最新推荐文章于 2020-07-04 11:18:39 发布