写了个ip地址的分段提取的类,大家看看代码,给点建议

tarsen 2005-08-05 10:18:20
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;
}
}
...全文
274 26 打赏 收藏 转发到动态 举报
写回复
用AI写文章
26 条回复
切换为时间正序
请发表友善的回复…
发表回复
yhbttfile 2005-08-23
  • 打赏
  • 举报
回复
呵呵,就这么一个小小的功能,是否真有必要写这么多代码?
个人意见仅供参考哈~~
xiaocai0001 2005-08-23
  • 打赏
  • 举报
回复
楼主该结帖啦~~~
tarsen 2005-08-06
  • 打赏
  • 举报
回复
再次感谢楼上的!
1.没有什么windows程序设计经验
2.c++学了一段时间了,对语法基本掌握了,有千行代码经验。
3.现在计算机专业大四刚毕业,马上要读研究生
这个就是我的全部基础了。
如果真的开始学习vc6.0,不知道怎么下手?从哪本书开始好?我这里有一套,红色封面的windows程序设计,还有一本windows核心编程。听说候捷的那本深入浅出mfc很好,不知道我该怎么一步一步的入手去学,怎么选择参考书?有经验的请告知:)谢谢!
xiaocai0001 2005-08-06
  • 打赏
  • 举报
回复
VC6.0做MFC开发的话入门较难,但若有VC6.0基础后,转到VC.NET中就会很快的。建议先学VC6.0吧,它可能和C++基础更贴近一些,直接用VC.NET的话可能一时难以接受。
xiaocai0001 2005-08-06
  • 打赏
  • 举报
回复
楼主,我改了一下程序,你看看,提点建议。
class Cip
{//solve an ipv4 address string
private:
void StrToInt();//内部数据转换用
bool CipCheckIp(char *strIP);

public:
Cip(char *str_target);
~Cip();
int SetIP(char *str);
int GetSeg(int seq_index);
bool SetSeg(int seq_index);
char GetIpType();

private:
int segment[4];
char *str;
};

Cip::Cip(char *str_target)
{
int i = 0;
str = new char[15];//an ip string buffer
for(i;i<4;i++)
segment[i] = 0;
if(strlen(str_target) > 6 && strlen(str_target) < 16)//error check,x.x.x.x,min=7,max=15
strcpy(str,str_target);
else
strcpy(str,'\0');
if(CipCheckIp(str))
StrToInt();
}

Cip::~Cip()
{
delete[] str;
}

int Cip::GetSeg(int seq_index)
{
if(seq_index <= 4 && seq_index >= 1)
return segment[seq_index - 1];
else
return -1;
}

bool Cip::CipCheckIp(char *str)//a rough check
{
int dot_count = 0;
int num_count = 0;
int num_val = 0;
while((*str))
{
if((*str) != '.')
{
if((*str) <= '9' && (*str) >= '0')
{
++ num_count;
num_val = num_val * 10 + (*str) - '0';
}
else
return false;
}
else{
++ dot_count;
if(num_count < 1 || num_count > 4 || num_val < 0 || num_val >255)
//check if the current segment is 1~3 numbers
return false;
num_count = 0;
num_val = 0;
}
++ str;
}
if(dot_count != 3)
return false;
return true;
}

void Cip::StrToInt()
{//将数据成员char *str转换到segment数组中
char *guard = str;
int cur_seg = 0;
int cur_val = 0;
while((*guard) != '\0')
{
if((*guard) != '.')
{
cur_val = cur_val * 10 + (*guard) - '0';
}
else
{
segment[cur_seg] = cur_val;
cur_val = 0; //reset to 0
cur_seg++;
}
guard++;
}
}
tarsen 2005-08-06
  • 打赏
  • 举报
回复
楼上的,我正想开始学vc呢,不知道自己的c++基础够不够,也不知道该从vc.net开始学,还是学vc6.0。主要是vc6.0的框架是mfc,学习vc6可以从mfc开始学起,那vs.net的框架是什么?真不知道怎么选择了:)
xiaocai0001 2005-08-06
  • 打赏
  • 举报
回复
regular expression是正则表达式
在VS.net中有
tarsen 2005-08-06
  • 打赏
  • 举报
回复
回复xiaocai0001(萧筱雨):其实也没有什么,在计算的时候,0*100是没有任何意义的。
回复楼上的,regular expression是什么东东??
K 2005-08-06
  • 打赏
  • 举报
回复
用regular expression简单啊
xiaocai0001 2005-08-06
  • 打赏
  • 举报
回复
202.193.064.34为什么不合法了?
实际运用中这没出什么问题啊,只是不好看了。
除非你的程序中有特殊要求了。
xiaocai0001 2005-08-06
  • 打赏
  • 举报
