Write function implement library function Atoi, convert string to shaping # include <stdio.h> #include <string.h> #include <assert.h> #include < Ctype.h>long long Calculate (const char *SRC, int flag) {long Long num = 0;while (*SRC) {if (*src >= ' 0 ') && (*src <= ' 9 ')) Determine if the input is a non-array character {num = num * + (*SRC-' 0 '); if (flag = = 1 && num>0x7fffffff) | | (Flag = =-1 && num < (signed int) 0x80000000)) Consider the number too big {num = 0;break;} src++;} Else{num = 0;break;}} return flag * NUM;} Long Long str_to_int (const char *str) {long Long num = 0;if (str = NULL && *str! = ') ') {while (Isspace (*STR)) {str ++;} int flag = 1;if (*str = = ' + ') str++;else if (*str = = '-') {str++;flag =-1;} if (*str! = ') "{num = calculate (str, flag);}} return num;} int main () {printf ("%lld\n", Str_to_int ("+2345"));p rintf ("%lld\n", Str_to_int (" -2345"));p rintf ("%lld\n", Str_to_int ("+2345")); printf ("%lld\n", Str_to_int (" -2345"));p rintf ("%lld\n", Str_to_int ("2345"));p rintf ("%lld\n", Str_to_int ("- 111111111 "));p rintf ("%lld\n ", str_to_int (""));p rintf ("%lld\n", Str_to_int ("123ab")); return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"C Language" write function implement library function atoi, convert string into shaping (consider number too large)