码迷,mamicode.com
首页 > 其他好文 > 详细

[华为机试练习题]34.识别有效的IP地址和掩码并进行分类统计

时间:2015-07-02 12:22:51      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:华为机试练习题

题目

描述:

请解析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

题目类别:

字符串  

难度:

    中级  

运行时间限制:

    10Sec 

内存限制:

    128MByte 

阶段:

入职前练习  

输入:

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

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

输出:

统计A、B、C、D、E、错误IP地址或错误掩码、私有IP的个数,之间以空格隔开,根据上面的IP,可以得到:

1.0.0.1~255.0.0.0 ----A类

192.168.0.2~255.255.255.0  ----C类,私有

10.70.44.68~255.254.255.0----错误的掩码

19..0.~255.255.255.0-----错误的IP

可以得到统计数据如下:

1 0 1 0 0 2 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

代码

/*---------------------------------------
*   日期:2015-07-02
*   作者:SJF0115
*   题目:识别有效的IP地址和掩码并进行分类统计
*   来源:华为机试练习题
-----------------------------------------*/
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <list>
using namespace std;

// 检查子网掩码和IP格式是否正确 并返回分段
bool isRight(string str,vector<string> &part){
    int size = str.size();
    int pointCount = 0;
    string::size_type index = 0;
    int prePoint = 0;
    while((index = str.find_first_of(‘.‘,index)) != string::npos){
        //..之间有数字
        if(index != prePoint){
            part.push_back(str.substr(prePoint,index-prePoint));
        }//if
        ++index;
        prePoint = index;
        ++pointCount;
    }//while
    if(prePoint < size){
        part.push_back(str.substr(prePoint));
    }//if

    int partSize = part.size();
    if(partSize != 4){
        return false;
    }//if

    // 判断每一部分均属于0-255
    int num;
    for(int i = 0;i < partSize;++i){
        num = atoi(part[i].c_str());
        if(num < 0 || num > 255){
            return false;
        }//if
    }//for
    // 代表错误IP
    if(pointCount != 3){
        return false;
    }//if
    return true;
}

// 检查IP
bool CheckIP(string ip,vector<int> &count){
    vector<string> part;
    // 格式不正确
    bool result = isRight(ip,part);
    if(!result){
        return false;
    }//if
    // 判断IP分类
    int num = atoi(part[0].c_str());
    if(num >= 1 && num <= 126){
        ++count[0];
    }//if
    else if(num >= 128 && num <= 191){
        ++count[1];
    }//else
    else if(num >= 192 && num <= 223){
        ++count[2];
    }//else
    else if(num >= 224 && num <= 239){
        ++count[3];
    }//else
    else if(num >= 240 && num <= 255){
        ++count[4];
    }//else
    else if(num == 127){
        return false;
    }
    // 私有IP
    int num1 = atoi(part[1].c_str());
    if(num==10||(num==172&&num1>=16&&num1<=31)||(num==192&&num1==168)){
        ++count[6];
    }//else
    return true;
}
// 判断是否是子网掩码
bool isNet(vector<string> part){
    int number[] = {0,128,192,224,240,248,252,254};
    int size = part.size();
    int num;
    bool flag = false;
    bool isOk = false;
    for(int i = 0;i < size;++i){
        num = atoi(part[i].c_str());
        if(flag && num != 0){
            return false;
        }//if
        else if(num != 255){
            flag = true;
            // 判断左边是不是全为1右边全为0
            for(int j = 0;j < 8;++j){
                if(num == number[j]){
                    isOk = true;
                    break;
                }//if
            }//for
            if(!isOk){
                return false;
            }//if
        }//if
    }//for
    return true;
}
// 检查子网掩码
bool CheckNet(string net){
    vector<string> part;
    bool result = isRight(net,part);
    if(!result){
        return false;
    }//if
    // 判断是否是子网掩码
    result = isNet(part);
    return result;
}

int main(){
    int n;
    string str;
    //freopen("C:\\Users\\Administrator\\Desktop\\c++.txt","r",stdin);
    int index;
    string ip,net;
    vector<int> count(7,0);
    while(getline(cin,str)){
        index = str.find("~",0);
        ip = str.substr(0,index);
        net = str.substr(index+1);

        bool resultNet = CheckNet(net);
        bool resultIP = false;
        if(resultNet){
            resultIP = CheckIP(ip,count);
        }//if

        if(!resultIP || !resultNet){
            count[5] += 1;
        }//if
    }//while
    for(int i = 0;i < 7;++i){
        if(i == 0){
            cout<<count[i];
        }//if
        else{
            cout<<" "<<count[i];
        }//else
    }//for
    cout<<endl;
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

[华为机试练习题]34.识别有效的IP地址和掩码并进行分类统计

标签:华为机试练习题

原文地址:http://blog.csdn.net/sunnyyoona/article/details/46722491

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!