rty

=fd
Last edited on
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.
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.
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);
}
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.
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
Topic archived. No new replies allowed.