[Huawei machine trial exercise] 54. Determine whether the IP addresses of any two computers belong to the same subnetwork.

Source: Internet
Author: User

[Huawei machine trial exercise] 54. Determine whether the IP addresses of any two computers belong to the same subnetwork.

Question

Description:

The subnet mask is used to determine whether the IP addresses of any two computers belong to the same subnetwork. The simplest understanding is that after the IP address AND subnet mask of the two computers perform the AND operation, if the result is the same, the two computers are on the same sub-network and can communicate directly. That's simple.

See the following example:
Computing Demonstration:

IP Address: 192.168.0.1 subnet mask: 255.255.255.0AND is converted to binary for calculation: IP Address: 1102.16.10101000.00000000.00000001 subnet mask: mask AND operation: 1102.16.10101000.00000000.00000000 converted to decimal for: 192.168.0.0

Operation demonstration 2:

IP Address: 192.168.0.254 subnet mask: 255.255.255.0 AND is converted to binary for calculation: IP Address: 1102.16.10101000.20.2.16.111110 subnet mask: mask AND operation: 1102.16.10101000.20.2.16.00000000 after being converted to decimal: after performing the AND operation on the IP addresses AND subnet masks of the two computers, 192.168.0.0 returns the same result. All are 192.168.0.0, so the two computers can be considered as the same sub-network.

Interface Description
Prototype:

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

Input parameters:

Char * pcIP1: IP address of Computer 1. 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 subnetwork. 1: IP1 and IP2 belong to the same sub-network.

Exercise phase:

Elementary

Code

/* ------------------------------------- * Date: 2015-07-05 * Author: SJF0115 * question: Determine whether the IP addresses of any two computers belong to the same subnetwork * Source: huawei machine trial exercise questions ----------------------------------------- */# include <iostream> # include "OJ. h "# include <string >#include <vector> using namespace std;/* function: determines whether the IP addresses of two computers are in the same subnetwork. Prototype: int IsSameSubNetwork (char * pcIp1, char * pcIp2, char * pcSubNetworkMask); input parameter: char * pcIP1: IP address of Computer 1, format: "192.168.0.254"; char * pcIP2: IP address of computer 2, in the format of "192.168.0.1"; char * pcSubNetworkMask: Subnet Mask, in the format of "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 subnetwork. * // check whether the subnet mask and IP address format are correct and return the segmented bool CheckIP (string str, vector <int> & numVec) {int size = str. size (); int pointCount = 0; string: size_type index = 0; int prePoint = 0; vector <string> part; while (index = str. find_first_of ('.', index ))! = String: npos ){//.. there is a number 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 int partSize = part. size (); if (partSize! = 4) {return false;} // if // determines that each part belongs to 0-255 int num; for (int I = 0; I <partSize; ++ I) {num = atoi (part [I]. c_str (); numVec. push_back (num); if (num <0 | num> 255) {return false;} // if} // for // indicates the wrong IP address if (pointCount! = 3) {return false;} // if return true;} int IsSameSubNetwork (char * pcIp1, char * pcIp2, char * pcSubNetworkMask) {if (pcIp1 = NULL | pcIp2 = NULL | pcSubNetworkMask = NULL) {return 0;} // if // convert it to string (used) string ip1 (pcIp1); string ip2 (pcIp2); string net (pcSubNetworkMask); vector <int> ip1Vec; vector <int> ip2Vec; vector <int> netVec; int result, result2; // ip subnet mask input valid if (CheckIP (ip1, ip1Vec) & CheckIP (ip2, ip2Vec) & CheckIP (net, netVec) {for (int I = 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 Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.