算法描述:依次扫描字符串,检测到第一个数字或正负符号时开始做类型转换,之后检测到非数字或结束符 \0 时停止转换,返回整型数。
注意测试用例:
1. 不规则输入,但是有效,”-3924x8fc”,” + 413”,
2. 无效格式,” ++c”, ” ++1”
3. 溢出数据,”2147483648”
#include "stdafx.h"
#include<iostream>
#include<string>
#define MAX 2147483647
using namespace std;
int _tmain()
{
string str;
char flag='+';
long long num=0;
string::size_type index=0,i=0;
cin>>str;
if(str.empty())
{
cout<<"the number is not exist!"<<endl;
return 0;
}
while(index!=str.size())
if(str[index]<'0'||str[index]>'9')
++index;
else break;
if(index==str.size())
{
cout<<"the number is not exist!"<<endl;
return 0;
}
if(index>=1&&str[index-1]=='-')