写了个ip地址的分段提取的类,大家看看代码,给点建议
class Cip{
private:
int segment[4];
char *str;
int StrToInt(char *str);
bool CheckIp(char *str);
public:
int GetSeg(int sequence);
bool SetSeg(int sequence);
char GetIpType();
Cip(char *str_target);
~Cip();
};//solve an ipv4 address string
int Cip::GetSeg(int sequence)
{
if(sequence <= 4 && sequence >= 1)
return segment[sequence];
else
return -1;
}
bool Cip::CipCheckIp(char *str)
{
int dot_count = 0;
int num_count = 0;
while((*str))
{
if((*str) != '.')
{
if((*str) <= '9' && (*str) >= '0')
++ num_count;
else
return false;
}
else{
++ dot_count;
if(num_count < 1 || num_count > 4)//check if the current segment is 1~3 numbers
return false;
num_count = 0;//reset to 0
}
++ str;
}
if(dot_count != 3)
return false;
return true;
}
Cip::Cip(char *str_target)
{
int i = 0;
for(;i < 4;i++)
segment[i] = 0;
str = new char[15];//an ip string buffer
if(strlen(str_target) > 6 && strlen(str_target) < 16)//error check,x.x.x.x,min=6,max=15
strcpy(str,str_target);
else
strcpy(str,'\0');
}
Cip::~Cip()
{
delete[] str;
}
int Cip::StrToInt(char *str)
{
char *guard = str;
int cur_seg = 0;
int cur_quan = 0;
while((*guard) != '\0')
++ guard;
-- guard;
while(guard >= str)
{
if((*guard) != '.')
{
cur_int += ((*guard) - '0')*cur_quan*10;
++ cur_quan;
}
else{
segment[cur_seg] = cur_int;
cur_int = 0;//reset to 0
++ cur_seg;//segment index increase itself
cur_quan = 0;//reset to 0
}
-- guard;
}
}
private:
int segment[4];
char *str;
int StrToInt(char *str);
bool CheckIp(char *str);
public:
int GetSeg(int sequence);
bool SetSeg(int sequence);
char GetIpType();
Cip(char *str_target);
~Cip();
};//solve an ipv4 address string
int Cip::GetSeg(int sequence)
{
if(sequence <= 4 && sequence >= 1)
return segment[sequence];
else
return -1;
}
bool Cip::CipCheckIp(char *str)
{
int dot_count = 0;
int num_count = 0;
while((*str))
{
if((*str) != '.')
{
if((*str) <= '9' && (*str) >= '0')
++ num_count;
else
return false;
}
else{
++ dot_count;
if(num_count < 1 || num_count > 4)//check if the current segment is 1~3 numbers
return false;
num_count = 0;//reset to 0
}
++ str;
}
if(dot_count != 3)
return false;
return true;
}
Cip::Cip(char *str_target)
{
int i = 0;
for(;i < 4;i++)
segment[i] = 0;
str = new char[15];//an ip string buffer
if(strlen(str_target) > 6 && strlen(str_target) < 16)//error check,x.x.x.x,min=6,max=15
strcpy(str,str_target);
else
strcpy(str,'\0');
}
Cip::~Cip()
{
delete[] str;
}
int Cip::StrToInt(char *str)
{
char *guard = str;
int cur_seg = 0;
int cur_quan = 0;
while((*guard) != '\0')
++ guard;
-- guard;
while(guard >= str)
{
if((*guard) != '.')
{
cur_int += ((*guard) - '0')*cur_quan*10;
++ cur_quan;
}
else{
segment[cur_seg] = cur_int;
cur_int = 0;//reset to 0
++ cur_seg;//segment index increase itself
cur_quan = 0;//reset to 0
}
-- guard;
}
}
...全文
相关推荐
IP地址分类以及C类IP地址的子网划分
国际规定:把所有的IP地址划分为 A,B,C,D,E
IP地址的分类 abcde类是如何划分的
现在的IP网络使用32位地址,以点分十进制表示,如172.16.0.0。地址格式为:IP地址=网络地址+主机地址 或 IP地址=主机地址+子网地址+主机地址,需要的朋友可以参考下
A、B、C、D和E类IP地址
IP地址由四段组成,每个字段是一个字节,8位,最大值是255,, IP地址由两部分组成,即网络地址和主机地址。网络地址表示其属于互联网的哪一个网络,主机地址表示其属于该网络中的哪一台主机。二者是主从关系。 IP地址的四大类型标识的是网络中的某台主机。IPv4的地址长度为32位,共4个字节,但实际中我们用点分十进制记法。
A,B,C类的IP地址详细介绍
本文将详细介绍A,B,C类的IP地址是什么意思,需要了解的朋友可以参考下
python如何判断IP地址合法性
主要为大家详细介绍了python如何判断IP地址合法性,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下


