程序目的:實現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");
}
截圖:

 

摘自http://hi.baidu.com/isokill/blog/item/7e0620d6146f11c9a8ec9a5a.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM