IP地址类
来源:互联网 发布:php 布尔型 编辑:程序博客网 时间:2024/05/16 23:55
问题及代码:
/* *copyright(c) 2014,烟台大学计算机学院 *All rights reserved *文件名称:test.cpp *作者:杨昊 *版本:v6.0 *时间;2016年4月10日 * *问题描述: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) { seg0=a; seg1=b; seg2=c; seg3=d; } //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); } /////////////////////////// void IP:: showIP() { int a=seg0; int b=seg1; int c=seg2; int d=seg3; cout<<a<<"."<<b<<"."<<c<<"."<<d<<endl; } 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; }
0 0
- IP地址类
- IP地址类
- IP地址类
- IP地址类
- IP地址类
- IP地址类
- IP地址类
- ip地址类
- IP地址类
- IP地址类
- IP地址类
- IP地址类
- IP地址类
- ip地址类
- 五类IP地址
- 私有IP,A类IP地址,B类IP地址,C类IP地址
- [网络TCP/IP]五类IP地址
- IP五类地址划分
- LeetCode *** 121. Best Time to Buy and Sell Stock
- CQOI 2016 day1日志
- 九度 1012
- 解决ZXING生成二维码图片白框太大的问题
- hybrid iOS 信息收藏
- IP地址类
- MySQL 入门(四)—— 数据库操作
- 关于数据库导出excle上限问题
- 微型计算机原理与接口技术8086cpu的寻址方式
- 关于BaseAdapter的使用及优化心得
- 一个HR的离职感悟,说的太好了!
- 几种任务调度的 Java 实现方法与比较
- Redis - 安装
- 多叉树的递归和非递归遍历