结构体:子网掩码

2023-12-30 23:30:35

?

?

#include<iostream>
using namespace std;
union IP  //创建共用体
{
	unsigned char a[4];
	unsigned int ip;
};
IP getIP()  //获取ip函数
{
	int a, b, c, d;
	scanf_s("%d.%d.%d.%d", &a, &b, &c, &d);
	IP address;
	address.a[3] = a; address.a[2] = b;
	address.a[1] = c; address.a[0] = d;
	return address;
}
int main()
{
	IP host, mask, otherip;
	host = getIP();  //获取主机ip
	mask = getIP();  //获取掩码ip
	int n;
	cin >> n; //有n个其他网
	for (int i = 0; i < n; i++)
	{
		otherip = getIP();  //获取其他网的ip
		//判断主机和其他网是否是内网
		if ((host.ip & mask.ip) == (otherip.ip & mask.ip))
			cout << "inner" << endl;
		else
			cout << "outer" << endl;
	}
	return 0;
}

?

文章来源:https://blog.csdn.net/2303_80050865/article/details/135310525
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。