求两个ip地址在子网掩码下是否为同一网段

#include <iostream>
#include <string>
#include <vector>
#include <sstream>
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 s0, int s1, int s2, int s3);  //构造函数
	void showIP();  //用四段法显示IP地址
	bool sameSubnet(const IP &ip, const IP &mark, unsigned int &newIp);  //判断是否处于同一子网
};


IP::IP(int s0, int s1, int s2, int s3)
{
	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 &newIp)
{
	//与子网掩码按位与,是计算机内部的操作,直接以一个整体操作更方便
	unsigned int i1, i2;
	i1 = address&mark.address; //和子网掩码作逻辑与运算
	//cout << i1 << endl;
	i2 = ip.address&mark.address; //和子网掩码作逻辑与运算
	newIp = i1;
	return (i1 == i2); //与子网掩码的逻辑与运算结果相同,则属于同一子网
}

void SplitString(const string& s, vector<string>& v, const string& c)
{
	string::size_type pos1, pos2;
	pos2 = s.find(c);
	pos1 = 0;
	while (string::npos != pos2)
	{
		v.push_back(s.substr(pos1, pos2 - pos1));

		pos1 = pos2 + c.size();
		pos2 = s.find(c, pos1);
	}
	if (pos1 != s.length())
		v.push_back(s.substr(pos1));
}

int str2int(const string &string_temp)
{
	int int_temp;
	stringstream stream(string_temp);
	stream >> int_temp;
	return int_temp;
}

string ip_int2string(unsigned int ip_value)
{
	//数值转化为IP 
	std::stringstream str; //stringstream的用法看另一篇文章 
	str << ((ip_value & 0xff000000) >> 24) << "."
		<< ((ip_value & 0x00ff0000) >> 16) <<  "."
		<< ((ip_value & 0x0000ff00) >> 8) <<  "."
		<< (ip_value & 0x000000ff);

	return str.str();
}
/*
192.168.1.1 192.168.1.2 255.255.255.0
*/
int main()
{
	string ip1, ip2, mask;
	cin >> ip1 >> ip2 >> mask;
	vector<string> v1,v2,v3;
	SplitString(ip1, v1, "."); //可按多个字符来分隔;
	SplitString(ip2, v2, "."); //可按多个字符来分隔;
	SplitString(mask, v3, "."); //可按多个字符来分隔; 
	int ip1_1 = str2int(v1[0]);
	int ip1_2 = str2int(v1[1]);
	int ip1_3 = str2int(v1[2]);
	int ip1_4 = str2int(v1[3]);

	IP iip1(ip1_1, ip1_2, ip1_3, ip1_4);

	int ip2_1 = str2int(v2[0]);
	int ip2_2 = str2int(v2[1]);
	int ip2_3 = str2int(v2[2]);
	int ip2_4 = str2int(v2[3]);
	IP iip2(ip2_1, ip2_2, ip2_3, ip2_4);

	int ip3_1 = str2int(v3[0]);
	int ip3_2 = str2int(v3[1]);
	int ip3_3 = str2int(v3[2]);
	int ip3_4 = str2int(v3[3]);
	IP mmask(ip3_1, ip3_2, ip3_3, ip3_4);
	unsigned int newIp = 0;
	if (iip1.sameSubnet(iip2, mmask, newIp))
		cout << "1"<<" ";
	else
		cout << "0" << " ";
	//cout << newIp << endl;
	cout << ip_int2string(newIp) << endl;
	return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值