本文共 3527 字,大约阅读时间需要 11 分钟。
题目
描述:
请解析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.255172.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.01.0.0.1~255.0.0.0192.168.0.2~255.255.255.019..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.01.0.0.1~255.0.0.0192.168.0.2~255.255.255.019..0.~255.255.255.0
样例输出:
1 0 1 0 0 2 1
代码
/*---------------------------------------* 日期:2015-07-02* 作者:SJF0115* 题目:识别有效的IP地址和掩码并进行分类统计* 来源:华为机试练习题-----------------------------------------*/#include#include #include #include #include using namespace std;// 检查子网掩码和IP格式是否正确 并返回分段bool isRight(string str,vector
&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;}// 检查IPbool CheckIP(string ip,vector &count){ vector 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 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 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 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<
转载地址:http://ohsel.baihongyu.com/