//
// main.cpp
// project
//
// Created by 徐伟 on 6/14/16.
// Copyright © 2016 fizz_i. All rights reserved.
//
#include <iostream>
using namespace std;
class IP
{
private:
union
{
struct
{
unsigned char seg0;
unsigned char seg1;
unsigned char seg2;
unsigned char seg3;
}; //IP地址共4字节,或者看成结构体所指4部分
unsigned int address; //或者看成一个整体
};
public:
IP(int=0,int=0,int=0,int=0); //构造函数
void showIP(); //用四段法显示IP地址
bool sameSubnet(const IP &ip, const IP &mark); //判断是否处于同一子网
char whatKind(); //返回属于哪一类网络
};
IP::IP(int s0,int s1,int s2,int s3)
{
//下面赋值的顺序,涉及整型数据存储结构,参考[《整型数据在内存中如何存储?》](http://blog.csdn.net/sxhelijian/article/details/51015706)
seg3=s0;
seg2=s1;
seg1=s2;
seg0=s3;
}
void IP::showIP()
{
//显示结果,是给人看的,分4段合适
cout<<int(seg3)<<"."<<int(seg2)<<"."<<int(seg1)<<"."<<int(seg0)<<endl;
return;
}
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()
{
//IP地址的类型,请到百度百科中搜索《IP地址》
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;
}
IP地址类
最新推荐文章于 2022-04-19 17:20:07 发布