华为OJ中级题-识别有效的IP地址和掩码并进行分类统计
2015-12-11 10:40
489 查看
请解析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;
}
所有的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;
}
相关文章推荐
- spark sql 无法访问 hive metastore问题解决
- win2003 共享文件夹无密码访问
- LeetCode 234 Palindrome Linked List
- Discuz!X/数据库操作方法
- linux-ssh免密码登录设置
- html中定位详解
- 动态规划 - 最大子序列的和
- Java之lambda表达式
- 关于程序员的一点感想
- mangodb索引问题
- ImageLoader 使用记录
- Open edXDevstack启动出现:could not connect to localhost:27017: [Errno 111] Connection refused.
- PE总结11--PE文件结构之 解析导入表
- Bean Validation 技术规范特性概述
- CSS扩展选择器
- redis的内部实现机制
- DataSet对象
- 转载:SQL Server编程基本语法
- abinbinyang---t android.widget.AbsListView.obtainView(AbsListView.java:2040)
- static 与 non-static 的区别