标签:
/*
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;
}
运行结果:
知识点总结:
布尔型的使用
学习心得;
感觉很难
标签:
原文地址:http://blog.csdn.net/wucong111/article/details/51352945