IP地址类
来源:互联网 发布:网络门视频种子 编辑:程序博客网 时间:2024/06/06 13:03
/* 2.2. 3.3. 2. 4.4. 5.5. 3. *Copyright (c) 2016,烟台大学计算机学院 6.6. 7.7. 4. 8.8. 9.9. 5. *All rights reserved. 10.10. 11.11. 6. 12.12. 13.13. 7. *文件名称:test.cpp 14.14. 15.15.8. 16.16. 17.17. 9. *作者: 武聪 18.18. 19.19. 10. 20.20. 21.21. 11. *完成日期:2016年5月9 日 22.22. 23.23.12. 24.24. 25.25. 13. *版本号:v1.0 26.26. 27.27. 14. *问题描述:设计一个IP地址类,用于保存IP地址,并实施在IP地址上的一些操作28.28. 29.29.15. *输入描述: 30.30. 31.31.16. *程序描述:32.32. 33.33. 17. */ 34.#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(); };35.IP::IP(int s0,int s1,int s2,int s3){ seg3=s0; seg2=s1; seg1=s2; seg0=s3;}36.void IP::showIP(){ cout<<int(seg3)<<"."<<int(seg2)<<"."<<int(seg1)<<"."<<int(seg0)<<endl; return;}37.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); }38.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';}39.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;}
运行结果:
知识点总结:
布尔型的使用
学习心得;
感觉很难
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五类地址划分
- AutoCAD使用总结…
- 1.2.第一个实验——点亮LED灯
- 解决传说中的 Android 65k 问题
- android开发glide图片加载框架使用示例
- 文件-工资处理
- IP地址类
- Java多线程编程7--拾遗增补--线程的状态(new,runnable,terminated,timed_waiting,waiting,blocked)
- 选择排序
- 游戏开发学习指导
- 错误笔记
- 深入剖析ThreadLocal
- 音视频框架ffmpeg源码的简要分析
- 随手一点小记录
- java里面的FutureTask简单使用(配合源码讲解)