HJ39判断两个IP是否属于同一子网(中)

提示:文章

文章目录

前言

接上文HJ39判断两个IP是否属于同一子网


查了下,atoi可以转换负数。

修改成下面的代码

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

bool isTargetSonNet(int array[], int len)
{
    int tempArray[32] = {0};
    int tempArrayIndex = 0;
    for(int i = 0; i < len; i++)
    {
        int data = array[i];
        if(data > 255 || data < 0)
        {
            return false;
        }
        int index = ((tempArrayIndex + 1) * 8);
        while(data)
        {
            int dat = data % 2;
            tempArray[--index] = dat;
            data /= 2;
        }
        tempArrayIndex++;
    }

    int count = 0;
    for(int i = 0; i + 1 < 32; i++)
    {
        int temp = tempArray[i] - tempArray[i + 1];
        if(temp != 0 && temp != 1)
        {
            return false;
        }
        if(temp == 1)
        {
            count++;
        }
    }

    if(count != 1)
    {
        return false;
    }

    return true;
}

int main() {
    int a, b;
    char sonNet[20] = {'\0'};
    char ip1[20] = {'\0'};
    char ip2[20] = {'\0'};
    int arraySonNet[4] = {0};
    int arrayIp1[4] = {0};
    int arrayIp2[4] = {0};
    
    while (scanf("%s", sonNet) != EOF) { // 注意 while 处理多个 case
        // 64 位输出请用 printf("%lld") to 
        char output = 'f';
        char delimiters[2] = ".";
        char* p = strtok(sonNet, delimiters);
        int index = 0;
        while(p != NULL)
        {
            //printf("%s\n", p);
            int data = atoi(p);
            
            // if(data > 255)
            // {
            //     output = '1';
            //     break;
            // }
            // else if(data != 255 && data != 0)
            // {
            //     output = '1';
            //     break;
            // }
            arraySonNet[index++] = data;
            p = strtok(NULL, delimiters);
        }
        if(!isTargetSonNet(arraySonNet, 4))
        {
            output = '1';
        }
        if(output != 'f')
        {
            printf("%c\n", output);
            break;
        }
        
        if(scanf("%s", ip1) != EOF)
        {
            p = strtok(ip1, delimiters);
            int index = 0;
            while(p != NULL)
            {
                //printf("%s\n", p);
                int data = atoi(p);
                if(data > 255 || data < 0)
                {
                    
                    output = '1';
                    break;
                }
                arrayIp1[index++] = data;
                p = strtok(NULL, delimiters);
            }
        }
        if(output != 'f')
        {
            printf("%c\n", output);
            break;
        }

        if(scanf("%s", ip2) != EOF)
        {
            p = strtok(ip2, delimiters);
            int index = 0;
            while(p != NULL)
            {
                //printf("%s\n", p);
                int data = atoi(p);
                if(data > 255 || data < 0)
                {
                    output = '1';
                    break;
                }
                arrayIp2[index++] = data;
                p = strtok(NULL, delimiters);
            }
        }
        if(output != 'f')
        {
            printf("%c\n", output);
            break;
        }

        int count = 0;
        for(int i = 0; i < 4; i++)
        {
            if( (ip1[i] & sonNet[i]) == (ip2[i] & sonNet[i]) )
            {
                ++count;
            }
        }
        if(output == 'f')
        {
            if(count == 4)
            {
                output = '0';
            }
            else {
                output = '2';
            }
        }
        
        
        printf("%c\n", output);
    }
    
    return 0;
}

验证一下demo示例

示例有错误

255.255.255.0
192.168.224.256
192.168.10.4
255.0.0.0
193.194.202.15
232.43.7.59
255.255.255.0
192.168.0.254
192.168.0.1

输出 1

修改成下面的代码也不行

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

bool isTargetSonNet(int array[], int len)
{
    int tempArray[32] = {0};
    int tempArrayIndex = 0;
    for(int i = 0; i < len; i++)
    {
        int data = array[i];
        if(data > 255 || data < 0)
        {
            return false;
        }
        int index = ((tempArrayIndex + 1) * 8);
        while(data)
        {
            int dat = data % 2;
            tempArray[--index] = dat;
            data /= 2;
        }
        tempArrayIndex++;
    }

    int count = 0;
    for(int i = 0; i + 1 < 32; i++)
    {
        int temp = tempArray[i] - tempArray[i + 1];
        if(temp != 0 && temp != 1)
        {
            return false;
        }
        if(temp == 1)
        {
            count++;
        }
    }

    if(count != 1)
    {
        return false;
    }

    return true;
}

int main() {
    int a, b;
    char sonNet[20] = {'\0'};
    char ip1[20] = {'\0'};
    char ip2[20] = {'\0'};
    int arraySonNet[4] = {0};
    int arrayIp1[4] = {0};
    int arrayIp2[4] = {0};
    
    while (scanf("%s", sonNet) != EOF) { // 注意 while 处理多个 case
        // 64 位输出请用 printf("%lld") to 
        char output = 'f';
        char delimiters[2] = ".";
        char* p = strtok(sonNet, delimiters);
        int index = 0;
        while(p != NULL)
        {
            //printf("%s\n", p);
            int data = atoi(p);
            
            // if(data > 255)
            // {
            //     output = '1';
            //     break;
            // }
            // else if(data != 255 && data != 0)
            // {
            //     output = '1';
            //     break;
            // }
            arraySonNet[index++] = data;
            p = strtok(NULL, delimiters);
        }
        if(!isTargetSonNet(arraySonNet, 4))
        {
            output = '1';
        }
        if(output != 'f')
        {
            printf("%c\n", output);
            break;
        }

        if(scanf("%s", ip1) != EOF)
        {
            p = strtok(ip1, delimiters);
            int index = 0;
            while(p != NULL)
            {
                //printf("%s\n", p);
                int data = atoi(p);
                if(data > 255 || data < 0)
                {
                    
                    output = '1';
                    break;
                }
                arrayIp1[index++] = data;
                p = strtok(NULL, delimiters);
            }
        }
        if(output != 'f')
        {
            printf("%c\n", output);
            continue;
        }

        if(scanf("%s", ip2) != EOF)
        {
            p = strtok(ip2, delimiters);
            int index = 0;
            while(p != NULL)
            {
                //printf("%s\n", p);
                int data = atoi(p);
                if(data > 255 || data < 0)
                {
                    output = '1';
                    break;
                }
                arrayIp2[index++] = data;
                p = strtok(NULL, delimiters);
            }
        }
        if(output != 'f')
        {
            printf("%c\n", output);
            continue;
        }

        int count = 0;
        for(int i = 0; i < 4; i++)
        {
            if( (ip1[i] & sonNet[i]) == (ip2[i] & sonNet[i]) )
            {
                ++count;
            }
        }
        if(output == 'f')
        {
            if(count == 4)
            {
                output = '0';
            }
            else {
                output = '2';
            }
        }
        
        printf("%c\n", output);
    }
    
    return 0;
}

示例有错误

255.255.255.0
192.168.224.256
192.168.10.4
255.0.0.0
193.194.202.15
232.43.7.59
255.255.255.0
192.168.0.254
192.168.0.1

输出

1
1
2
0

修改代码,修改成goto,但是还是不行,会打印4个输出,很奇怪,我就调试。调试后我意识到goto会跳过其他scanf操作。
应该先把数据采集完在处理。


总结

接下文:HJ39判断两个IP是否属于同一子网(下)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值