天天看點

程式目的:實作c++整型和字元數組,字元串的互相轉化

環境:dev-c++

源代碼:

#include <iostream>

#include <cstdlib>

using namespace std;

main()

{

     int i1=123,i2,i3;

     float f1;

     char c1[20]="234.45",c2[20];

     string s1="456",s2;

      i2=atoi(c1);      //将字元數組轉化為整數

     f1=atof(c1);      //将字元數組轉化為浮點型

     itoa(i1,c2,10);   //将整型轉化為字元數組  10為10進制的意思 

     cout<<"字元數組轉化為整型: "<<i2<<endl;

     cout<<"字元數組轉化為浮點型: "<<f1<<endl;

     cout<<"整型轉化為字元數組: "<<c2<<endl;

     strncpy(c2,s1.c_str(),s1.length());//字元串轉化為字元數組 

     i3=atoi(c2);

     cout<<"字元串轉化為整型: "<<i3<<endl; 

     system("pause");

}

截圖:

程式目的:實作c++整型和字元數組,字元串的互相轉化

本文轉自茄子_2008部落格園部落格,原文連結:http://www.cnblogs.com/xd502djj/archive/2012/03/09/2387742.html,如需轉載請自行聯系原作者。

繼續閱讀