forked from vaibhavpathak999/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_addition.cpp
More file actions
38 lines (34 loc) · 939 Bytes
/
Copy pathstring_addition.cpp
File metadata and controls
38 lines (34 loc) · 939 Bytes
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
#include <string>
#include <iostream>
#include <cassert>
std::string strAdd(std::string s, std::string r)
{
int re = 0;
std::string sum;
// precondition for empty strings
assert(s.length() > 0 && r.length() > 0);
if (r.length() < s.length())
r.insert(r.begin(), s.length() - r.length(), '0');
else if (r.length() > s.length())
s.insert(s.begin(), r.length() - s.length(), '0');
for (int i = s.length() - 1; i >= 0; --i)
{
int a = (int(s[i] + r[i]) + re - 96);
sum.insert(sum.begin(), char(a % 10 + 48));
re = a / 10;
}
if (re != 0)
sum.insert(sum.begin(), char(re + 48));
return sum;
}
int main()
{
std::string s;
std::string c;
std::cout << "Enter Number One" << "\n";
std::cin >> s;
std::cout << "Enter Number Two" << "\n";
std::cin >> c;
std::cout << "Addition Result: " << strAdd(s, c) << "\n";
return 0;
}