IP地址
2016-04-12 21:58
183 查看
/* *Copyright (c) 2016,烟台大学计算机学院 *All rights reserved. *文件名称:main.cpp *作 者:郭辉 *完成时间:2016年4月7日 *版 本 号: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 a,int b,int c,int d) { seg3=a; seg2=b; seg1=c; seg0=d; } void IP::showIP() { cout<<int(seg3)<<"."<<int(seg2)<<"."<<int(seg1)<<"."<<int(seg0)<<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(seg3<128) return 'A'; else if(seg3<192) return 'B'; else if(seg3<224) return 'C'; else if(seg3<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; }
相关文章推荐
- 机器学习:浅谈先验概率,后验概率
- 编译Opencv的GPU,利用CUDA加速
- AWR
- 窗体程序调C写DLL(其中调C++写DLL),Runtime Error
- C++ Primer Plus编程练习7.13-5
- Runtime(一)
- Linux监视显卡使用情况
- 关于指针的一些总结
- docker(2):安装docker管理工具rancher
- 有关需求规格说明
- 第一行代码 读笔⑤ (Broadcast)
- 创建远程线程实现DLL注入
- App更新策略课程-检查更新
- 【bzoj4296】【PA2015】【Mistrzostwa】【bfs+dfs】
- Pycharm及Sublime Text输出中文
- JS实现循环给元素绑定事件的几个常用方法
- ubuntu14.04 64位 jd-gui
- poj 1125 Stockbroker Grapevine
- spring mvc 上传文件
- netfilter入门