第6周-项目3-IP地址类
2016-04-05 19:09
190 查看
问题及代码:
运行结果:
*All rights reserved. *文件名称:main.cpp *作 者:王艺霖 *完成日期:2016年4月5日 *版 本 号:v1.0 * *问题描述:判断IP地址的类型是否属于同一子网 输入描述:无输入 *输出描述:IP地址的类型 */ #include <iostream> using namespace std; class IP { private: union { struct { unsigned char seg0; unsigned char seg1; unsigned char seg2; unsigned char seg3; }; unsigned int address; }; public: IP(int=0,int=0,int=0,int=0); void showIP(); bool sameSubnet(const IP&ip,const IP&mark); char whatKind(); }; IP::IP(int a0,int a1,int a2,int a3) { seg0=a0; seg1=a1; seg2=a2; seg3=a3; } void IP::showIP() { cout<<int(seg0)<<'.'<<int(seg1)<<'.'<<int(seg2)<<'.'<<int(seg3)<<endl; } bool IP::sameSubnet(const IP&ip,const IP&mark) { unsigned int i1,i2; i1=address&mark.address; i2=ip.address&mark.address; return (i1==i2); } char IP::whatKind() { if(seg0<128) return 'A'; else if(seg0<192) return 'B'; else if(seg0<224) return 'C'; else if(seg0<240) return 'D'; else return 'E'; } int main() { IP ip1(202,194,116,97), ip2(202,194,119,102), mark(255,255,248,0); cout<<"ip1: "; ip1.showIP(); cout<<"ip2: "; ip2.showIP(); if(ip1.sameSubnet(ip2,mark)) cout<<"两个IP在同一子网"<<endl; else cout<<"两个IP不在同一子网"<<endl; cout<<"ip1属于"<<ip1.whatKind()<<"类网络"<<endl; return 0; }
运行结果:
相关文章推荐
- 第五周项目三 时间类(1)
- Nmap的那些事儿
- 安卓自制的计算器
- CUDA编程(五)关注内存的存取模式
- Leetcode 95. Unique Binary Search Trees II
- 第五周上机实践项目 项目2--游戏中的角色类(2)
- Redis与Memcached的区别
- 第6周项目1—分数类的雏形
- 打印log信息
- ssh 登录会显示最后一个用户,scope prototype
- 第五周项目2(2)
- 【Raspberry Pi系列】4. ARM指令
- corosync
- Fortran 循环
- 欧拉路 (Fleury算法)
- 全排列
- Mysql 安全问题
- HDU 1181
- 第五周项目2(1)
- 课堂翻译