华为笔试: 判断两个IP是否为同一网段(c/c++实现)

该博客介绍了如何使用C/C++编程判断两个IP地址是否属于同一个网络段。通过与子网掩码进行位运算比较,如果得到相同的与运算结果,则认为两个IP在同一网段。
摘要由CSDN通过智能技术生成
问题描述:

判断两个Ip是否为同一个网段: Ip1,Ip2分别与子网淹Ip3进行与操作,结果相同则判断为同一个网段,输出结构:1 与操作结果; 否则输出0;

#include 
#include
#include
#define Length 4
using namespace std;

void printIntArray(int * array){
    for ( int i = 0; i < Length; i++ )
        cout << array[i] << "  ";
    cout << endl;
}

int charToInt(char ch, int sum){
    int result = sum * 10 + ( ch - '0' );
    return result;
}

void getIntOfIp(char * ip, int * intArray){
    int count = 0;
    int temp = 0;
    while ( *ip != '\0' ){

        if ( *ip == '.' ){
            intArray[count] = temp;
            count ++;
            temp = 0;    
        }else if ( count == 0 ){
            temp = charToInt(*ip, temp);
        }else if ( count == 1 ){
            temp = charToInt(*ip, temp);
        }else if ( count == 2 ){
  
  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值