Created
January 18, 2014 07:35
-
-
Save non-static/8487448 to your computer and use it in GitHub Desktop.
String to Integer (atoi) 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…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// atoi.cpp : Defines the entry point for the console application. | |
// | |
#include "stdafx.h" | |
#include <iostream> | |
using namespace std; | |
class Solution | |
{ | |
public: | |
int atoi(const char *str) | |
{ | |
bool positive = true; | |
if (str == NULL) | |
return 0; | |
int i = 0; | |
int result = 0; | |
while ((str[i] != NULL) && ((str[i] == ' ') || (str[i] == '\t') || (str[i] == '\n') || (str[i] == '\r'))) | |
i++; | |
if (str[i] == '-') | |
{ | |
positive = false; | |
i++; | |
} | |
else if (str[i] == '+') | |
{ | |
positive = true; | |
i++; | |
} | |
while (str[i] != NULL) | |
{ | |
// Hit invalid char | |
if ((str[i] < '0') || (str[i] > '9')) | |
break; | |
if ((2147483647 - (str[i] - '0')) / 10 < result) | |
{ | |
result = (positive) ? 2147483647 : -2147483648; | |
break; | |
} | |
result = result * 10 + (str[i] - '0'); | |
i++; | |
} | |
return (positive) ? result : -1 * result; | |
} | |
}; | |
int _tmain(int argc, _TCHAR* argv[]) | |
{ | |
Solution s; | |
int result = 0; | |
char *input = NULL; | |
input = NULL; | |
result = s.atoi(input); | |
cout << ((input == NULL) ? "" : input) << " -- " << result << endl; | |
input = ""; | |
result = s.atoi(input); | |
cout << ((input == NULL) ? "" : input) << " -- " << result << endl; | |
input = "0"; | |
result = s.atoi(input); | |
cout << ((input == NULL) ? "" : input) << " -- " << result << endl; | |
input = "-0"; | |
result = s.atoi(input); | |
cout << ((input == NULL) ? "" : input) << " -- " << result << endl; | |
input = "1"; | |
result = s.atoi(input); | |
cout << ((input == NULL) ? "" : input) << " -- " << result << endl; | |
input = "-1"; | |
result = s.atoi(input); | |
cout << ((input == NULL) ? "" : input) << " -- " << result << endl; | |
input = "+1"; | |
result = s.atoi(input); | |
cout << ((input == NULL) ? "" : input) << " -- " << result << endl; | |
input = " 1"; | |
result = s.atoi(input); | |
cout << ((input == NULL) ? "" : input) << " -- " << result << endl; | |
input = " -1"; | |
result = s.atoi(input); | |
cout << ((input == NULL) ? "" : input) << " -- " << result << endl; | |
input = " +11"; | |
result = s.atoi(input); | |
cout << ((input == NULL) ? "" : input) << " -- " << result << endl; | |
input = "123"; | |
result = s.atoi(input); | |
cout << ((input == NULL) ? "" : input) << " -- " << result << endl; | |
input = "q123"; | |
result = s.atoi(input); | |
cout << ((input == NULL) ? "" : input) << " -- " << result << endl; | |
input = "123q"; | |
result = s.atoi(input); | |
cout << ((input == NULL) ? "" : input) << " -- " << result << endl; | |
input = "123e456"; | |
result = s.atoi(input); | |
cout << ((input == NULL) ? "" : input) << " -- " << result << endl; | |
input = "-+123q"; | |
result = s.atoi(input); | |
cout << ((input == NULL) ? "" : input) << " -- " << result << endl; | |
input = "2147483648"; | |
result = s.atoi(input); | |
cout << ((input == NULL) ? "" : input) << " -- " << result << endl; | |
input = "-2147483648"; | |
result = s.atoi(input); | |
cout << ((input == NULL) ? "" : input) << " -- " << result << endl; | |
input = "-2147483649"; | |
result = s.atoi(input); | |
cout << ((input == NULL) ? "" : input) << " -- " << result << endl; | |
input = " -11919730356x"; | |
result = s.atoi(input); | |
cout << ((input == NULL) ? "" : input) << " -- " << result << endl; | |
input = " 10522545459"; | |
result = s.atoi(input); | |
cout << ((input == NULL) ? "" : input) << " -- " << result << endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment