[Microsoft面试]题目:输入一个表示整数的字符串,把该字符串转换成整数并输出。
例如输入字符串&quot;345&quot;,则输出整数345。 Thisquestion checks how the interviewee is familiar with C/C++? I’m so bad atC/C++...intatoi(char * str) {int neg = 0;char * p = str;if (*p == ‘-’) { p++; neg = 1;} else if (*p == ‘+’) { p++;}int num = 0;while (*p != ‘\0’) { if (*p>=&#39;0&#39; && *p <= &#39;9&#39;) { num = num * 10 + (*p-’0’); } else { error(“illegal number”); } p++;}return num;}PS: I didn’t figure out how to tell a overflow problem easily.
页:
[1]