• Welcome to the world's largest Chinese hacker forum

    Welcome to the world's largest Chinese hacker forum, our forum registration is open! You can now register for technical communication with us, this is a free and open to the world of the BBS, we founded the purpose for the study of network security, please don't release business of black/grey, or on the BBS posts, to seek help hacker if violations, we will permanently frozen your IP and account, thank you for your cooperation. Hacker attack and defense cracking or network Security

    business please click here: Creation Security  From CNHACKTEAM

牛客网-华为机考-HJ18-识别有效的互联网协议(互联网协议)地址和掩码并进行分类统计


Recommended Posts

//识别有效的互联网协议(互联网协议)地址和掩码并进行分类统计

//https://www。现在编码器。E4公元前3a 5689723 a 7435682

#包括输入输出流

#包含字符串

#包含算法

#包含位集

#包含矢量

#包含流

使用命名空间标准

枚举IPAddressType {

错误IP=-1,

A_IP,

B_IP,

C_IP,

D_IP,

E_IP,

其他_IP

};

结构IPAndMaskResult {

IP地址类型IP类型{ ERROR _ IP };

弯曲件是私有IP { };

bool是有效的掩码{ };

};

结构IPAndMaskValidNum {

int A _ IP _ Num { };

int B _ IP _ Num { };

int C _ IP _ Num { };

int D _ IP _ Num { };

int E _ IP _ Num { };

int ERR _ IP _ Num { };

int ERR _ Mask _ Num { };

int Private _ IP _ Num { };

};

//子网掩码要求为3点分制格式

bool IsValidMask(常量字符串掩码)

{

字符串流ss(掩码);

字符串tmp { };

string bitStr { };

bool isValidMask=true

while (getline(ss,tmp,' . ')))) {

bit set 8 b((int)stoi(tmp));

bitStr=b . to _ string();

}

自动ITER=bitstr。查找(' 1 ');

如果(iter!=bitStr.npos) {

bitStr=bitStr。substr(ITER);

}

if(bitstr。find(' 10 ')==bitstr。NPOs | |

bitStr.find('01 ')!=bitStr.npos ) {

isValidMask=false

}

//cout ' in mask : ' mask ' bitStr : ' bitStr '是有效的: '是有效的掩码endl

返回isValidMask

}

IPAndMaskResult CheckIPAddrType(常量字符串IP)

{

IPAndMaskResult ipAndMaskRes { };

const int ipLen=4;

字符串流ss(IP);

字符串tmp { };

int ipNum[ipLen]{ };

int index=0;

while (getline(ss,tmp,' . ')))) {

如果(tmp。size()==0 | | stoi(tmp)255){

ipAndMaskRes.ipType=ERROR _ IP

返回ipAndMaskRes//有效互联网协议(Internet Protocol的缩写)直接退出

}

IP num[index]=stoi(tmp);

}

if(IP num[0]==0 | | IP num[0]==127){

ipAndMaskRes.ipType=OTHERS _ IP

返回ipAndMaskRes

}

if ((ipNum[0]==10) ||

(IP编号[0]==172 IP编号[1]=16 IP编号[1]=31)| |

(ipNum[0]==192 ipNum[1]==168)) {

ipAndMaskRes.isPrivateIP=true

}

if (ipNum[0]=1 ipNum[0]=126) {

ipAndMaskRes.ipType=A _ IP

} else if(IP num[0]=128 IP num[0]=191){

ipAndMaskRes.ipType=B _ IP

} else if(IP num[0]=192 IP num[0]=223){

ipAndMaskRes.ipType=C _ IP

} else if(IP num[0]=224 IP num[0]=239){

ipAndMaskRes.ipType=D _ IP

} else if(IP num[0]=240 IP num[0]=255){

ipAndMaskRes.ipType=E _ IP

}否则{

ipAndMaskRes.ipType=ERROR _ IP

}

返回ipAndMaskRes

}

IPAndMaskResult解决方案(常量字符串IPAndMask)

{

IPAndMaskResult ipAndMaskRes { };

字符串IP { };

字符串掩码{ };

string tmp=IPAndMask

自动ITER=ipandmask。查找(' ~ ');

如果(iter!=IPAndMask.npos) {

ip=IPAndMask.substr(0,ITER);

if (iter 1 IPAndMask.size()) {

mask=ipandmask。substr(ITER一号);

}

}

ipAndMaskRes=CheckIPAddrType(IP);

ipandmaskres。is valid mask=is valid mask(掩码);

返回ipAndMaskRes

}

int main()

{

IPAndMaskValidNum IPAndMaskValidNum { };

IPAndMaskResult ipAndMaskRes { };

字符串IPAndMask { };

while (getline(cin,IPAndMask)) {

ipAndMaskRes=解决方案(IPAndMask);

if(ipandmaskres。iptype==OTHERS _ IP){

继续;

}

如果(!ipAndMaskRes.isValidMask) {

ipAndMaskValidNum .错误_掩码_数量;

继续;

}

开关(ipAndMaskRes.ipType) {

案例错误_IP: ipAndMaskValidNum .错误_ IP _数字;打破;

案例A_IP: ipAndMaskValidNum .一个IP号码打破;

案例B_IP: ipAndMaskValidNum .B _ IP _数字打破;

案例C_IP: ipAndMaskValidNum .C _ IP _数字打破;

案例D_IP: ipAndMaskValidNum .D _ IP数字;打破;

案例e _ IP 3330 ipandmaskvalidnum。电子邮件地址号码;打断;打断;

默认值3330

打断;打断;

}

if(ipandmaskres。isprivatip)}

ipandmaskvalidnum。私有IP号码(IP号码)(IP地址:IP地址:IP地址:IP地址:IP地址:IP地址:IP地址:IP地址:IP地址:IP地址:IP地址:IP地址:IP地址:IP地址:IP地址:

}

}

ipAndMaskValidNum。A_IP_Num ' '层

ipAndMaskValidNum。B_IP_Num ' '中

ipAndMaskValidNum。C_IP_Num ' '中

ipAndMaskValidNum。D_IP_Num“”

ipAndMaskValidNum。电子邮件地址号码' '

ipandmaskvalidnum。err _ IP _ num ipandmaskvalidnum。错误_掩码_编号' '

ipandmaskvalidnum。私有IP号码结尾:

返回0;

}

Link to comment
Share on other sites