rty
Apr 10, 2014 at 11:25am UTC
=fd
Last edited on Apr 18, 2014 at 2:45pm UTC
Apr 10, 2014 at 12:13pm UTC
Quick and dirty solution which does not includes input validity check:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
#include <iostream>
#include <string>
std::string addLargeNumbers(const std::string& lhs, const std::string& rhs)
{
int l = lhs.size() - 1;
int r = rhs.size() - 1;
std::string result;
bool overflow = false ;
while (not (l < 0 && r < 0)) {
int temp = (l < 0 ? 0 : lhs[l--] - '0' ) +
(r < 0 ? 0 : rhs[r--] - '0' ) +
(overflow ? 1 : 0);
overflow = false ;
if (temp > 9) {
overflow = true ;
temp %= 10;
}
result.push_back(temp + '0' );
}
return {result.rbegin(), result.rend()};
}
int main()
{
std::string input1, input2;
std::cin >> input1 >> input2;
std::cout << addLargeNumbers(input1, input2);
}
Trere is more academic solutions which includes: using stacks to manipulate characters, converting number in arbitrary length binary representation.
Apr 11, 2014 at 3:48am UTC
Thank you very much. It's helpful.
But if you could help me with maxsize, it would be great. Or check if out and/or input is less than 20 digits. Thanks again for your help.
Apr 11, 2014 at 8:35am UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
#include <iostream>
#include <string>
#define MAX_SIZE 20
std::string addLargeNumbers(const std::string& lhs, const std::string& rhs)
{
int l = lhs.size() - 1;
int r = rhs.size() - 1;
std::string result;
bool overflow = false ;
while (not (l < 0 && r < 0)) {
int temp = (l < 0 ? 0 : lhs[l--] - '0' ) +
(r < 0 ? 0 : rhs[r--] - '0' ) +
(overflow ? 1 : 0);
overflow = false ;
if (temp > 9) {
overflow = true ;
temp %= 10;
}
result.push_back(temp + '0' );
}
if (result.size() == MAX_SIZE && overflow)
{
std::cout << std::endl << "Output number has more than 20 digits" ;
exit(-1);
}
return {result.rbegin(), result.rend()};
}
int main()
{
std::string input1, input2;
std::cin >> input1;
if (input1.size() > MAX_SIZE)
{
std::cout << std::endl << "Input number has more than 20 digits" ;
exit(-1);
}
std::cin >> input2;
if (input2.size() > MAX_SIZE)
{
std::cout << std::endl << "Input number has more than 20 digits" ;
exit(-1);
}
std::cout << addLargeNumbers(input1, input2);
}
Apr 13, 2014 at 2:36am UTC
g
Last edited on Apr 15, 2014 at 3:45am UTC
Apr 13, 2014 at 2:51am UTC
The only problem is if sum of two numbers is greater than 20 digits, it gives me this:
terminate called after throwing an instance of std::logic_error_S_construct null not valid
This application has requested the Runtime to terminate in an unusual way.
Apr 13, 2014 at 6:02am UTC
1 2 3
string addLargeNumbers(const string& lhs, const string& rhs)
//...
return 0;
You are telling that function will return a string but trying to return 0. Use exit to interrupt program execcution or throw your own exception.
BTW is your "no larger than 20 digits" a
constraint (tells you that both inputs and result will not be larger than 20 digits so you should not bother about larger numbers) or invariant (that means you should check it)?
Also my "quick and dirty" solution contains a bug. Try adding 99 and 99 to see it. To fix, change my original code:
21 22 23 24 25
}
if (overflow)
result.push_back('1' );
return {result.rbegin(), result.rend()};
}
Last edited on Apr 13, 2014 at 6:03am UTC
Topic archived. No new replies allowed.