|
| 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 | + |
1 | 16 | #include <algorithm>
|
2 | 17 | #include <cassert>
|
3 | 18 | #include <iostream>
|
4 | 19 | #include <string>
|
5 | 20 |
|
| 21 | +/** |
| 22 | + * @namespace |
| 23 | + * @brief Greedy Algorithms |
| 24 | + */ |
| 25 | +namespace greedy_algorithms { |
6 | 26 | /**
|
7 | 27 | * A class to perform binary addition of two binary strings.
|
8 | 28 | */
|
9 |
| -class Solution { |
| 29 | +class BinaryAddition { |
10 | 30 | public:
|
11 | 31 | /**
|
12 | 32 | * Adds two binary strings and returns the result as a binary string.
|
@@ -43,46 +63,47 @@ class Solution {
|
43 | 63 | return result;
|
44 | 64 | }
|
45 | 65 | };
|
| 66 | +} // namespace greedy_algorithms |
46 | 67 |
|
47 | 68 | /**
|
48 | 69 | * Function to run tests for the addBinary method.
|
49 | 70 | */
|
50 | 71 | void runTests() {
|
51 |
| - Solution solution; |
| 72 | + BinaryAddition binaryAddition; |
52 | 73 |
|
53 | 74 | // 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"); |
55 | 76 |
|
56 | 77 | // 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"); |
58 | 79 |
|
59 | 80 | // 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"); |
61 | 82 |
|
62 | 83 | // Test case for a binary string of all zeros.
|
63 |
| - assert(solution.addBinary("0", "0") == "0"); |
| 84 | + assert(binaryAddition.addBinary("0", "0") == "0"); |
64 | 85 |
|
65 | 86 | // Test case where both binary strings consist of all ones.
|
66 |
| - assert(solution.addBinary("1111", "1111") == "11110"); |
| 87 | + assert(binaryAddition.addBinary("1111", "1111") == "11110"); |
67 | 88 |
|
68 | 89 | // 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"); |
71 | 92 |
|
72 | 93 | // Test case for large binary numbers with many digits.
|
73 |
| - assert(solution.addBinary("101010101010101010101010101010", |
74 |
| - "110110110110110110110110110110") == |
| 94 | + assert(binaryAddition.addBinary("101010101010101010101010101010", |
| 95 | + "110110110110110110110110110110") == |
75 | 96 | "1100001100001100001100001100000");
|
76 | 97 |
|
77 | 98 | // 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"); |
79 | 100 |
|
80 | 101 | // Test case for adding empty strings (edge case).
|
81 |
| - assert(solution.addBinary("", "") == ""); |
| 102 | + assert(binaryAddition.addBinary("", "") == ""); |
82 | 103 |
|
83 | 104 | // Test case where both binary strings consist of alternating ones and
|
84 | 105 | // zeros.
|
85 |
| - assert(solution.addBinary("10101010", "01010101") == "11111111"); |
| 106 | + assert(binaryAddition.addBinary("10101010", "01010101") == "11111111"); |
86 | 107 |
|
87 | 108 | std::cout << "All tests passed!" << std::endl;
|
88 | 109 | }
|
|
0 commit comments