题解 | 识别有效的IP地址和掩码并进行分类统计

#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>

using namespace std;

// 将IP地址字符串转换为4个整数的数组
bool parseIP(const string& ip, int* ipParts) {
    stringstream ss(ip);
    string part;
    for (int i = 0; i < 4; ++i) {
        if (!getline(ss, part, '.')) return false;
        if (part.empty() ||
                part.find_first_not_of("0123456789") != string::npos) return false;
        ipParts[i] = stoi(part);
        if (ipParts[i] < 0 || ipParts[i] > 255) return false;
    }
    return true;
}

// 检查子网掩码是否合法
bool isValidSubnetMask(const int* mask) {
    bool hasZero = false;
    for (int i = 0; i < 4; ++i) {
        for (int j = 7; j >= 0; --j) {
            bool bit = (mask[i] >> j) & 1;
            if (bit == 0) hasZero = true;
            else if (hasZero) return false; // 1 after 0 is invalid
        }
    }
    // 全0或全1的掩码也是非法的
    if (!hasZero || (mask[0] == 255 && mask[1] == 255 && mask[2] == 255 &&
                     mask[3] == 255)) return false;
    return true;
}

// 判断IP地址的类别
char getIPClass(const int* ip) {
    if (ip[0] >= 1 && ip[0] <= 126) return 'A';
    if (ip[0] >= 128 && ip[0] <= 191) return 'B';
    if (ip[0] >= 192 && ip[0] <= 223) return 'C';
    if (ip[0] >= 224 && ip[0] <= 239) return 'D';
    if (ip[0] >= 240 && ip[0] <= 255) return 'E';
    return 'X'; // 不属于任何类别
}

// 判断是否为私有IP
bool isPrivateIP(const int* ip) {
    if (ip[0] == 10) return true;
    if (ip[0] == 172 && (ip[1] >= 16 && ip[1] <= 31)) return true;
    if (ip[0] == 192 && ip[1] == 168) return true;
    return false;
}

int main() {
    int countA = 0, countB = 0, countC = 0, countD = 0, countE = 0;
    int countError = 0, countPrivate = 0;

    string line;
    while (getline(cin, line)) {
        stringstream ss(line);
        string ipStr, maskStr;
        getline(ss, ipStr, '~');
        getline(ss, maskStr);

        int ip[4], mask[4];
        if (!parseIP(ipStr, ip) || !parseIP(maskStr, mask)) {
            countError++;
            continue;
        }

        // 特殊IP地址(0.*.*.* 和 127.*.*.*)不处理
        if (ip[0] == 0 || ip[0] == 127) continue;

        // 检查子网掩码是否合法
        if (!isValidSubnetMask(mask)) {
            countError++;
            continue;
        }

        // 判断IP类别
        char ipClass = getIPClass(ip);
        switch (ipClass) {
            case 'A':
                countA++;
                break;
            case 'B':
                countB++;
                break;
            case 'C':
                countC++;
                break;
            case 'D':
                countD++;
                break;
            case 'E':
                countE++;
                break;
            default:
                break;
        }

        // 判断是否为私有IP
        if (isPrivateIP(ip)) countPrivate++;
    }

    cout << countA << " " << countB << " " << countC << " " << countD << " " <<
         countE << " "
         << countError << " " << countPrivate << endl;

    return 0;
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务