回复
做合法性检查是最麻烦的了,你能想到多少,就能写出多少。最终还是要看你要求的检测强度了。
tarsen 2005-08-06
  • 打赏
  • 举报
回复
这样检查吧:
对于ipv4,(x代表一个字符)一个极端是xxx.xxx.xxx.xxx的形式,另一个极端是x.x.x.x的形式。然而,每一个段(x或者xxx)都必须是一个整数,而且这个整数的范围在0~255之间。这个是最起码的合法形式。另外,还有可能出现的不合法的情况如下:
1.202.193.064.34这里的064就不合法,程序应当反馈此ip不合法
2.难道还有么?

tarsen 2005-08-06
  • 打赏
  • 举报
回复
其实我写的时候,就是大略的写了个ip字符串的合法性检查,没有怎么去想。
看来大家都有意见了:),那现在我们讨论一下如何进行合法性检查,然后用程序表达出来。
谢谢xiaocai0001(萧筱雨) ,大家一起讨论:)
tarsen 2005-08-06
  • 打赏
  • 举报
回复
回复antijpn(antijpn):其实我就是逐字节拿出来的,只不过要做个合法性检查,另外,我这个类还提供了一些操作供用户使用,比如用户需要知道第二段是什么数字啦之类的需求。
回复楼上的:我先改一下那个比较明显的错误如下:
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 - 1];
else
return -1;
}
bool Cip::CipCheckIp(char *str)//a rough check
{
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;
}
}
xiaocai0001 2005-08-06
  • 打赏
  • 举报
回复
没看明白楼上修改的,能否写清楚点,修改之处做一下标记!
fallinleave 2005-08-06
  • 打赏
  • 举报
回复
弄错了,是下面的函数

bool Cip::CipCheckIp(char *str)
{
  int dot_count = 0;
  int num_count = 0;
  while((*str))
  {
    if((*str) != '.')
    {
      if((*str) <= '9' && (*str) >= '0')
   num_count = num_count * 10 + (*str)-'0';
else
   return false;
}
else
{
  if(num_count == 0 && dot_count == 0)
return false;
  ++ dot_count;
  if(num_count < 0 || num_count > 255)
return false;
num_count = 0;//reset to 0
}
++ str;
  }
  if(dot_count != 3)
    return false;
  return true;
}
fallinleave 2005-08-06
  • 打赏
  • 举报
回复
xiaocai0001(萧筱雨),你虽然检查了数值范围,但是还不够精细,比如第一段不能为0,后面的几段中如果出现“076”之类的数值虽然合法,但是不好看,可以商榷。我建议修改成下面的函数

