华为oj中级 识别有效的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

知识点 字符串,循环,查找,搜索,排序,函数,指针,枚举,位运算,结构体,联合体,文件操作,递归
运行时间限制 0M
内存限制 0
输入
多行字符串。每行一个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

#include<iostream>    
#include<string>    
#include<math.h>    
using namespace std;    
int ip[4],mask[4];    
int ipA=0,ipB=0,ipC=0,ipD=0,ipE=0,ipErr=0,ipP=0;    
bool ipJudge(string ipstr,string maskstr)    
{    
    string tmp;    
    unsigned int mask_int=0;    
    for(int i=0;i<3;i++)    
    {    
        int pos=ipstr.find_first_of('.');    
        tmp=ipstr.substr(0,pos);    
        if(tmp.empty())    
            ip[i]=-1;       
        else    
            ip[i]=atoi(tmp.c_str());            
        ipstr=ipstr.substr(pos+1);    
    }    
    if(ipstr.empty())    
        ip[3]=-1;    
    else    
        ip[3]=atoi(ipstr.c_str());      
    for(int i=0;i<3;i++)    
    {    
        int pos=maskstr.find_first_of('.');    
        tmp=maskstr.substr(0,pos);    
        if(tmp.empty())    
            mask[i]=-1;    
        else    
            mask[i]=atoi(tmp.c_str());      

        maskstr=maskstr.substr(pos+1);    
    }    
    if(maskstr.empty())    
        mask[3]=-1;    
    else    
        mask[3]=atoi(maskstr.c_str());    
    for(int i=0;i<4;i++)    
    {    
        if(ip[i]<0||ip[i]>255||mask[i]<0||mask[i]>255)    
        {    
            ipErr++;    
            return false;    
        }    
        mask_int=mask_int*256+mask[i];    
    }    
    mask_int=~mask_int+1;    
    unsigned int test=0x01;     
    int flag=0;     
    for(int i=0;i<32;i++)    
    {       

        if(mask_int==test<<i)    
        {    
                flag=1;    
                break;      
        }    
    }    
    if(flag==0)    
    {    
        ipErr++;    
        return false;    
    }    
    if(ip[0]>=1&&ip[0]<=126)    
    {    
        ipA++;    
        if(ip[0]==10)    
            ipP++;    
    }    
    else if(ip[0]>=128&&ip[0]<=191)    
    {    
        ipB++;    
        if(ip[0]==172&&ip[1]>=16&&ip[1]<=31)    
            ipP++;    
    }    
    else if(ip[0]>=192&&ip[0]<=223)    
    {    
        ipC++;    
        if(ip[0]==192&&ip[1]==168)    
            ipP++;    
    }    
    else if(ip[0]>=224&&ip[0]<=239)    
        ipD++;    
    else     
        ipE++;    
    return true;    
}    
int main()    
{    
    string str,ipstr,maskstr;       
    while(getline(cin,str))    
    {    
        int pos=str.find('~');    
        ipstr=str.substr(0,pos);    
        maskstr=str.substr(pos+1);    
        ipJudge(ipstr,maskstr);    
    }    
    cout<<ipA<<" "<<ipB<<" "<<ipC<<" "<<ipD<<" "<<ipE<<" "<<ipErr<<" "<<ipP<<endl;    
    //system("pause");    
    return 0;    
}  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值