返回信息流最好是 C/C++ 的, 万谢!
这是一条镜像帖。来源:北邮人论坛 / acm-icpc / #91689同步于 2016/11/21
该镜像源已超过 30 天没有更新,可能在源站已被删除。
ACM_ICPC机器人发帖
谁能给个判断IP地址合法性的算法,在网上找的好像不对
byr10086
2016/11/21镜像同步7 回复
订阅后,新回复会通过你的通知中心匿名送达。
7 条回复
bool isValidIpAddress(char *ipAddress)
{
struct sockaddr_in sa;
int result = inet_pton(AF_INET, ipAddress, &(sa.sin_addr));
return result != 0;
}
头文件:
#include <arpa/inet.h>
顺便写个正则表达式也可以验证。
大概长这样:
/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/
这个正则表达式的规则是
210.110 – must have 4 octets
255 – must have 4 octets
y.y.y.y – only digits are allowed
255.0.0.y – only digits are allowed
666.10.10.20 – octet number must be between [0-255]
4444.11.11.11 – octet number must be between [0-255]
33.3333.33.3 – octet number must be between [0-255]
【 在 Sluggard 的大作中提到: 】
: bool isValidIpAddress(char *ipAddress)
: {
: struct sockaddr_in sa
;
: ...................
多谢,不想直接调用系统接口,希望找能有一段实现的代码
先split,判断是不是4个,然后对每个都tryParse,都可以的话再判断0-255
v6的话复杂一点,因为有简写的格式,不过也不难
正则也可以
发自「贵邮」
```c
/* int
* inet_pton4(src, dst)
* like inet_aton() but without all the hexadecimal and shorthand.
* return:
* 1 if `src' is a valid dotted quad, else 0.
* notice:
* does not touch `dst' unless it's returning 1.
* author:
* Paul Vixie, 1996.
*/
static int
inet_pton4(src, dst)
const char *src;
u_char *dst;
{
static const char digits[] = "0123456789";
int saw_digit, octets, ch;
u_char tmp[INADDRSZ], *tp;
saw_digit = 0;
octets = 0;
*(tp = tmp) = 0;
while ((ch = *src++) != '\0') {
const char *pch;
if ((pch = strchr(digits, ch)) != NULL) {
u_int new = *tp * 10 + (pch - digits);
if (new > 255)
return (0);
*tp = new;
if (! saw_digit) {
if (++octets > 4)
return (0);
saw_digit = 1;
}
} else if (ch == '.' && saw_digit) {
if (octets == 4)
return (0);
*++tp = 0;
saw_digit = 0;
} else
return (0);
}
if (octets < 4)
return (0);
/* bcopy(tmp, dst, INADDRSZ); */
memcpy(dst, tmp, INADDRSZ);
return (1);
}
```
【 在 byr10086 的大作中提到: 】
: ;
: 多谢,不想直接调用系统接口,希望找能有一段实现的代码
#include <iostream>
#include <cstring> //memset
using namespace std;
int isIpAddress(char ip[])
{
int ipNum[4]; //可能越界
int ipNumFlag[4];//记录四个数字是否都出现,避免出现12.3..4这种ip
int dotcount = 0;//记录匹配到的'.'的个数
int numpos = 0;
memset(ipNum, 0, sizeof(int)*4);
memset(ipNumFlag, 0, sizeof(int)*4);
//保证交替出现
for(int i = 0; i < strlen(ip); i++)
{
if(ip[i] == '.')//点
{
dotcount++;
//对付1.2.3.4.5这样的坑爹数据
//我就是没考虑这种样例,导致ipNumFlag越界所以运行时异常。
if(dotcount > 3)
{
return 0;
}
}
else if(ip[i] >= '0' && ip[i] <= '9')//数字
{
ipNum[dotcount] = 10 * ipNum[dotcount] + (ip[i] - '0');
ipNumFlag[dotcount] = 1;
}
else //其他字符
{
return 0;
}
}
//保证4个数(3个点),且数的范围在0~255之间
for(int i = 0; i < 4; i++)
{
if(ipNumFlag[i] == 0 || ipNum[i] > 255 || ipNum[i] < 0)
{
return 0;
}
}
//保证数字
for(int i = 0; i < 4; i++)
{
if(ipNum[i] > 255)
{
return 0;
}
}
return 1;
}
int main()
{
int T;
cin >> T;
char ipstr[16];
while(T--)
{
cin >> ipstr;
if(isIpAddress(ipstr))
{
cout << "Yes" << endl;
}
else
{
cout << "No" << endl;
}
}
return 0;
}