將一個整數N轉換成字串!(遞迴和非遞迴、及——進位轉化)

來源:互聯網
上載者:User

一、遞迴

(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'; 
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.