识别有效的IP地址和掩码并进行分类

题目描述

请解析IP地址和对应的掩码,进行分类识别。要求按照A/B/C/D/E类地址归类,不合法的地址和掩码单独归类。

所有的IP地址划分为 A,B,C,D,E五类

A类地址1.0.0.0~126.255.255.255;

B类地址128.0.0.0~191.255.255.255;

C类地址192.0.0.0~223.255.255.255;

D类地址224.0.0.0~239.255.255.255;

E类地址240.0.0.0~255.255.255.255

私网IP范围是:

10.0.0.0~10.255.255.255

172.16.0.0~172.31.255.255

192.168.0.0~192.168.255.255

子网掩码为二进制下前面是连续的1,然后全是0。(例如:255.255.255.32就是一个非法的掩码)

输入描述:

多行字符串。每行一个IP地址和掩码,用~隔开。

输出描述:

统计A、B、C、D、E、错误IP地址或错误掩码、私有IP的个数,之间以空格隔开。

示例1

输入

10.70.44.68~255.254.255.0
1.0.0.1~255.0.0.0
192.168.0.2~255.255.255.0
19..0.~255.255.255.0

输出

1 0 1 0 0 2 1

 

解题代码:

#include<iostream>
#include<string>
#include<vector>
 
using namespace std;
 
void splitString(const string& str, const string separator, vector<string>& res)
{
    int pos1, pos2;
    pos1 = 0;
    pos2 = str.find(separator, pos1);  //找到返回位置,找不到返回-1
    string temp;
    while (pos1 != pos2)
    {
        temp = str.substr(pos1, pos2 - pos1);  //第二次循环这里,pos1等于“~”后面一个位置,而pos1=-1,pos2-pos1<0
        res.push_back(temp);                   //所以默认截取pos1,到字符串末尾
        pos1 = pos2 + 1;
        if (pos1 == 0)break;
        pos2 = str.find(separator, pos1);
        
    }
}
 
bool checkIpNums(const vector<int>& ips)
{
    if (ips.size() == 4)
    {
        if (ips[0] >=0 && ips[0] <= 255 && ips[1] >=0 && ips[1] <= 255
            && ips[2] >=0 && ips[2] <= 255 && ips[3] >= 0 && ips[3] <= 255)
            return true;
        else
            return false;
    }
    else
        return false;
}
 
bool validMask(const vector<int>& mask)
{
    if (checkIpNums(mask))
    {
        int pos=3;
        for (int i = 3; i >= 0; i--)
        {
            if (mask[i] != 0)
            {
                pos = i;
                break;
            }
        }
         
        if (pos == 3&&mask[pos]==255)return false;
         
        if (mask[pos] == 255 || mask[pos] == 254 || mask[pos] == 252 || mask[pos] == 248 ||
            mask[pos] == 240 || mask[pos] == 224 || mask[pos] == 192 || mask[pos] == 128)
        {
            int tag = 0;
            while (pos >0)
            {
                pos--;
                if (mask[pos] != 255)
                {
                    tag = 1;
                    break;
                }
            }
            if (tag == 0)return true;
            else return false;
        }
        else return false;
    }
    else
        return false;
}
 
int main()
{
    string str;
    int A, B, C, D, E, errIpMask, privateIP;
    A = B = C = D = E = errIpMask = privateIP = 0;
    while(getline(cin, str))
    {
        vector<string> tempSepStr, ipStr, maskStr;
        vector<int> ip, mask;
        splitString(str, "~", tempSepStr);
        splitString(tempSepStr[0], ".", ipStr);
        splitString(tempSepStr[1], ".", maskStr);
        for (int i = 0; i < ipStr.size(); i++)
        {
            ip.push_back(stoi(ipStr[i]));
        }
        for (int i = 0; i < maskStr.size(); i++)
        {
            mask.push_back(stoi(maskStr[i]));
        }
 
        if (validMask(mask)&&checkIpNums(ip))
        {
                if (ip[0] >= 1 && ip[0] <= 126)
                {
                    if (ip[0] == 10)privateIP++;
                    A++;
                }
 
                if (ip[0] == 172 && ip[1] >= 16 && ip[1] <= 31)privateIP++;
                if (ip[0] == 192 && ip[1] == 168)privateIP++;
 
                if (ip[0] >= 128 && ip[0] <= 191) B++;
 
                if (ip[0] >= 192 && ip[0] <= 223) C++;
 
                if (ip[0] >= 224 && ip[0] <= 239) D++;
 
                if (ip[0] >= 240 && ip[0] <= 255) E++;
        }
        else
            errIpMask++;
    }
     
    cout << A << " " << B << " " << C << " " << D << " " << E
        << " " << errIpMask << " " << privateIP << endl;
    system("pause");
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值