atoi(),atof(),atol();iato(),fato(),lato()---字符串和數字互相轉換

1.double atof(const char *nptr);
把字符串轉換成浮點數值。
<math.h>或<stdlib.h>
nptr:帶轉換的字符串。
若無溢出,返回字符串的雙精度浮點數值。


int atoi(const char* nptr);
把字符串轉換成整型數。
<stdlib.h>
nptr:帶轉換的字符串。
成功轉換,返回字符串的整數值;若無法轉換返回0.


long atol(const char* nptr);
將字符串轉換成長整形數值。
<stdlib.h>
nptr: 待轉換的字符串。
若無溢出,返回字符串的長整形數值。


2.數字轉化爲字符串

  ● itoa():將整型值轉換爲字符串。
  ● ltoa():將長整型值轉換爲字符串。
  ● ultoa():將無符號長整型值轉換爲字符串。
  ● gcvt():將浮點型數轉換爲字符串,取四捨五入。
  ● ecvt():將雙精度浮點型值轉換爲字符串,轉換結果中不包含十進制小數點。
  ● fcvt():指定位數爲轉換精度,其餘同ecvt()。

還可以使用sprintf系列函數把數字轉換成字符串,其比itoa()系列函數運行速度慢。下列程序演示瞭如何使用itoa()函數和gcvt()函數:

# include <stdio.h>
# include <stdlib.h>

int main ()
{
  int num_int = 435;
    double num_double = 435.10f;
    char str_int[30];
    char str_double[30];

    itoa(num_int, str_int, 10);  //把整數num_int轉成字符串str_int   參數 10表示按十進制類型進行轉換
    gcvt(num_double, 8, str_double);  //把浮點數num_double轉成字符串str_double   參數8表示精確位數

    printf("str_int: %s\n", str_int);
    printf("str_double: %s\n", str_double);

   return 0;
}
結果:
str_int: 435
str_double: 435.10001

如果不使用atoi或sprintf等庫函數,可以通過把整數的各位上的數字加“0”轉換成char類型並存到字符數組中。但是要注意,需要採用字符串逆序的方法。如以下程序所示:

1    #include <iostream>
2    using namespace std;
3   
4    void int2str(int n, char *str)
5    {
6        char buf[10] = "";
7        int i = 0;
8        int len = 0;
9        int temp = n < 0 ? -n: n;  // temp爲n的絕對值
10  
11       if (str == NULL)
12       {
13           return;
14       }
15       while(temp)
16       {
17           buf[i++] = (temp % 10) + '0';  //把temp的每一位上的數存入buf
18           temp = temp / 10;
19       }
20  
21       len = n < 0 ? ++i: i;  //如果n是負數,則多需要一位來存儲負號
22       str[i] = 0;            //末尾是結束符0
23       while(1)
24       {
25           i--;
26           if (buf[len-i-1] ==0)
27           {
28               break;
29           }
30           str[i] = buf[len-i-1];  //把buf數組裏的字符拷到字符串
31       }
32       if (i == 0 )
33       {
34           str[i] = '-';          //如果是負數,添加一個負號
35       }
36   }
37  
38   int main()
39   {
40       int nNum;
41       char p[10];
42  
43       cout << "Please input an integer:";
44       cin >> nNum;
45       cout << "output: " ;
46       int2str(nNum, p);        //整型轉換成字符串
47       cout<< p << endl;
48  
49       return 0;
50   }
結果:

程序中的int2str函數完成了int類型到字符串類型的轉換。在代碼第46行對int2str函數做了測試。程序的執行結果如下所示:
Please input an integer: 1234
Output: 1234
如果輸入的是個負數,程序執行結果如下所示:
Please input an integer: -1234
Output: -1234

3. 字符串轉化爲數字

  ● atof():將字符串轉換爲雙精度浮點型值。
  ● atoi():將字符串轉換爲整型值。
  ● atol():將字符串轉換爲長整型值。
  ● strtod():將字符串轉換爲雙精度浮點型值,並報告不能被轉換的所有剩餘數字。
  ● strtol():將字符串轉換爲長整值,並報告不能被轉換的所有剩餘數字。
  ● strtoul():將字符串轉換爲無符號長整型值,並報告不能被轉換的所有剩餘數字。

以下程序演示如何使用atoi ()函數和atof ()函數:

1    # include <stdio.h>
2    # include <stdlib.h>
3   
4    int main ()
5    {
6        int num_int;
7        double num_double;
8        char str_int[30] = "435";         //將要被轉換爲整型的字符串
9        char str_double[30] = "436.55";  //將要被轉換爲浮點型的字符串
10  
11       num_int = atoi(str_int);          //轉換爲整型值
12       num_double = atof(str_double);  //轉換爲浮點型值
13  
14       printf("num_int: %d\n", num_int);
15       printf("num_double: %lf\n", num_double);
16  
17       return 0;
18   }
結果:
num_int: 435
num_double: 436.550000

不使用庫函數將字符串轉換爲數字:

1    #include <iostream>
2    using namespace std;
3   
4    int str2int(const char *str)
5    {
6        int temp = 0;
7        const char *ptr = str;  //ptr保存str字符串開頭
8   
9        if (*str == '-' || *str == '+')  //如果第一個字符是正負號,
10       {                      //則移到下一個字符
11           str++;
12       }
13       while(*str != 0)
14       {
15           if ((*str < '0') || (*str > '9'))  //如果當前字符不是數字
16           {                       //則退出循環
17               break;
18           }
19           temp = temp * 10 + (*str - '0'); //如果當前字符是數字則計算數值
20           str++;      //移到下一個字符
21       }   
22       if (*ptr == '-')     //如果字符串是以“-”開頭,則轉換成其相反數
23       {
24           temp = -temp;
25       }
26  
27       return temp;
28   }
29  
30   int main()
31   {
32       int n = 0;   
33       char p[10] = "";
34  
35       cin.getline(p, 20);   //從終端獲取一個字符串
36       n = str2int(p);      //把字符串轉換成整型數
37       
38       cout << n << endl;
39  
40       return 0;
41   }
程序執行結果:
輸入:1234
輸出:1234
輸入:-1234
輸出:-1234
輸入:+1234
輸出:1234


參考:http://blog.sina.com.cn/s/blog_4c8a2a870100qgq7.html





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章