应该是很常用的功能比如
输入 202.202.12.*
输出 202.202.12.1 202.202.12.2 202.202.12.3 ... 202.202.12.255输入 202.202.*.*
输出 202.202.1.1 202.202.1.2 202.202.1.3 ... 202.202.255.255输入 202.202.**
输出 语法错误输入 202.202.12.7-12
输出 202.202.12.7 202.202.12.8 202.202.12.9 202.202.12.10 202.202.12.11 202.202.12.12思路可能简单,但是实现很繁琐应该有人做过,希望有代码,谢谢!
输入 202.202.12.*
输出 202.202.12.1 202.202.12.2 202.202.12.3 ... 202.202.12.255输入 202.202.*.*
输出 202.202.1.1 202.202.1.2 202.202.1.3 ... 202.202.255.255输入 202.202.**
输出 语法错误输入 202.202.12.7-12
输出 202.202.12.7 202.202.12.8 202.202.12.9 202.202.12.10 202.202.12.11 202.202.12.12思路可能简单,但是实现很繁琐应该有人做过,希望有代码,谢谢!
伪代码:
// 判断是否正确的IP地址
将str中的*替换为0
if( IPAddress=inet_addr(str) == INADDR_NONE) return false // 循环输出
char *p = (char*)&IPAddress;
for(i=0;i<*的个数;i++){
for(j=0;j<256;j++){
p[3-i] = j;
输出inet_ntoa(IPAddress);
}
}
可以满足 1.1.1.* 到 *.*.*.* 的情况//
// 不合法的 strRangeIP
// 202.204.*. situation A
// 202.204.*.203 situation A // 不支持这种情况
// 202.204.* situation C
// 202.204..* situation B
{
// situation A
if ( '*' != strRangeIP.GetAt(strRangeIP.GetLength()-1) )
return FALSE;
// situation B
CString replaceMe = strRangeIP;
replaceMe.Replace('*', '1');
if (INADDR_NONE == inet_addr(replaceMe)) return FALSE;
// situation C
if ( 3 == replaceMe.Replace('.','a') ) return FALSE; return TRUE;
}// 判断
{
i = strRangeIP.Find('*');
if (-1 == i) return FALSE; // 没有 * 的情况
if (0 == i) return TRUE; // *.*.*.* 的情况 str1 = strIP.Left(i);
str2 = strRangeIP.Left(i); if (str1 == str2) return TRUE;
else return FALSE;
}
判断合法性:
/////////////////////////////////////////////////////
//
// 判断是否是合法的 - 符号
//
// legal: (x.x.x.min-max)
// 202.204.5.202-205
// illegal:// 202.204.-.5 situation A
// 202.204.202-205.5// 202.204.5.202- situation B
// 202.204.5.-202 situation D
// 202.204.5.205-202 situation C
// 202.204.5.205-2023 situation E
//
BOOL IPHasHyphen(CString strIP)
{
CString replaceMe = strIP,
strEnd = _T(""); // situation B
if ('-' == strIP.GetAt(strIP.GetLength()-1) ) return FALSE;
if (1 != replaceMe.Replace('-', '1')) return FALSE; int i = 0,
j = 0;
char ch = 0; // situation A
for (i=0; i<strIP.GetLength()&&j<3; i++)
{
ch = strIP.GetAt(i); // for 202.204.5.
if (!( (ch >= '0' && ch <= '9')||('.' == ch) )) return FALSE; if ('.' == ch) j++;
} // situation C
CString str1, str2;
int nHyphen, n1, n2; strEnd = strIP.Right(strIP.GetLength() - i); nHyphen = strEnd.Find('-');
if (0 == nHyphen) return FALSE; // situation D str1 = strEnd.Left(nHyphen);
str2 = strEnd.Right(strEnd.GetLength() - nHyphen - 1);
n1 = StrToInt(str1);
n2 = StrToInt(str2); // situation E
if ( (n1 < 0) || (n1 > 255) || (n2 < 0) || (n2 > 255) ) return FALSE; if (n2 < n1) return FALSE;
else return TRUE;
}判断是否在地址范围
{
// 判断前面部分 202.204.5
i = strRangeIP.ReverseFind('.'); if (-1 == i) return FALSE; str1 = strIP.Left(i);
str2 = strRangeIP.Left(i); if (str1 != str2) return FALSE; // 判断后面部分 199-204 // 1. 得到 range 的数值
CString strEnd;
int nHyphen, n1, n2, n3; strEnd = strRangeIP.Right(strRangeIP.GetLength() - i - 1); nHyphen = strEnd.Find('-');
if (0 == nHyphen) return FALSE; // 202.204.5.-204 (应该已经过滤掉了) str1 = strEnd.Left(nHyphen);
str2 = strEnd.Right(strEnd.GetLength() - nHyphen - 1);
n1 = StrToInt(str1); // 199
n2 = StrToInt(str2); // 204 // situation E
if ( (n1 < 0) || (n1 > 255) || (n2 < 0) || (n2 > 255) ) return FALSE;
if (n2 < n1) return FALSE; // 这两行判断的也应该已经过滤掉了 // 2. 得到 strIP 的数值
strEnd = strIP.Right(strIP.GetLength() - i - 1);
n3 = StrToInt(strEnd); if ( (n3 < n1) || (n3 > n2) ) return FALSE;
else return TRUE;
}
else return FALSE;
#include <iostream.h>
#include <string.h>
#include <Winsock2.h>void main(){
char str[30];
unsigned int starNum=0;
int dotNum=0;
cin>>str;
for(int i=0;i<strlen(str);i++){
if(str[i]=='*'){
starNum++;
str[i]='0';
}
if(str[i]=='.')
dotNum++;
}
unsigned long IPAddress;
if( (IPAddress=inet_addr(str)) == 0 || dotNum!=3){
cout<<"error IP Address"<<endl;
return;
}
unsigned char *p = (unsigned char *)&IPAddress;
int i1,i2,i3,i4,j1,j2,j3,j4;
for(i1=0;i1<starNum;i1++)
for(j1=0;j1<256;j1++)
{
p[3] = j1;
struct in_addr IP;
IP.S_un.S_addr = IPAddress;
cout<<inet_ntoa(IP)<<endl;
for(i2=0;i2<starNum-1;i2++)
for(j2=0;j2<256;j2++)
{
p[2] = j2;
struct in_addr IP;
IP.S_un.S_addr = IPAddress;
cout<<inet_ntoa(IP)<<endl;
for(i3=0;i3<starNum-2;i3++)
for(j3=0;j3<256;j3++)
{
p[1] = j3;
struct in_addr IP;
IP.S_un.S_addr = IPAddress;
cout<<inet_ntoa(IP)<<endl;
for(i4=0;i4<starNum-3;i4++)
for(j4=0;j4<256;j4++)
{
p[0] = j4;
struct in_addr IP;
IP.S_un.S_addr = IPAddress;
cout<<inet_ntoa(IP)<<endl;
}
}
}
}
}
给楼主一个我最近写的CIpRange类,所有的功能封装在类中,附有详尽注释及用法说明,并且已经修改的完全符合楼主的要求了。另外还用这个类写了一个基于控制台的示例程序,要的话就来下载啊!ftp://vc:[email protected]/IP解析程序/testParseIp.rar
Mark