```
/******************************************************************************************************
* < BigInteger Addition > using strings;
*
* Written by Mathias Van Malderen;
* Copyright (c) 2009 Mathias Van Malderen;
*
* NOTE: -> It's NOT allowed to sell this source code to anyone !!!
* -> You MAY use this code for any purpose, but on your own risk ...
*
*******************************************************************************************************/
#include <iostream>
#include <string>
using namespace std;
inline int ab(int a);
inline int min(int a, int b);
inline int max(int a, int b);
short str2digit(const string &str);
string digit2str(short digit);
void reverseStr(string &str);
void addZeros(string &str, int n);
string addition(string &s1, string &s2);
bool checkInt(const string &str);
int main()
{
string num1, num2;
cout << "Enter a very long number: ";
cin >> num1;
cout << "Enter another very long number: ";
cin >> num2;
cout << endl << endl;
if(checkInt(num1) && checkInt(num2))
{
cout << "Sum = " << addition(num1, num2) << endl;
} else {
cout << "You have to enter two valid integers !" << endl;
}
return 0;
}
string addition(string &s1, string &s2)
{
/* Calculate the sum of two (big) numbers (=integers) */
if(s1.length() != s2.length())
{
int diff = ab(s1.length()-s2.length());
if(max(s1.length(),s2.length()) == s1.length())
{
addZeros(s2,diff);
} else {
addZeros(s1,diff);
}
}
string solution;
string s_dig1;
string s_dig2;
int s = 0;
bool carry = false;
reverseStr(s1);
reverseStr(s2);
for(int i = 0; i < static_cast<int>(s1.length()); i++)
{
s_dig1 = s1[i];
s_dig2 = s2[i];
s = str2digit(s_dig1) + str2digit(s_dig2);
if(carry)
{
s++;
carry = false;
}
if(s > 9)
{
solution += digit2str(s-10);
carry = true;
} else {
solution += digit2str(s);
}
}
/* This code was added as a BUGFIX */
/* Try to run this function without this bugfix with two arguments of length 1 and you'll get an incorrect result */
/* For instance if you run this function (without this bugfix) with "5" and "9" as it's arguments, you'll get "4" as answer */
/* BEGIN_OF_FIX */
if(carry)
{
solution += "1";
}
/* END_OF_FIX */
reverseStr(solution);
return solution;
}
inline int ab(int a)
{
/* Return the absolute value of an integer */
return a>0?a:-a;
}
inline int max(int a, int b)
{
/* Return the highest number of the two */
return a>b?a:b;
}
inline int min(int a, int b)
{
/* Return the lowest number of the two */
return a<b?a:b;
}
void addZeros(string &str, int n)
{
/* Add n zeros to the beginning of the string */
string tmp;
for(int i = 0; i < n; i++)
{
tmp = tmp + "0";
}
str = tmp + str;
}
short str2digit(const string &str)
{
/* Convert a string to a digit */
if(str.length() > 1) return -1;
switch(str[0])
{
case '0':
return 0;
case '1':
return 1;
case '2':
return 2;
case '3':
return 3;
case '4':
return 4;
case '5':
return 5;
case '6':
return 6;
case '7':
return 7;
case '8':
return 8;
case '9':
return 9;
default:
return -2;
}
}
string digit2str(short digit)
{
/* Convert a digit to a string */
switch(digit)
{
case 0:
return "0";
case 1:
return "1";
case 2:
return "2";
case 3:
return "3";
case 4:
return "4";
case 5:
return "5";
case 6:
return "6";
case 7:
return "7";
case 8:
return "8";
case 9:
return "9";
default:
return "";
}
}
void reverseStr(string &str)
{
/* Reverse a string */
int strlen;
strlen = str.length();
string revstr;
if(strlen != 0)
{
for(int i=1; i<strlen+1; i++)
{
revstr = revstr + str[strlen-i];
}
}
str = revstr;
}
bool checkInt(const string &str)
{
/* Check whether the string contains a valid integer */
string tmp_s;
for(int i = 0; i < static_cast<int>(str.length()); i++)
{
tmp_s = str[i];
if(str2digit(tmp_s) == -1 || str2digit(tmp_s) == -2)
{
return false;
}
}
return true;
}
```

**Are you able to help answer this sponsored question?**

Questions asked by members who have earned a lot of community kudos are featured in order to give back and encourage quality replies.

Recommended Articles