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

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

#include<iostream>
#include<string>
#include<ctype.h>

using namespace std;
static int result[7]={0};   //保存最后结果的数组
string str2bit(string str)
{
    int num=atoi(str.c_str());
    char bits[10]={0};
    itoa(num,bits,2);
    string tmp(bits);
    if(tmp.size()<8)
    {
        str=string(8-tmp.size(),'0')+tmp;
    }
    else
        str=tmp;
    return str;
}
void checkNetSegment(string mask,string ip)
{
    int count1=0,count2=0;
    string str1[4],str2[4];
    string test;
    //int flag=0;
    for(string::size_type i=0;i<mask.size();i++)//掩码的点个数
    {
        if(mask[i]=='.' && mask[i+1]!='.')
            count1++;
    }
    for(string::size_type i=0;i<ip.size();i++)  //  ip的点个数
    {
        if(ip[i]=='.' && ip[i+1]!='.')
            count2++;
    }
    if(count1==3 && count2==3)  //  如果格式正确
    {
        string::size_type i=0;
        int flag1=0,flag2=0,flagmask=0;
        int k=0;
        while(i<mask.size())
        {
            string::size_type tmp=i;
            while(isdigit(mask[tmp]))
                tmp++;
            str1[k]=mask.substr(i,tmp-i);
            k++;
            i=tmp+1;
        }
        i=0;
        k=0;
        while(i<ip.size())
        {
            string::size_type tmp=i;
            while(isdigit(ip[tmp]))
                tmp++;
            str2[k]=ip.substr(i,tmp-i);
            k++;
            i=tmp+1;
        }
        for(int i=0;i<4;i++)        //判断每一段的值是否在0~255之间
        {
            if(atoi(str1[i].c_str())>=0 && atoi(str1[i].c_str())<256)
                flag1++;
            if(atoi(str2[i].c_str())>=0 && atoi(str2[i].c_str())<256)
                flag2++;
        }
        for(int i=0;i<4;i++)    //判断掩码是不是在1在0前面
        {
            string temp=str2bit(str1[i]);
            test+=temp;
        }
        for(int j=1;j<32-1;j++)
            if(test[j-1]>test[j] &&test[j]<test[j+1])
            {
                flagmask=1;
                break;
            }
        if(flag1==3 && flag2==3 && flagmask==0)     //格式正确
        {
            if(str2[0]>="1" && str2[0]<="126")
                result[0]++;
            else if(str2[0]>="128" && str2[0]<="191")
                result[1]++;
            else if(str2[0]>="192" && str2[0]<="223")
                result[2]++;
            else if(str2[0]>="224" && str2[0]<="239")
                result[3]++;
            else if(str2[0]>="240" && str2[0]<="255")
                result[4]++;
            else if(str2[0]=="127")     //格式不正确
                result[5]++;
            if(str2[0]=="10" ||(str2[0]=="172" && str2[1]>="16" &&str2[1]<="31")||
                (str2[0]=="192" && str2[1]=="168"))     //私有IP
                result[6]++;
        }
        else result[5]++;
    }
    else
        result[5]++;
}
int main()
{
    string s1,mask,ip;

    while(getline(cin,s1))
    {
        string::size_type pos=s1.find('~',0);
        ip=s1.substr(0,pos);
        mask=s1.substr(pos+1);
        checkNetSegment(mask,ip);
    }
    for(int i=0;i<7;i++)
    {
        cout<<result[i];
        if(i==6)
            cout<<endl;
        else
            cout<<" ";
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值