Convert String to Long

问题:

Given a string, write a routine that converts the string to a long, without using the built in functions that would do this. Describe what (if any) limitations the code has. 

代码:

 1 /*

 2  * Author: Min Li

 3  * Discussion:

 4  *    1. Return 0 when input is not valid (Empty or wrong format)

 5  *    2. Return LONG_MAX when input string overflows

 6  *    3. Return LONG_MIN when input string underflows

 7  *    4. Input String is allowed to have additional character after a valid substring that can form a long number. (e.g. +123+)

 8  *    5. Input can have many whitespaces before the first non-whitespace character. (e.g. "    123")

 9  *

10  */

11 

12 

13 // Class: Solution

14 

15 class Solution {

16 public:

17     // Method: Convert String to Long

18     long stringToLong(string s) {

19         // Special Case: Empty

20         if(s.size()==0)

21           return 0;

22         long sign;                                          // Record the sign of the long number

23         int index=0;

24         int strLen = s.size();                              // The length of input

25         long result=0;                                      // The final result

26         // Discard the whitespaces before the first non-whitespace.

27         while(index<strLen && s[index]==' ') index++;

28 

29         // The input only contains whitespaces

30         if(index==strLen)

31           return 0;

32 

33         // Determine the sign

34         if(s[index]=='-') {                                 // Input starts with "-"

35           sign = -1;

36           ++index;

37         }

38         else if(s[index]=='+') {                            // Input starts with "+"

39           sign = 1;

40           ++index;

41         }

42         else if(s[index] < '0' || s[index] > '9')           // Invalid input

43           return 0;

44         else                                                // Unsigned input

45           sign = 1;

46 

47         // Retrieve the digit after first non-whitespace character

48         while(index<strLen) {

49             if(s[index]>='0' && s[index]<='9') {                                          // New character is 0-9

50                 int digit = s[index]-'0';

51                 if(result>LONG_MAX/10 || (result==LONG_MAX/10 && digit>LONG_MAX%10)) {      // Overflow or underflow

52                     result = sign==-1?LONG_MIN:LONG_MAX;

53                 }

54                 else

55                    result = result*10+digit;

56             }

57             else                                                                          // New character is not 0-9

58                 break;

59             index++;

60         }

61 

62 

63         return sign*result;

64 

65     }

66     

67     // Method: Test

68     void test() {

69         string testString;

70 //        testString = "";                        // Test Case 1: Empty

71         testString = "123";                        // Test Case 2: Valid unsigned input

72 //        testString = "+123";                    // Test Case 3: Valid signed input

73 //        testString = "   123";                  // Test Case  : Valid input with whitespaces

74 //        testString = "abc123";                  // Test Case  : Invalid input

75 //        testString = "++123";                    // Test Case 4: Invalid signed input

76 //        testString = "+123+";                    // Test Case 5: Valid input format with additional characters

77 //        testString = "3924x8";                    // Test Case 6: Invalid input format

78 //        testString = "1000000000000000000000";         // Test Case 7: Overflow

79 //        testString = "-10000000000000000000";          // Test Case 8: Underflow

80      cout << stringToLong(testString) << endl;

81     }

82 };

 

你可能感兴趣的:(convert)