cumtoj 子网掩码

阅读: 评论:0

cumtoj 子网掩码

cumtoj 子网掩码

请解析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

int IP[8] = { 0 };//0-3IP地址,4-7Mask码

int a = 0, b = 0, c = 0, d = 0, e = 0,F=0,G=0;//按照A/B/C/D/E类地址归类,不合法的地址和掩码单独归类,私有地址归类

void funa(string x){

int len = x.length();

string tmp;

int num,ip=0,mask=0;

stringstream ss;

for (int i = 0; i < len; ++i)

{

if (x[i] == '.'||x[i]=='~'){

if (tmp == "")tmp = "999";

ss << tmp;

ss >> num;

//cout << num << " ";

IP[ip++] = num;

ss.clear(); tmp.clear();

}

else{

tmp.push_back(x[i]);

}

}

ss << tmp;

ss >> num;

// cout << num << " ";

IP[ip] = num;

ss.clear(); tmp.clear();

}

void funClass(){

if (IP[0] <= 126)++a;

if (IP[0] >= 128 && IP[0] <= 191)++b;

if (IP[0] >= 192 && IP[0] <= 223)++c;

if (IP[0] >= 240 && IP[0] <= 255)++d;

if (IP[0] == 10)++G;

if ((IP[1]>=16)&&(IP[1] <= 31)&&( IP[0]=172))++G;

if (IP[1] == 168 && IP[0] == 192)++G;

}

bool findIpNMask(int x[]){

bool f = true;

for (int i = 0; i < 4; ++i){

if (IP[i] > 255){ ++F;f=false; break; }

}

for (int i = 4; i < 8; ++i){

if (IP[i] != 255 && IP[i] != 0){ ++F; f = false; break; }

}

return f;

}

void HWoj(string str){

//string str = "192..0.~255.255.255.0";

funa(str);

for (int i = 0; i < 8; ++i){

funa(str);

}

if (findIpNMask(IP)){

funClass();

}

cout << a << " " << b << " " << c << " " << d << " " << F << " " << G;

cout << endl;

}

本文发布于:2024-01-31 10:39:17,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170666877327922.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:子网掩码   cumtoj
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23