首页 > 代码库 > Convert String to Long

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: Solution14 15 class Solution {16 public:17     // Method: Convert String to Long18     long stringToLong(string s) {19         // Special Case: Empty20         if(s.size()==0)21           return 0;22         long sign;                                          // Record the sign of the long number23         int index=0;24         int strLen = s.size();                              // The length of input25         long result=0;                                      // The final result26         // Discard the whitespaces before the first non-whitespace.27         while(index<strLen && s[index]== ) index++;28 29         // The input only contains whitespaces30         if(index==strLen)31           return 0;32 33         // Determine the sign34         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 input43           return 0;44         else                                                // Unsigned input45           sign = 1;46 47         // Retrieve the digit after first non-whitespace character48         while(index<strLen) {49             if(s[index]>=0 && s[index]<=9) {                                          // New character is 0-950                 int digit = s[index]-0;51                 if(result>LONG_MAX/10 || (result==LONG_MAX/10 && digit>LONG_MAX%10)) {      // Overflow or underflow52                     result = sign==-1?LONG_MIN:LONG_MAX;53                 }54                 else55                    result = result*10+digit;56             }57             else                                                                          // New character is not 0-958                 break;59             index++;60         }61 62 63         return sign*result;64 65     }66     67     // Method: Test68     void test() {69         string testString;70 //        testString = "";                        // Test Case 1: Empty71         testString = "123";                        // Test Case 2: Valid unsigned input72 //        testString = "+123";                    // Test Case 3: Valid signed input73 //        testString = "   123";                  // Test Case  : Valid input with whitespaces74 //        testString = "abc123";                  // Test Case  : Invalid input75 //        testString = "++123";                    // Test Case 4: Invalid signed input76 //        testString = "+123+";                    // Test Case 5: Valid input format with additional characters77 //        testString = "3924x8";                    // Test Case 6: Invalid input format78 //        testString = "1000000000000000000000";         // Test Case 7: Overflow79 //        testString = "-10000000000000000000";          // Test Case 8: Underflow80      cout << stringToLong(testString) << endl;81     }82 };

 

Convert String to Long