练习时,查询到CSDN有为博主提供了IP的判断算法,如连接http://blog.csdn.net/dzyhenry/article/details/8138322
提交后,未通过验证,几经改进,通过了测试,但只拿到及格分数60.
代码如下:请各位指点一二
int IsValueValid(char IP[])
{
char *p = IP;
if(p[0] <'0' ||p[0] >'9')
return 0;
int Len,Integer, DotCnt;
Len=Integer=DotCnt=0;
char LastChar;
while(*p !='\0')
{
if((*p)>='0' &&(*p) <='9')
{
Integer= Integer*10 + *p -'0';
Len++;
if(Len >3)
return 0;
}
elseif(*p !='.' ||Integer > 255 ||((*p =='.')&& LastChar == '.' ))
return 0;
else
{
DotCnt++;
Integer= 0;
Len= 0;
}
LastChar= *p;
p++;
}
if(DotCnt ==3)
return1;
else
return0;
}
int main()
{
char IpAddress[18];
cout << "Please input the ip address: \n";
cin >> IpAddress;
if(IsValueValid(IpAddress))
cout<<"YES";
else
cout <<"NO";
return0;
}