题解 | #识别有效的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]);
    }
}
全部评论

相关推荐

一颗宏心:华为HR晚上过了十二点后还给我法消息。
点赞 评论 收藏
分享
ProMonkey2024:5个oc?厉害! 但是有一个小问题:谁问你了?😡我的意思是,谁在意?我告诉你,根本没人问你,在我们之中0人问了你,我把所有问你的人都请来 party 了,到场人数是0个人,誰问你了?WHO ASKED?谁问汝矣?誰があなたに聞きましたか?누가 물어봤어?我爬上了珠穆朗玛峰也没找到谁问你了,我刚刚潜入了世界上最大的射电望远镜也没开到那个问你的人的盒,在找到谁问你之前我连癌症的解药都发明了出来,我开了最大距离渲染也没找到谁问你了我活在这个被辐射蹂躏了多年的破碎世界的坟墓里目睹全球核战争把人类文明毁灭也没见到谁问你了(别的帖子偷来的,现学现卖😋)
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务