編寫一個遞歸函數,將任意的十進制正整數轉換為八進制數,要求用C++語言,遞歸函數
#include
#include
using namespace std;
void Function( int number, string& result )
{
int replace = number;
int value = number % 8;
char ch[ 10 ];
sprintf( ch, "%d", value );
result += ch;
replace /= 8;
if( replace <= 0 )
{
return ;
}
Function( replace, result );
}
void main()
{
int x = 10;
string result;
Function( 10, result );
int index = result.size();
stringstream sstream;
while( index > 0 )
{
string ret;
auto obj = result.substr( index - 1, 1 );
index -= 1;
sstream << obj;
}
string ret;
sstream >> ret;
cout << ret.c_str() << endl;
system( "pause" );
}