题目说明:
输入一个表示整数的字符串,把该字符串转换成整数并输出。例如输入字符串"345",则输出整数345。
程序代码:
#include <gtest/gtest.h> using namespace std; int StrToInt(const char* szValue, int nBase = 0, bool* bValid = NULL) { long long nValue = 0; int nSign = 1; if (bValid) { *bValid = false; } if (!szValue) { return 0; } if (nBase < 0 || nBase == 1 || nBase > 36) { return 0; } char cData = *szValue++; while(cData <= ' ') // skip whitespace { cData = *szValue++; } if (cData == '+') { cData = *szValue++; } else if(cData == '-') { cData = *szValue++; nSign = -1; } if (nBase==0) { if (cData != '0') nBase = 10; else if (*szValue == 'x' || *szValue == 'X') nBase = 16; else nBase = 8; } if (nBase == 16) { if (cData == '0' && (*szValue == 'x' || *szValue == 'X')) { szValue++; cData = *szValue++; } } while (cData != '