华为机试:识别有效的IP地址和掩码进行分类统计

#include <bitset>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

vector<int> get_ip (string str) {          //获取ip地址
    string to_number;
    vector<int> IP;
    for (char ch : str) {
        if (ch == '~') break;
        else if (ch == '.') {
            if (!to_number.empty()) IP.push_back(stoi(to_number));        //stoi转int型函数
            to_number.clear();
        } else {
            to_number.push_back(ch);
        }
    }
    if (!to_number.empty()) IP.push_back(stoi(to_number));
    to_number.clear();
    return IP;
}

vector<int> get_maskcode (string str) {             //获取子网掩码
    string to_number;
    vector<int> MASKCODE;
    bool flag = false;
    for (char ch : str) {
        if (ch == '~') flag = true;
        else if (ch == '.' && flag) {
            MASKCODE.push_back(stoi(to_number));
            to_number.clear();
        } else if (flag) {
            to_number.push_back(ch);
        } else {
            continue;
        }
    }
    MASKCODE.push_back(stoi(to_number));
    to_number.clear();
    return MASKCODE;
}

bool check_ip (vector<int>
               IP) {              //将判断ip地址是否合法以及归属哪类的公共部分提取出来
    bool flag = false;
    int i = 1;
    for (i = 1; i < 4; i++) {
        if (IP[i] >= 0 && IP[i] <= 255) flag = true;
        else flag = false;
    }
    return flag;
}

bool check_maskcode (vector<int>
                     MASKCODE) {           //判断子网掩码是否合法
    int i = 0;
    string str;
    bool flag = false;
    for (i = 0; i < 4; i++) {
        bitset<8> bs(MASKCODE[i]);                //bitset转换二进制函数
        str += bs.to_string();                     //to_string转string字符串函数
    }
    if (str.find('1') == str.npos || str.find('0') == str.npos) {               //find方法找不到的返回值是npos标记
        return false;
    } else {
        for (i = 0; i < str.length(); i++) {
            if (str[i] == '0') {
                if (i != str.length() - 1 && str[i + 1] == '1') return false;
            }
        }
    }
    return true;
}

void check (vector<int> IP, vector<int> MASKCODE,
            vector<int>& COUNT) {       //判断类别及私有ip地址
    if (IP.size() == 4) {

        if (IP[0] != 0 && IP[0] != 127) {

            if (IP[0] >= 1 && IP[0] <= 126) {                  //A类及私有ip
                if (check_ip(IP) && check_maskcode(MASKCODE)) {
                    if (IP[0] == 10) COUNT[6] += 1;
                    COUNT[0] += 1;
                } else COUNT[5] += 1;

            } else if (IP[0] >= 128 && IP[0] <= 191) {           //B类及私有ip
                if (check_ip(IP) && check_maskcode(MASKCODE)) {
                    if (IP[0] == 172 && (IP[1] >= 16 && IP[1] <= 31)) COUNT[6] += 1;
                    COUNT[1] += 1;
                } else COUNT[5] += 1;

            } else if (IP[0] >= 192 && IP[0] <= 223) {           //C类及私有ip
                if (check_ip(IP) && check_maskcode(MASKCODE)) {
                    if (IP[0] == 192 && IP[1] == 168) COUNT[6] += 1;
                    COUNT[2] += 1;
                } else COUNT[5] += 1;

            } else if (IP[0] >= 224 && IP[0] <= 239) {               //D类
                if (check_ip(IP) && check_maskcode(MASKCODE)) COUNT[3] += 1;
                else COUNT[5] += 1;

            } else if (IP[0] >= 240 && IP[0] <= 255) {                     //E类
                if (check_ip(IP) && check_maskcode(MASKCODE)) COUNT[4] += 1;
                else COUNT[5] += 1;
            }
        }
    } else {
        COUNT[5] += 1;              //错误类
    }
}

int main() {
    // int a, b;
    // while (cin >> a >> b) { // 注意 while 处理多个 case
    //     cout << a + b << endl;
    // }
    string input;
    vector<int> count(7, 0);         //分别存储ABCDE、错误、私有
    vector<int> ip, maskcode;
    while (cin >>  input) {
        vector<int> ip, maskcode;
        ip = get_ip(input);
        maskcode = get_maskcode(input);
        check(ip, maskcode, count);
    }
    for (int i = 0; i < 6; i++) {
        cout << count[i] << " " ;
    }
    cout << count[6] << endl;
}

全部评论
又是充电学习的一天
点赞 回复 分享
发布于 2023-02-15 12:42 广东
又学会了一题
点赞 回复 分享
发布于 2023-02-15 12:57 湖北

相关推荐

01-02 21:17
已编辑
西安理工大学 后端
程序员小白条:项目不太重要,你的优势的算法竞赛,然后多背相关的八股文,项目可以不作为重点考虑,面试可能就简单带过项目就行了,你可以直接写简历,背项目相关的八股文就行,也不用自己做,时间紧张的情况下,性价比最高
点赞 评论 收藏
分享
评论
点赞
8
分享

创作者周榜

更多
牛客网
牛客企业服务