Topic
Describe:
请解析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
Topic Category:
字符串
Difficulty:
中级
Run time limit:
Memory Limit:
Stage:
入职前练习
Input:
多行字符串。每行一个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
Output:
统计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
Sample input:
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
Sample output:
1 0 1 0 0 2 1
Code
/* ---------------------------------------* Date: 2015-07-02* sjf0115* title: Identify and classify valid IP addresses and masks * Source: Machine Test Exercises--------- --------------------------------*/#include <iostream>#include <vector>#include <string>#include <algorithm>#include <list>using namespace STD;//Check that the subnet mask and IP format are correct and return fragmentationBOOLIsright (stringStr vector<string>&part) {intSize = Str.size ();intPointcount =0;string:: Size_type index =0;intPrepoint =0; while(Index = str.find_first_of ('. ', index))! =string:: NPOs) {//.. There are numbers between 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 intPartsize = Part.size ();if(Partsize! =4){return false; }//if //Judge that each part belongs to 0-255 intNum for(inti =0; i < partsize;++i) {num = Atoi (PART[I].C_STR ());if(Num <0|| num >255){return false; }//if}//for //On behalf of the wrong IP if(Pointcount! =3){return false; }//if return true;}//Check IPBOOLCheckip (stringIp vector<int>&count) { vector<string>Part//format is not correct BOOLresult = Isright (Ip,part);if(!result) {return false; }//if //Judging IP classification intnum = atoi (part[0].c_str ());if(Num >=1&& Num <=126) {++count[0]; }//if Else if(Num >= -&& 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 >= -&& Num <=255) {++count[4]; }//else Else if(num = =127){return false; }//Private IP intNUM1 = Atoi (part[1].c_str ());if(num==Ten|| (num==172&&num1>= -&&num1<= to)|| (num==192&&num1==168)) {++count[6]; }//else return true;}//Determine if it is a subnet maskBOOLIsnet ( vector<string>part) {intNumber[] = {0, -,192,224, -,248,252,254};intSize = Part.size ();intNumBOOLFlag =false;BOOLIsOk =false; for(inti =0; i < size;++i) {num = Atoi (PART[I].C_STR ());if(Flag && num! =0){return false; }//if Else if(num! =255) {flag =true;//Judging left is not all for 1 right all 0 for(intj =0; J <8; ++j) {if(num = = Number[j]) {isOk =true; Break; }//if}//for if(!isok) {return false; }//if}//if}//for return true;}//Check subnet maskBOOLChecknet (stringNET) { vector<string>PartBOOLresult = Isright (Net,part);if(!result) {return false; }//if //Determine if it is a subnet maskresult = Isnet (part);returnResult;}intMain () {intNstringStr//freopen ("C:\\users\\administrator\\desktop\\c++.txt", "R", stdin); intIndexstringIp,net; vector<int>Count7,0); while(Getline (Cin, str)) {index = Str.find ("~",0); IP = str.substr (0, index); NET = Str.substr (index+1);BOOLResultnet = checknet (NET);BOOLResultip =false;if(resultnet) {Resultip = Checkip (Ip,count); }//if if(!resultip | |!resultnet) {count[5] +=1; }//if}//while for(inti =0; I <7; ++i) {if(i = =0){cout<<count[i]; }//if Else{cout<<" "<<count[i]; }//else}//for cout<<endl;return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
[Machine Test exercises]34. Identify valid IP addresses and masks and classify statistics