atoi函數就是把一串字符串轉換爲int型整數的函數,經過將字符串中的字符一個一個強制類型轉換,而且存入一個臨時數組中,再將數組中的數字處理一下便可獲得咱們須要的整數。數組
實現這個函數的過程當中,咱們須要注意負數的處理,要進行一次判斷,肯定返回值的正負。ide
其餘的字符按照ASCII碼錶進行轉換便可。函數
下面是代碼:字符串
#include<stdio.h> #include<math.h> int my_atoi(char a[],int sz) { int i = 0, count = 0, ret = 0, tcount = 0, pm = 1, sum[64]; for (i = 0; i < sz; i++) { int tmp = (int)a[i]; if (45 == tmp) { pm = 0; continue; } if (47 < tmp && 59 > tmp) sum[count++] = tmp - 48; } tcount = count--; for (i = 0; i < tcount; i++) ret+= (pow(10, count--))*(sum[i]); if (0 == pm) return ret*-1; else return ret; } int main() { char a[] = "1233423144"; int i = my_atoi(a,sizeof(a)/sizeof(a[0])); printf("%d\n", i); system("pause"); return 0; }