将一个整数N转换成字符串!(递归和非递归、及——进制转化)

来源:互联网 发布:马里亚纳网 知乎 编辑:程序博客网 时间:2024/04/27 19:13

一、递归

(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<<"请输入一个整数:"; 
 cin>>nNum; 
 cout<<"输入的是:"; 
 if(nNum<0) 
 { 
  cout<<'-'; nNum=-nNum; 
 } 
 convert(nNum); 
 cout<<endl; 

=====

 

二、非递归

 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 a;
 int n;
 cin>>n;
 a=long_to_string(n);
 cout<<a<<endl;
 return 0;
}

 

 2. 一个非常简易的方法

 

#include <iostream>
using namespace std;

void IntoChar(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;
 IntoChar(p,n);
 delete []p;
 return 0;
}

 

附:进制转化

/////进制转化

/////

#include <iostream> 
#include <cmath> 
#include <string> 
#include <sstream> 
using namespace std; 

template<class T, class U> 
T hex_cast(U u) 

stringstream ss; 
ss.setf(ios_base::uppercase); 
ss << hex << u; 
T 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 << HexToDec("3e7") << '/n'; 
cout << HexToOct("3e7") << '/n'; 
cout << HexToBin("3e7") << '/n'; 
}