Skip to content

Commit c846d8b

Browse files
committed
Documentation added
1 parent 8461184 commit c846d8b

File tree

1 file changed

+35
-14
lines changed

1 file changed

+35
-14
lines changed

greedy_algorithms/binary_addition.cpp

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,32 @@
1+
/**
2+
* @file binary_addition.cpp
3+
* @brief Adds two binary numbers and outputs resulting string
4+
* @see https://www.geeksforgeeks.org/cpp-program-to-add-two-binary-strings/
5+
* @details The algorithm for adding two binary strings works by processing them
6+
* from right to left, similar to manual addition. It starts by determining the
7+
* longer string's length to ensure both strings are fully traversed. For each
8+
* pair of corresponding bits and any carry from the previous addition, it
9+
* calculates the sum. If the sum exceeds 1, a carry is generated for the next
10+
* bit. The results for each bit are collected in a result string, which is
11+
* reversed at the end to present the final binary sum correctly. This method
12+
* efficiently handles varying string lengths and carrying during addition.
13+
* @author [Muhammad Junaid Khalid](https://github.com/mjk22071998)
14+
*/
15+
116
#include <algorithm>
217
#include <cassert>
318
#include <iostream>
419
#include <string>
520

21+
/**
22+
* @namespace
23+
* @brief Greedy Algorithms
24+
*/
25+
namespace greedy_algorithms {
626
/**
727
* A class to perform binary addition of two binary strings.
828
*/
9-
class Solution {
29+
class BinaryAddition {
1030
public:
1131
/**
1232
* Adds two binary strings and returns the result as a binary string.
@@ -43,46 +63,47 @@ class Solution {
4363
return result;
4464
}
4565
};
66+
} // namespace greedy_algorithms
4667

4768
/**
4869
* Function to run tests for the addBinary method.
4970
*/
5071
void runTests() {
51-
Solution solution;
72+
BinaryAddition binaryAddition;
5273

5374
// Test case for two binary strings of equal length without any carry over.
54-
assert(solution.addBinary("1010", "1101") == "10111");
75+
assert(binaryAddition.addBinary("1010", "1101") == "10111");
5576

5677
// Test case for two binary strings of equal length with a carry over.
57-
assert(solution.addBinary("1111", "1111") == "11110");
78+
assert(binaryAddition.addBinary("1111", "1111") == "11110");
5879

5980
// Test case for two binary strings where one is longer than the other.
60-
assert(solution.addBinary("101", "11") == "1000");
81+
assert(binaryAddition.addBinary("101", "11") == "1000");
6182

6283
// Test case for a binary string of all zeros.
63-
assert(solution.addBinary("0", "0") == "0");
84+
assert(binaryAddition.addBinary("0", "0") == "0");
6485

6586
// Test case where both binary strings consist of all ones.
66-
assert(solution.addBinary("1111", "1111") == "11110");
87+
assert(binaryAddition.addBinary("1111", "1111") == "11110");
6788

6889
// Test case where one binary string is zero and the other is non-zero.
69-
assert(solution.addBinary("0", "10101") == "10101");
70-
assert(solution.addBinary("10101", "0") == "10101");
90+
assert(binaryAddition.addBinary("0", "10101") == "10101");
91+
assert(binaryAddition.addBinary("10101", "0") == "10101");
7192

7293
// Test case for large binary numbers with many digits.
73-
assert(solution.addBinary("101010101010101010101010101010",
74-
"110110110110110110110110110110") ==
94+
assert(binaryAddition.addBinary("101010101010101010101010101010",
95+
"110110110110110110110110110110") ==
7596
"1100001100001100001100001100000");
7697

7798
// Test case where one binary string is much longer than the other.
78-
assert(solution.addBinary("1", "11111111") == "100000000");
99+
assert(binaryAddition.addBinary("1", "11111111") == "100000000");
79100

80101
// Test case for adding empty strings (edge case).
81-
assert(solution.addBinary("", "") == "");
102+
assert(binaryAddition.addBinary("", "") == "");
82103

83104
// Test case where both binary strings consist of alternating ones and
84105
// zeros.
85-
assert(solution.addBinary("10101010", "01010101") == "11111111");
106+
assert(binaryAddition.addBinary("10101010", "01010101") == "11111111");
86107

87108
std::cout << "All tests passed!" << std::endl;
88109
}

0 commit comments

Comments
 (0)