/** Author: klchang
* Date: 2018.2.26
* Description: Print digits from 1 to the maximum n digits.*/#include#include
//Check if the string contains illegal characters
bool checkDigitString(std::stringnumeric_str)
{bool isLegal = true;
std::basic_string::iterator iter =numeric_str.begin();for (; iter != numeric_str.end(); ++iter) {char ch = *iter;if (ch < '0' || ch > '9') {
isLegal= false;break;
}
}returnisLegal;
}//Remove the leading zeros in a numeric string
std::string removeLeadingZeros(std::stringnumeric_str)
{int i = 0;
size_t len=numeric_str.length();//Return null string when including illegal characters
if (!checkDigitString(numeric_str)) {
std::cout<< "Input string" << numeric_str << "contains at least an illegal character." <<:endl>
}for (; i < len; ++i) {if (!(numeric_str[i] == '0'))break;
}if (i >=len) {
numeric_str= "0";
}else{
numeric_str=numeric_str.substr(i);
}returnnumeric_str;
}//Simulate the numeric operation that numeric string adds one
std::string incrementByOne(std::string&numeric_str)
{
size_t len=numeric_str.size();
std::stringoutput_str(numeric_str);if (len <= 0)returnoutput_str;int carry = 0;bool lowest_bit = true;
std::basic_string::reverse_iterator riter =output_str.rbegin();for (; riter != output_str.rend(); ++riter) {int value = *riter - '0';if(lowest_bit) {
lowest_bit= false;
value++;
}
value+=carry;
carry= 0; //clear carry
if (value > 9) {
carry= 1;
value-= 10;
}*riter = '0' + value; //update correspondent characters
if (carry <= 0) break;
}//pass the length of number string
if (carry > 0) {
output_str= std::string("1") +output_str;
}returnoutput_str;
}//Compare the two numeric strings//Return value: int,//1 when s1 > s2; 0 when s1 == s2; -1 when s1 < s2
int compare(std::string s1, std::strings2)
{int result = 0;
std::stringvalid_s1, valid_s2;
valid_s1=removeLeadingZeros(s1);
valid_s2=removeLeadingZeros(s2);
size_t len_1=valid_s1.size();
size_t len_2=valid_s2.size();if (len_1 >len_2) {
result= 1;
}else if (len_1
result= -1;
}else{
std::basic_string::iterator iter1 =valid_s1.begin();
std::basic_string::iterator iter2 =valid_s2.begin();for (; iter1 != valid_s1.end(); ++iter1, ++iter2 ) {if (*iter1 == *iter2) {continue;
}else if (*iter1 > *iter2) {
result= 1;
}else{
result= -1;
}break;
}
}returnresult;
}//Print the digits without the leading zeros
void printDigits(std::stringdigits)
{
std::string out_str =removeLeadingZeros(digits);if (out_str != "0") {
std::cout<< out_str <<:endl>
}
}//Print N digits of length `length` starting from index start
void printNDigitsRecursively(char* digits, int length, intstart)
{if (length ==start) {
std::string cur_number =digits;
printDigits(cur_number);return;
}//Set the digit of the start index
for (int i = 0; i < 10; ++i) {
digits[start]= i + '0';
printNDigitsRecursively(digits, length, start+1);
}
}//print digits from 1 to maximum
void printNDigits(int n, int method=0)
{if (n <= 0) {
std::cout<< "ERROR: Illegal parameters n <= 0!" <<:endl>
}if (method == 1) {//Recursive method
std::cout << "\nUse the recursive method to print the numbers from 1 to maximum n digits:" <<:endl start="1;char*" digits="new" char>
digits[n]= '\0'; //easy to forget to add the '\0' to the C string
for (int i = 0; i < 10; ++i) {
digits[0] = i + '0'; //Convert digit to character digits
printNDigitsRecursively(digits, n, start);
}delete[] digits;
}else{//Simulation of the integer self-incrementation operation method//for (i = 0; i < 10; ++i) print i
std::cout << "\nSimulate the self incrementation of the integer:" <<:endl maximum integer in string form>
std::string maxNdigits("");for (int i = 0; i < n; ++i)
maxNdigits+= "9";
std::string cur_num = "1";do{
std::cout<< cur_num <<:endl>
cur_num=incrementByOne(cur_num);
}while (compare(maxNdigits, cur_num) >= 0);
std::cout<<:endl>
}
}voidunitest()
{int n = 3;
printNDigits(n,1); //recursive method
printNDigits(n, 0); //simulation method
}intmain()
{
unitest();return 0;
}