/* 功能:将输入的string类型的IP信息转换为string类型
* 输入:string类型的IP信息
* 输出:DWORD结果,正常返回解析结果值,异常时,dwIP为0
* 返回:返回解析的整型,异常时返回"0"
* 输入:string类型的IP信息
* 输出:DWORD结果,正常返回解析结果值,异常时,dwIP为0
* 返回:返回解析的整型,异常时返回"0"
*/
代码:
#include <stdlib.h>
#include <atlstr.h>
#include "oj.h"
#include <iostream>
#include <string>
using namespace std;
bool effective(string strIP)//检查ip地址的有效性
{
int cnt = 0;
char *ip = (char*)strIP.c_str();
while(*ip != '\0')
{
if(*ip == '.')
{
cnt++;
}
if(*ip >= '0' && *ip <= '9' || *ip == '.')
{
ip++;
}
else
{
return false;
}
}
if(cnt != 3)
return false;
return true;
}
string GetValueByIP(string strIP)
{
if(!effective(strIP))
return "0";
unsigned int res = 0;
char resIp[12] = {0};
unsigned char value[4] = {0};//存