bool Cip::CipCheckIp(char *str)
{
  int dot_count = 0;
  int num_count = 0;
  while((*str))
  {
    if((*str) != '.')
    {
      if((*str) <= '9' && (*str) >= '0')
   num_count = num_count * 10 + (*str)-'0';//此处修改
{
if((num_count == 0)
}
      else
   return false;
}
else
{
  if(num_count == 0 && dot_count == 0)
return false;
  ++ dot_count;
  if(num_count < 0 || num_count > 255)
return false;
num_count = 0;//reset to 0
}
++ str;
  }
  if(dot_count != 3)
    return false;
  return true;
}
xiaocai0001 2005-08-06
  • 打赏
  • 举报
回复
还有,你在这个函数里bool Cip::CipCheckIp(char *str)做IP合法性检测,但只是检测了IP的位数,没有检测IP范围,IP每个字段只能是0~255,与其这样数位数num_count < 1 || num_count > 4的检测,还不如直接检查其值.改写成如下代码:
bool Cip::CipCheckIp(char *str)
{
  int dot_count = 0;
  int num_count = 0;
  while((*str))
  {
    if((*str) != '.')
    {
      if((*str) <= '9' && (*str) >= '0')
   num_count = num_count * 10 + (*str)-'0';       else
   return false;
}
else
{
  ++ dot_count;
  if(num_count < 0 || num_count > 255)
return false;
num_count = 0;//reset to 0
}
++ str;
  }
  if(dot_count != 3)
    return false;
  return true;
}

楼主认为呢?
大家共同学习学习
tarsen 2005-08-06
  • 打赏
  • 举报
回复
这已经是考虑了楼上各位的意见,尤其是xiaocai0001(萧筱雨),然后我又多加了一些功能。
同时,感谢楼上各位的建议!欢迎继续批评指正!
tarsen 2005-08-06
  • 打赏
  • 举报
回复
在实验室的一个服务器上,没有安装任何编译器,只好用ultraedit写,目前我还没有检查出来错误,大家(可以把它粘到你自己的编辑器上查看,帖子中的显示效果不太好)看看有没有错误?当然,如果有其他任何毛病,大家都可以仁者见仁智者见智的指出来,谢谢!
btw:比我当初第一个版本,多加了两个整数字符串和整数之间互相转化的函数。
class Cip{
private:
int segment[4];//save four segments of an ip in the form of integer
char *str;//save the ip string
private:
bool StrToInt(char *str);//get each segment from the ip string
bool IntToStr(int value,char *buffer);change an integer into the form of character
public:
int GetSeg(int sequence);//an interface by which user can get some segment(1~4)
bool GetIp(char *ip_buffer);//attention:the length of the buffer should be 15bytes at least
char GetIpType();//an interface by which user can get the type of the ip,attention:the function StrToInt should be executed in advance
bool SetSeg(int sequence,int new_value);//an interface by which user can set some segment(1~4)
Cip(char *str_target);
Cip();
~Cip();
};//class that solve an ipv4 address string
bool Cip::IntToStr(int value,char *buffer)
{
int quan = 10;
char *guard = buffer;
int number = value/quan;
while(value > 0)
{
while( number > 10)
{
quan *= 10;
number = value/quan;
}
(*guard) = number + '0';
++ guard;
value -= number*quan;
quan /= 10;
while(value < quan)//in case the next position is 0
{
(*guard) = '0';
++ guard;
quan /= 10;
if(value == 0 && quan == 1)
{
(*guard) = '\0';
return true;
}
}//jump out the while,then current position must not be 0
quan = 10;
number = value/quan;
if(number == 0)//if it is the last position
{
(*guard) = value + '0';
++ guard;
(*guard) = '\0';
return true;
}
}
if(value == 0)
{
(*guard) = number + '0';
++ guard;
(*guard) = '\0';
return true;
}
else{
(*guard) = '\0';
return false;
}
}
bool Cip::GetIp(char *ip_buffer)
{
char *guard = ip_buffer;
int i = 0;
for(;i < 4;++ i)
{
if(IntToStr(segment[i],guard))
{
while((*guard) != '\0')
++ guard;
++ guard;
(*guard) = '.';
++ guard;
}
else
return false;
}
return true;
}
char Cip::GetIpType()
{
if(segment[0] <= 127) //0 000 0000 ~ 0111 1111
return 'a';
else if(segment[0] <= 191)//10 00 0000~10 11 1111
return 'b';
else if(segment[0] <= 223)//110 0 0000~110 1 1111
return 'c';
else if(segment[0] <= 239)//1110 0000 ~1110 1111
return 'd';
else if(segment[0] <= 255)//1111 0000 ~1111 1111
return 'e';
}
bool Cip::SetSeg(int sequence,int new_value)
{
if(sequence <= 4 && sequence >= 1)
{
if(new_value <= 255 && new_value >= 0)
{
segment[sequence - 1] = new_value;
return true;
}
else
return false;
}
else
return false;
}
int Cip::GetSeg(int sequence)
{
if(sequence <= 4 && sequence >= 1)//in the user's opinion,there is noly four segments in an ip
return segment[sequence - 1];
else
return -1;
}
Cip::Cip()
{
int i = 0;
for(;i < 4;i++)
segment[i] = -1;//default,set each segment's value to -1
str = new char[15];//an ip string buffer
strcpy(str,"0.0.0.0");//default ip string is 0.0.0.0
}
Cip::Cip(char *str_target)
{
int i = 0;
for(;i < 4;i++)
segment[i] = -1;//default,set each segment's value to -1
str = new char[15];//an ip string buffer
if(strlen(str_target) > 6 && strlen(str_target) < 16)//a rough check,x.x.x.x,the length,min=6,max=15
strcpy(str,str_target);
else
strcpy(str,'\0');
}
Cip::~Cip()
{
delete[] str;
}
bool Cip::StrToInt(char *str)
{
char *guard = str;
int cur_seg = 0;
int cur_quan = 0;
int dot_count = 0;
if((*str) == '\0')//check whether the target ip string is empty
return false;
if((*str) <= '9' && (*str) >= '0')//if the first letter is between '0'~'9',then continue the next operation
{
while((*guard) != '\0')
++ guard;
-- guard;
while(guard >= str)
{
if((*guard) != '.')
{
cur_int += ((*guard) - '0')*cur_quan*10;
++ cur_quan;
}
else{
if(cur_int > 255 || cur_int < 0)//each segment's value should between 0~255
return false;
segment[cur_seg] = cur_int;
cur_int = 0;//reset to 0
++ cur_seg;//segment index increase itself
cur_quan = 0;//reset to 0
++ dot_count;
}
-- guard;
}
}
else
return false;//it shows the first letter is not between '0'~'9',then return false
}
加载更多回复(6)

64,637

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