[Huawei Machine Test exercises]54. Determine whether the IP addresses of any two computers belong to the same subnet

Source: Internet
Author: User

Topic

Describe:

子网掩码是用来判断任意两台计算机的IP地址是否属于同一子网络的根据。最为简单的理解就是两台计算机各自的IP地址与子网掩码进行AND运算后,如果得出的结果是相同的,则说明这两台计算机是处于同一个子网络上的,可以进行直接的通讯。就这么简单。

Take a look at the following example:
One of the operation demos:

IP地址:192.168.0.1子网掩码:255.255.255.0AND运算转化为二进制进行运算:IP地址:11010000.10101000.00000000.00000001 子网掩码:11111111.11111111.11111111.00000000 AND运算:11010000.10101000.00000000.00000000 转化为十进制后为: 192.168.0.0

The second of the Operation demo:

IP地址:192.168.0.254 子网掩码:255.255.255.0 AND运算转化为二进制进行运算:IP地址:11010000.10101000.00000000.11111110 子网掩码:11111111.11111111.11111111.00000000 AND运算:11010000.10101000.00000000.00000000 转化为十进制后为:192.168.0.0通过以上对两台计算机IP地址与子网掩码的AND运算后,我们可以看到它运算结果是一样的。均为192.168.0.0,所以这二台计算机可视为是同一子网络。

Interface description
Prototype:

int IsSameSubNetwork(char * pcIp1, char * pcIp2, char * pcSubNetworkMask);

Input parameters:

char * pcIP1: 计算机1的IP地址,格式:“192.168.0.254”char * pcIP2: 计算机2的IP地址,格式:“192.168.0.1”char * pcSubNetworkMask: 子网掩码,格式:“255.255.255.0”

return value:

0:IP1与IP2不属于同一子网络。1:IP1与IP2属于同一子网络。

Practice Stage:

Code

/* ---------------------------------------* Date: 2015-07-05* sjf0115* title: Determine whether the IP addresses of any two computers belong to the same subnet * Source: Huawei Machine Test exercises---- -------------------------------------*/#include <iostream>#include "OJ.h"#include <string>#include <vector>using namespace STD;/* Function: Determine the IP address of the two computers is the same subnet. Prototype: int issamesubnetwork (char * pcIp1, char * pcIp2, char * pcsubnetworkmask); input parameter: char * pcIP1: Computer 1 IP address, format: "192. 168.0.254 "; char * pcIP2: IP address of Computer 2, format:" 192.168.0.1 "; char * pcsubnetworkmask: Subnet mask, Format:" 255.255.255.0 "; return value: 0:ip1 And IP2 do not belong to the same sub-network; 1:ip1 and IP2 belong to the same subnet; *///Check that the subnet mask and IP format are correct and return fragmentationBOOLCheckip (stringStr vector<int>&numvec) {intSize = Str.size ();intPointcount =0;string:: Size_type index =0;intPrepoint =0; vector<string>Part while(Index = str.find_first_of ('. ', index))! =string:: NPOs) {//.. There are numbers between        if(Index > Prepoint)        {Part.push_back (Str.substr (prepoint,index-prepoint)); }//if++index;        Prepoint = index;    ++pointcount; }//while    if(Prepoint < size)    {Part.push_back (Str.substr (prepoint)); }//if    intPartsize = Part.size ();if(Partsize! =4){return false; }//if    //Judge that each part belongs to 0-255    intNum for(inti =0; i < partsize;++i) {num = Atoi (PART[I].C_STR ()); Numvec.push_back (num);if(Num <0|| num >255){return false; }//if}//for    //On behalf of the wrong IP    if(Pointcount! =3){return false; }//if    return true;}intIssamesubnetwork (Char* PCIP1,Char* PCIP2,Char* Pcsubnetworkmask) {if(PCIP1 = = NULL | | pcIp2 = = NULL | | pcsubnetworkmask = = NULL) {return 0; }//if    //Convert to String (own custom)    stringIP1 (PCIP1);stringIP2 (PCIP2);stringNET (pcsubnetworkmask); vector<int>Ip1vec; vector<int>Ip2vec; vector<int>Netvec;intRESULT,RESULT2;//IP subnet Mask input legal    if(Checkip (Ip1,ip1vec) && Checkip (Ip2,ip2vec) && Checkip (Net,netvec)) { for(inti =0; I <4; ++i) {result = Ip1vec[i] & Netvec[i]; RESULT2 = Ip2vec[i] & Netvec[i];if(Result! = RESULT2) {return 0; }//if}//for}//if    Else{return 0; }//else    return 1;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

[Huawei Machine Test exercises]54. Determine whether the IP addresses of any two computers belong to the same subnet

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.