1. Recursion
(1)
=====
# Include <iostream>
# Include <string>
Using namespace STD;
String dectostr (unsigned N)
{
Return n> 0? Dectostr (N/10) + static_cast <char> (N % 10 + '0 '):"";
}
Int main ()
{
Long n = 12345608;
Cout <dectostr (n) <Endl;
Return 0;
}
=====
(2)
=====
# Include <iostream. h>
Void convert (int n)
{
Int I;
If (I = N/10 )! = 0) convert (I );
Cout <(char) (N % 10 + '0 ');
}
Void main ()
{
Int nnum;
Cout <"enter an integer :";
Cin> nnum;
Cout <"the input is :";
If (nnum <0)
{
Cout <'-'; nnum =-nnum;
}
Convert (nnum );
Cout <Endl;
}
=====
Ii. Non-recursion
1.
# Include <iostream>
# Include <string>
Using namespace STD;
# Define max_string_len 45
String long_to_string (long N)
{
Char STR [max_string_len]; // receive the characters of N
Char * s = STR + sizeof (STR); // point to the next of the last char
Unsigned long m;
If (n <0)
M =-N;
Else
M = N;
* (-- S) = M % 10 + '0 ';
While (M/= 10)
{
* (-- S) = M % 10 + '0 ';
}
If (n <0)
* (-- S) = '-';
Int Len = sizeof (STR)-(S-Str)/sizeof (STR [0]);
Cout <"Len of the Long is:" <Len <Endl;
String result (s, Len );
Return result;
}
Int main ()
{
String;
Int N;
Cin> N;
A = long_to_string (N );
Cout <A <Endl;
Return 0;
}
2. A very simple method
# Include <iostream>
Using namespace STD;
Void expect char (char * P, long N)
{
Char buffer [20];
Long temp = n> 0? N:-N;
Int I = 0;
While (temp)
{
Buffer [I] = (TEMP % 10) + '0 ';
Temp/= 10;
I ++;
}
Int Len = I;
For (int K = 0; k <Len; k ++)
P [k] = buffer [len-1-k];
P [k + 1] = '/0 ';
If (n> 0)
Cout <p <Endl;
Else
Cout <"-" <p <Endl;
}
Int main ()
{
Long N;
Char * P = new char [20];
Cout <"input a integer :";
Cin> N;
Char (p, N );
Delete [] P;
Return 0;
}
Appendix: hexadecimal conversion
//// Hexadecimal conversion
/////
# Include <iostream>
# Include <cmath>
# Include <string>
# Include <sstream>
Using namespace STD;
Template <class T, Class U>
T hex_cast (U)
{
Stringstream SS;
SS. SETF (ios_base: uppercase );
SS T;
SS> T;
Return T;
}
Double bintodec (INT bin, int y =-1)
{
Return bin> 0? Bin % 10 * POW (2.0, Y + 1) + bintodec (bin/10, Y + 1): 0;
}
String dectohex (INT dec)
{
Return dec> 16? Dectohex (DEC/16) + hex_cast <string> (Dec % 16): "0x" + hex_cast <string> (DEC );
}
Double dectooct (INT Dec, int y =-1)
{
Return dec> = 8? Dectooct (DEC/8, Y + 1) + dec % 8 * POW (10.0, Y + 1): Dec * POW (10.0, Y + 1 );
}
Double dectobin (INT Dec, int y =-1)
{
Return dec> 0? Dectobin (DEC/2, Y + 1) + dec % 2 * POW (10.0, Y + 1): Dec;
}
Double bintooct (INT bin)
{
Return dectooct (bintodec (BIN ));
}
Double hextodec (string HEX)
{
Return Hex. Size ()> 0?
Hex_cast <int> (hex [0]) * POW (16.0, static_cast <int> (hex. size ()-1) + hextodec (hex. substr (1): 0;
}
Double hextooct (string HEX)
{
Return dectooct (hextodec (HEX ));
}
Double hextobin (string HEX)
{
Return dectobin (hextodec (HEX ));
}
String bintohex (INT bin)
{
Return dectohex (bintodec (BIN ));
}
Int main ()
{
Cout. SETF (ios_base: fixed );
Cout. Precision (0 );
Cout <bintodec (1111100111) <'/N ';
Cout <bintooct (1111100111) <'/N ';
Cout <bintohex (1111100111) <'/N ';
Cout <dectohex (999) <'/N ';
Cout <dectooct (999) <Endl ;;
Cout <dectobin (999) <'/N ';
Cout Cout Cout }