TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
digit_separation.cpp File Reference

Separates digits from numbers in forward and reverse order. More...

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdint>
#include <iostream>
#include <vector>
Include dependency graph for digit_separation.cpp:

Go to the source code of this file.

Classes

class  greedy_algorithms::DigitSeparation
 A class that provides methods to separate the digits of a large positive number. More...
 

Namespaces

namespace  greedy_algorithms
 for string class
 

Functions

static void tests ()
 self test implementation
 
int main ()
 main function
 

Detailed Description

Separates digits from numbers in forward and reverse order.

See also
https://www.log2base2.com/c-examples/loop/split-a-number-into-digits-in-c.html

The DigitSeparation class provides two methods to separate the digits of large integers: digitSeparationReverseOrder and digitSeparationForwardOrder. The digitSeparationReverseOrder method extracts digits by repeatedly applying the modulus operation (% 10) to isolate the last digit, then divides the number by 10 to remove it. This process continues until the entire number is broken down into its digits, which are stored in reverse order. If the number is zero, the method directly returns a vector containing {0} to handle this edge case. Negative numbers are handled by taking the absolute value, ensuring consistent behavior regardless of the sign.

Author
Muhammad Junaid Khalid

Definition in file digit_separation.cpp.

Function Documentation

◆ main()

int main ( void )

main function

Returns
0 on successful exit

Definition at line 138 of file digit_separation.cpp.

138 {
139 tests(); // run self test implementation
140
141 return 0;
142}
static void tests()
self test implementation

◆ tests()

static void tests ( )
static

self test implementation

Returns
void

Definition at line 83 of file digit_separation.cpp.

83 {
85
86 // Test case: Positive number
87 std::int64_t number = 1234567890;
88 std::vector<std::int64_t> expectedReverse = {0, 9, 8, 7, 6, 5, 4, 3, 2, 1};
89 std::vector<std::int64_t> expectedForward = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
90 std::vector<std::int64_t> reverseOrder =
92 assert(reverseOrder == expectedReverse);
93 std::vector<std::int64_t> forwardOrder =
95 assert(forwardOrder == expectedForward);
96
97 // Test case: Single digit number
98 number = 5;
99 expectedReverse = {5};
100 expectedForward = {5};
101 reverseOrder = ds.digitSeparationReverseOrder(number);
102 assert(reverseOrder == expectedReverse);
103 forwardOrder = ds.digitSeparationForwardOrder(number);
104 assert(forwardOrder == expectedForward);
105
106 // Test case: Zero
107 number = 0;
108 expectedReverse = {0};
109 expectedForward = {0};
110 reverseOrder = ds.digitSeparationReverseOrder(number);
111 assert(reverseOrder == expectedReverse);
112 forwardOrder = ds.digitSeparationForwardOrder(number);
113 assert(forwardOrder == expectedForward);
114
115 // Test case: Large number
116 number = 987654321012345;
117 expectedReverse = {5, 4, 3, 2, 1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
118 expectedForward = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 1, 2, 3, 4, 5};
119 reverseOrder = ds.digitSeparationReverseOrder(number);
120 assert(reverseOrder == expectedReverse);
121 forwardOrder = ds.digitSeparationForwardOrder(number);
122 assert(forwardOrder == expectedForward);
123
124 // Test case: Negative number
125 number = -987654321012345;
126 expectedReverse = {5, 4, 3, 2, 1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
127 expectedForward = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 1, 2, 3, 4, 5};
128 reverseOrder = ds.digitSeparationReverseOrder(number);
129 assert(reverseOrder == expectedReverse);
130 forwardOrder = ds.digitSeparationForwardOrder(number);
131 assert(forwardOrder == expectedForward);
132}
A class that provides methods to separate the digits of a large positive number.
std::vector< std::int64_t > digitSeparationForwardOrder(std::int64_t largeNumber) const
Implementation of digitSeparationForwardOrder method.
std::vector< std::int64_t > digitSeparationReverseOrder(std::int64_t largeNumber) const
Implementation of digitSeparationReverseOrder method.