The following Code is only for my review. It is less practical than N ~~ Haha:>
//// Convert a string to an integer. // # include <cstdlib> # include <iostream> # include <string> long tointeger (const STD: string & Str) {bool isnegative = false; long result = 0; size_t I; const size_t length = Str. length (); // skip non-numeric or symbolic characters. // for (I = 0; I <length & ('0'> STR [I] | '9' <STR [I]); ++ I) {/// process the negative number. // If ('-' = STR [I] & '0' <= STR [I + 1] & '9'> = STR [I + 1]) {isnegative = true; I ++; break ;}/// process numbers. // whi Le (I <length & '0' <= STR [I] & '9'> = STR [I]) {result = 10 * result + (STR [I ++]-'0');} return true = isnegative? Result *-1: result;} int main (void) {STD: cout <tointeger ("123456") <STD: Endl; STD :: cout <tointeger ("-123456") <STD: Endl; STD: cout <tointeger ("-- 123456-") <STD: Endl; STD:: cout <tointeger ("abBsD-123456") <STD: Endl; STD: cout <tointeger ("abBsD-123456XddeE") <STD: Endl; STD :: cout <tointeger ("abbsd123456xddee") <STD: Endl; return exit_success ;}