字串轉整數

2021-08-08 23:12:45 字數 661 閱讀 5948

題目

題目也沒給樣例,做起來覺得怪怪的,注意以下幾點之後就ac啦

需要去掉首尾空字元

需要判斷符號

碰到非數字字元就捨棄

#include 

#include

using namespace std;

/*atoi (表示 ascii to integer)

把字串轉換成整型數的乙個函式

1、需要去掉首尾空字元

2、需要判斷符號

3、碰到非數字字元就捨棄

*/class

solution else

if(str[0] == '+')

//計算值

for( ; i < str.length(); i++)

result = result * 10 + t;

if(result * flag > int_max) return int_max;

if(result * flag < int_min) return int_min;

}result = result * flag;

return (int)result;

}};int main()

在庫函式中本來就有乙個atoi函式能夠將字串轉為整數

原始碼

整數轉字串

將輸入的整數轉化為字串。輸入 整數 輸出 指向字串的指標 函式原型 char shuzi2zifu int n include include includechar shuzi2zifu int n else flag 0 int m n while n printf d n count p ch...

字串轉整數

題目描述 輸入乙個由數字組成的字串,把它轉換成整數並輸出。例如 輸入字串 123 輸出整數123。給定函式原型int strtoint const char str 實現字串轉換成整數的功能,不能使用庫函式atoi。分析與解答 本題考查的實際上就是字串轉換成整數的問題,或者說是要你自行實現atoi函...

字串轉整數

字串轉整數 user hihone date 2019 2 2 time 10 58 description 首先,該函式會根據需要丟棄無用的開頭空格字元,直到尋找到第乙個非空格的字元為止。當我們尋找到的第乙個非空字元為正或者負號時,則將該符號與之後面盡可能多的連續數字組合起來,作為該整數的正負號 ...