### rty

=fd
Last edited on
Quick and dirty solution which does not includes input validity check:
 ``12345678910111213141516171819202122232425262728293031`` ``````#include #include 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.
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.
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748`` ``````#include #include #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); }``````
g
Last edited on
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.
 ``123`` ``````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:
 ``2122232425`` `````` } if (overflow) result.push_back('1'); return {result.rbegin(), result.rend()}; }``````
Last edited on
Topic archived. No new replies allowed.