[问答题]

A+B Problem

Time Limit: lSec  Input File: aplusb.in

Calculate a+ b.

lnput ·

The input will consist of a series of pairs of integers a and b,separated by one line, Each integer may consist of less than 79 digits.

Output

For each pair of input integers a and b you should output the sum of a and b in one line.
Sample lnput

1

5000000000000000000000000000000000000000000000000000000000000000

-1

23

Sample Output

5000000000000000000000000000000000000000000000000000000000000001

22

```//===================================
//Number.h
//大整数类
//===================================
#include<iostream>
#include<string>
using namespace std;
//-----------------------------------
class Number{
string s;
void comple(string& s);          //s取补
void stretch();                        //s位伸展
string shrink()const;             //s位压缩
enum { Num=80 };
public:
Number():s(Num,'0'){}
Number(const string& a):s(a){ stretch(); }
void add(const Number& a);     //s+=a   设计为protected也许更为合适
friend Number operator+(const Number& a, const Number& b);
friend istream& operator>>(istream& cin, Number& a);
friend ostream operator<<(ostream& cout,const Number& a);
};//---------------------------------

```//===================================
//Number.cpp
//===================================
#include"Number.h"
#include<iostream>
#include<string>
using namespace std;
//-----------------------------------
Number operator+(const Number& a, const Number& b){
Number w(a);
return w;
}//----------------------------------
istream& operator>>(istream& cin, Number& a){
cin>>a.s;
a.stretch();
return cin;
}//----------------------------------
ostream& operator<<(ostream& cout, const Number& a){
return cout<<a.shrink();
}//----------------------------------
void Number::add(const Number& a){      // s+=a按位加
for(int i=s.length()-1,tmp=0; i>=0; --i, tmp/=10){
tmp += a.s[i]-'0'+ s[i]-'0';
s[i] = tmp%10 + '0';
}
}//----------------------------------
void Number::comple(string& t){            //十进制数取补
for(int i=0; i<t.length(); ++i)              //取反
t[i] = 105 - t[i];           // '0'+'9'=105
int i=t.length()-1;
while(i>=0 && t[i]=='9')                    //+1
t[i--]='0';
t[i]++;
}//----------------------------------
void Number::stretch(){             //伸展至Num位
if(s[0] !='-')
s = string(Num-s.length(),'0')+s;
else{
s[0]='0';
comple();
s = string(Num-s.length(),'9')+s;
}
}//----------------------------------
string Number::shrink()const{              // 按有效位压缩
string t(s);
if(t[0]=='9')
comple(t);
int pos = t.find_first_not_of('0');
return pos<0 ? string("0") : (s[0]=='9'? "-":"")+t.substr(pos);
}//----------------------------------```

```//===================================
//EX1102.cpp
//大整数A+B对象版
//===================================
#include"Number.h"
#include<fstream>
#include<iostream>
using namespace std;
//-----------------------------------
int main() {
ifstream cin("aplusb.in");
for(Number a,b; cin>>a>>b; )
cout<<a+b<<"\n";
}//==================================```

1条回答 544浏览

# 相关试题

• 扫描二维码，关注牛客网

• 下载牛客APP，随时随地刷题