-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddBinary.cpp
More file actions
70 lines (70 loc) · 1.89 KB
/
addBinary.cpp
File metadata and controls
70 lines (70 loc) · 1.89 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include<iostream>
#include<algorithm>
std::string add(const std::string &str1, const std::string &str2){
std::string returnValue = "";
int s1Index = str1.size()-1,s2Index = str2.size()-1, carry=0;
while(s1Index>=0 && s2Index>=0){
int currSum = (str1[s1Index] - '0') + (str2[s2Index] - '0') + carry;
//currSun would be 0 1 2 and 3
if(currSum==3){
returnValue.push_back('1');
carry = 1;
}
else if(currSum==2){
returnValue.push_back('0');
carry = 1;
}
else{
//std::cout<<" to_string "<<std::to_string(currSum)<<std::endl;
returnValue+=std::to_string(currSum);
carry = 0;
}
--s1Index;--s2Index;
}
while(s1Index>=0){
int currSum = (str1[s1Index] - '0') + carry;
//currSun would be 0 1 and 2
if(currSum==3){
returnValue.push_back('1');
carry = 1;
}
else if(currSum==2){
returnValue.push_back('0');
carry = 1;
}
else{
returnValue+=std::to_string(currSum);
carry = 0;
}
--s1Index;
}
while(s2Index>=0){
int currSum = (str2[s2Index] - '0') + carry;
//currSun would be 0 1 and 2
if(currSum==3){
returnValue.push_back('1');
carry = 1;
}
else if(currSum==2){
returnValue.push_back('0');
carry = 1;
}
else{
returnValue+=std::to_string(currSum);
carry = 0;
}
--s2Index;
}
if(carry==1){
returnValue.push_back('1');
}
std::reverse(returnValue.begin(),returnValue.end());
return returnValue;
}
int main(){
std::string str1,str2;
std::cin>>str1>>str2;
std::string output = add(str1,str2);
std::cout<<output<<std::endl;
return 0;
}