Enter a string such as "+1234", convert it to the corresponding number 1234 output, if "1234", then output-1234:
#include <stdio.h>enum charec{true,false};int check = false;int my_atoi ( CONST&NBSP;CHAR&NBSP;*STR) { int num = 0; if (str != null) && (*str != ' + ') { int minus = 0; if (*str == ' + ') str++; else if (*str == '-') { str++; minus = 1; } while (*str != ') { if ((*str >= ' 0 ') && (*str <= ' 9 ')) { int flag = minus?-1:1; num = (num*10) +flag* ( *str-' 0 '); if ((!minus&& (num>0x7fffffff)) | | (minus&& ((signed int) num<0x80000000))) { num = 0; break; } str++; } else { num = 0; break; } } if (*str == ') { check = true; } } return num;} Int main () { char str[50]; int ret = 0; scanf ("%s", str); ret = my_atoi (str); printf ("%d\n", ret); return 0;}
For the input string "0" output result 0 and the input empty string output is also the same as 0 of the two cases, deliberately set a global enumeration constant, so that you can check the value of the enumeration constant to differentiate, but also to consider the input string converted to the corresponding number outside the range of int.
This article is from the "Knock Code good Sleep zzz" blog, please be sure to keep this source http://2627lounuo.blog.51cto.com/10696599/1707218
The implementation converts a string into a corresponding shaping number