问题是这样子的:
Implement atoi to convert a string to an integer.
Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases.
Notes: It is intended for this problem to be specified vaguely (ie, no given input specs). You are responsible to gather all the input requirements up front.
平时写程序过于急躁,思考不深入,写这个看似容易的程序真是死了一大批脑细胞啊!!!反思,对于数值处理的问题,以及字符串处理的问题。
最重要的是:会不会溢出,会不会越界!!!
以下是我的代码,感觉略臭略长。如有大神帮改,感激不尽啊!!!
class Solution { public: int atoi(const char *str) { int ret = 0; const char *fStr = str; int signflag = 1; int signCount = 0; int intCount = 0; if(fStr == NULL ) { return 0; } while(*fStr != '