string work(string a,string b)
{
int l1=a.size(),l2=b.size();
for(int i=0;i<l1/2;i++){char ch=a[i];a[i]=a[l1-1-i];a[l1-i-1]=ch;}
for(int i=0;i<l2/2;i++){char ch=b[i];b[i]=b[l2-1-i];b[l2-i-1]=ch;}
string c;
if(l1>=l2) {
c=a;
for(int i=0;i<l2;i++) {
c[i]+=b[i]-'0';
if(c[i]>'9')
{
if(i+1==c.size()) { c[i+1]='1';}
else {c[i+1]+=1;}
}
}
}
else {
c=b;
for(int i=0;i<l1;i++) {
c[i]+=a[i]-'0';
if(c[i]>'9')
{
if(i+1==c.size()) { c[i+1]='1';}
else {c[i+1]+=1;}
}
}
}
int l3=c.size();
for(int i=0;i<l3/2;i++){char ch=c[i];c[i]=c[l3-i-1];c[l3-i-1]=ch;}
return c;
}
请各位指点一下,写了好多次都写不对
#include <iostream>
#include <string>
#include <iterator>
class BigInt
{
public:
BigInt(unsigned int n = 0)
{
if (n == 0)
_data.push_back('0');
else
{
while (n)
{
_data.push_back(n % 10 + '0');
n /= 10;
}
}
}
BigInt(const std::string &s) : _data(s.rbegin(), s.rend()) {}
BigInt &operator+=(const BigInt &other);
BigInt operator+(const BigInt &other) const
{
BigInt t = *this;
t += other;
return t;
}
private:
std::string _data;
friend std::istream &operator>>(std::istream &is, BigInt &a);
friend std::ostream &operator<<(std::ostream &os, const BigInt &a);
};
BigInt &BigInt::operator+=(const BigInt &other)
{
int carry = 0;
auto itr1 = _data.begin();
auto itr2 = other._data.begin();
while (itr1 != _data.end() && itr2 != other._data.end())
{
int a = *itr1 - '0';
int b = *itr2 - '0';
int c = a + b + carry;
carry = c / 10;
c %= 10;
*itr1 = c + '0';
++itr1;
++itr2;
}
while (itr1 != _data.end())
{
int a = *itr1 - '0' + carry;
carry = a / 10;
a %= 10;
*itr1 = a + '0';
++itr1;
}
while (itr2 != other._data.end())
{
int a = *itr2 - '0' + carry;
carry = a / 10;
a %= 10;
_data.push_back(a + '0');
++itr2;
}
if (carry > 0)
_data.push_back(carry + '0');
return *this;
}
std::istream &operator>>(std::istream &is, BigInt &a)
{
std::string s;
is >> s;
a = s;
return is;
}
std::ostream &operator<<(std::ostream &os, const BigInt &a)
{
std::copy(a._data.rbegin(), a._data.rend(), std::ostream_iterator<char>(os));
return os;
}
int main()
{
BigInt a, b;
std::cin >> a >> b;
std::cout << a + b << std::endl;
return 0;
}
$ g++ -Wall main.cpp
$ ./a.out
1234567890123456789012345678901234567890
9999999999999999999999999999999999999999
11234567890123456789012345678901234567889
#include <iostream>
#include <string>
#include <iterator>
class BigInt
{
public:
BigInt(unsigned int n = 0)
{
if (n == 0)
_data.push_back('0');
else
{
while (n)
{
_data.push_back(n % 10 + '0');
n /= 10;
}
}
}
BigInt(const std::string &s) : _data(s.rbegin(), s.rend()) {}
BigInt &operator+=(const BigInt &other);
BigInt operator+(const BigInt &other) const
{
BigInt t = *this;
t += other;
return t;
}
private:
std::string _data;
friend std::istream &operator>>(std::istream &is, BigInt &a);
friend std::ostream &operator<<(std::ostream &os, const BigInt &a);
};
BigInt &BigInt::operator+=(const BigInt &other)
{
int carry = 0;
auto itr1 = _data.begin();
auto itr2 = other._data.begin();
while (itr1 != _data.end() && itr2 != other._data.end())
{
int a = *itr1 - '0';
int b = *itr2 - '0';
int c = a + b + carry;
carry = c / 10;
c %= 10;
*itr1 = c + '0';
++itr1;
++itr2;
}
while (itr1 != _data.end())
{
int a = *itr1 - '0' + carry;
carry = a / 10;
a %= 10;
*itr1 = a + '0';
++itr1;
}
while (itr2 != other._data.end())
{
int a = *itr2 - '0' + carry;
carry = a / 10;
a %= 10;
_data.push_back(a + '0');
++itr2;
}
if (carry > 0)
_data.push_back(carry + '0');
return *this;
}
std::istream &operator>>(std::istream &is, BigInt &a)
{
std::string s;
is >> s;
a = s;
return is;
}
std::ostream &operator<<(std::ostream &os, const BigInt &a)
{
std::copy(a._data.rbegin(), a._data.rend(), std::ostream_iterator<char>(os));
return os;
}
int main()
{
BigInt a, b;
std::cin >> a >> b;
std::cout << a + b << std::endl;
return 0;
}
$ g++ -Wall main.cpp
$ ./a.out
1234567890123456789012345678901234567890
9999999999999999999999999999999999999999
11234567890123456789012345678901234567889