题解 | #识别有效的IP地址和掩码 ---不想写正则的可以来拿#
识别有效的IP地址和掩码并进行分类统计
http://www.nowcoder.com/practice/de538edd6f7e4bc3a5689723a7435682
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <regex>
#include <algorithm>
#include <vector>
#include <stack>
#include <bitset>
using namespace std;
/*检查子网掩码*/
int checkNM(regex nex,string ips,vector<int>& total,string& NetMask,string& IP)
{
smatch sm,nm; int count = 0; regex reg("~(.*)");
regex_search(ips,sm,reg); int num = 0; string bits = "";
string netmask = sm.str(1); netmask += "@";
/*将子网掩码转化为二进制字符串*/
for (char ch : netmask)
{
if(isdigit(ch))
{
num = num *10 + ch -'0';
}else{
bitset<8> bitset1(num);
string bbits = bitset1.to_string();
bits.append(bbits);
num = 0;
}
}
regex_match(bits,nm,nex);
if (nm.size())
{
NetMask = nm.str();
count += 1;
}else{
++total[5];
}
return count;
}
/*检查IP*/
int checkIP(regex rex,string ips,vector<int>& total,string& NetMask,string& IP)
{
smatch sm; int count = 0;
regex_search(ips, sm, rex);
if (sm.size())
{
IP = sm.str();
count += 1;
}
return count;
}
/*注意注意注意注意!!*/
/*建议直接拿走正则用吧,我写的太复杂了*/
int main(void)
{
/*初始化变量*/
string ips;//= "1.0.0.1~255.0.0.0";
smatch sm,nm;
static string NetMask = "";
static string IP = "";
vector<int> total(7,0);
//string APattern = "((1[0-2][0-6])|([1-9][0-9])|([1-9]))\\.(([1-2][0-9][0-9]|[1-9][0-9]|[0-9])\\.){2}([1-2][0-9][0-9]|[1-9][0-9]|[0-9])~255\\.0\\.0\\.0";
/*正则*/
string APattern = "(^1[0-1][0-9]|^[1-9][0-9]|^[1-9]|^12[0-6])\\.((1[0-9][0-9]|2[0-4][0-9]|25[0-4]|[1-9][0-9]|[0-9])\\.){2}(1[0-9][0-9]|2[0-4][0-9]|25[0-4]|[1-9][0-9]|[0-9])";
string BPattern = "(^12[8-9]|^1[3-8][0-9]|^19[0-1])\\.((1[0-9][0-9]|2[0-4][0-9]|25[0-4]|[1-9][0-9]|[0-9])\\.){2}(1[0-9][0-9]|2[0-4][0-9]|25[0-4]|[1-9][0-9]|[0-9])";
string CPattern = "(^19[2-9]|^2[0-1][0-9]|^22[0-3])\\.((1[0-9][0-9]|2[0-4][0-9]|25[0-4]|[1-9][0-9]|[0-9])\\.){2}(1[0-9][0-9]|2[0-4][0-9]|25[0-4]|[1-9][0-9]|[0-9])";
string DPattern = "(^22[4-9]|^23[0-9])\\.((1[0-9][0-9]|2[0-4][0-9]|25[0-4]|[1-9][0-9]|[0-9])\\.){2}(1[0-9][0-9]|2[0-4][0-9]|25[0-4]|[1-9][0-9]|[0-9])";
string EPattern = "^24[0-9]|^25[0-5]\\.((1[0-9][0-9]|2[0-4][0-9]|25[0-4]|[1-9][0-9]|[0-9])\\.){2}(1[0-9][0-9]|2[0-4][0-9]|25[0-4]|[1-9][0-9]|[0-9])";
string NPattern = "1+0+$";
string FPattern = "(^0|^127).*";
regex Aregex(APattern);regex Bregex(BPattern);regex Cregex(CPattern);
regex Dregex(DPattern);regex Nregex(NPattern);regex Eregex(EPattern);
regex Fregex(FPattern);
while ( cin >> ips )
{
regex_match(ips,sm,Fregex);
if (sm.size())
{
continue;
}
if (!checkNM(Nregex,ips,total,NetMask,IP))
{
continue;
}
if (checkIP(Aregex, ips,total,NetMask,IP))
{
++total[0];
IP.substr(0,2) == "10" && IP.substr(2,1) == "." ? ++total[6] : total[6];
continue;
}
if (checkIP(Bregex, ips,total,NetMask,IP))
{
++total[1];
if (IP.substr(0,3) == "172")
{
int num = 0;
for (int i = 4 ;i <=6 ;++i )
{
num = num * 10 + IP[i]-'0';
}
if (num >= 16 && num <= 31)
{
++total[6];
}
}
continue;
}
if (checkIP(Cregex, ips,total,NetMask,IP))
{
++total[2];
IP.substr(0,3) == "192" && IP.substr(4,3) == "168" ? ++total[6] : total[6];
continue;
}
if (checkIP(Dregex, ips,total,NetMask,IP) && NetMask != "")
{
++total[3];
continue;
}
if (checkIP(Eregex, ips , total,NetMask,IP) && NetMask != "")
{
++total[4];
continue;
}
++total[5];
}
for (int i = 0;i <7;++i)
{
printf("%d ",total[i]);
}
}