[编程题]判断两个IP是否属于同一子网

Talk is cheap, show me the code.

一、问题描述

子网掩码是用来判断任意两台计算机的IP地址是否属于同一子网络的根据。

二、问题分析

这道题又是一道坑题,题意没描述清楚,首先判断一个ip或者mask合法没给出判断标准,这道题是值范围都在 0-255 之间即可,其次居然255.0没有4位数字的mask也是合法的。除了这些问题外,这道题其实主要还是考察字符串处理,我用了多种方式去实现字符串的处理,包括直接用子串函数,利用STL库,采用字符串流sstream等多种方式,最终得出的结论是,还是字符串流好用啊,以后就用字符串流了。

解题方式1:

采用字符串流,最佳实践。不过要注意这道题输入的数字没有4位也算对,当从字符串流读取数字时如果没有读取到4位数字,那么没有赋值的变量一定要有默认值的,所以这种实现一定要给变量初始化啊,不然就成了不在0-255之间的数了,会出问题。

#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int main()
{
    string mask, ip1, ip2;
    while (cin >> mask >> ip1 >> ip2)
    {
        unsigned int mask1 = 0, mask2 = 0, mask3 = 0, mask4 = 0;
        unsigned int ip1a = 0, ip1b = 0, ip1c = 0, ip1d = 0;
        unsigned int ip2a = 0, ip2b = 0, ip2c = 0, ip2d = 0;
        char ch;
        stringstream ss;
        ss << mask;
        ss >> mask1 >> ch >> mask2 >> ch >> mask3 >> ch >> mask4;
        ss.clear();
        ss << ip1;
        ss >> ip1a >> ch >> ip2b >> ch >> ip2c >> ch >> ip2d;
        ss.clear();
        ss << ip2;
        ss >> ip2a >> ch >> ip2b >> ch >> ip2c >> ch >> ip2d;
        if (mask1 < 0 || mask1 > 255 || mask2 < 0 || mask2 > 255 || mask3 < 0 || mask3 > 255 || mask4 < 0 || mask4 > 255 || ip1a < 0 || ip1a > 255 || ip1b < 0 || ip1b > 255 || ip1c < 0 || ip1c > 255 || ip1d < 0 || ip1d > 255 || ip2a < 0 || ip2a > 255 || ip2b < 0 || ip2b > 255 || ip2c < 0 || ip2c > 255 || ip2d < 0 || ip2d > 255)
        {
            cout << 1 << endl;
        }
        else {
            if ((ip1a & mask1) == (ip2a & mask1) && (ip1b & mask2) == (ip2b & mask2) && (ip1c & mask3) == (ip2c & mask3) && (ip1d & mask4) == (ip2d & mask4))
        {
            cout << 0 << endl;
        } else {
            cout << 2 << endl;
        }
        }
    }

    return 0;
}

解题方式2:

尝试其他字符串处理的方式,不过都略显繁琐,建议还是采用解题方式1中的字符串流。

#include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
using namespace std;

int checkNetSegment(string mask, string ip1, string ip2)
{
    istringstream is(mask);
    unsigned int a, b, c ,d;
    string temp;
    getline(is, temp, '.');
    a = atoi(temp.c_str());
    getline(is, temp, '.');
    b = atoi(temp.c_str());
    getline(is, temp, '.');
    c = atoi(temp.c_str());
    getline(is, temp, '.');
    d = atoi(temp.c_str());
    if ( a < 0 || a > 255 || b < 0 || b > 255 || c < 0 || c > 255 || d < 0 || d > 255)
        return 1;
    unsigned int x, y, m, n;
    x = atoi(ip1.substr(0, ip1.find_first_of('.')).c_str());
    temp = ip1.substr(ip1.find_first_of('.') + 1);
    y = atoi(temp.substr(0, temp.find_first_of('.')).c_str());
    temp = temp.substr(temp.find_first_of('.') + 1);
    m = atoi(temp.substr(0, temp.find_first_of('.')).c_str());
    temp = temp.substr(temp.find_first_of('.') + 1);
    n = atoi(temp.substr(0, temp.find_first_of('.')).c_str());
    if (x < 0 || x > 255 || y < 0 || y > 255 || m < 0 || m > 255 || n < 0 || n > 255)
        return 1;
    unsigned int e, f, g, h;
    e = a & x;
    f = b & y;
    g = c & m;
    h = d & n;
    unsigned int i, j, k, p;
    string::iterator it1 = find(ip2.begin(), ip2.end(), '.');
    i = atoi(ip2.substr(0, it1 - ip2.begin()).c_str());
    string::iterator it2 = find(it1 + 1, ip2.end(), '.');
    j = atoi(ip2.substr(it1 - ip2.begin() + 1, it2 - it1).c_str());
    string::iterator it3 = find(it2 + 1, ip2.end(), '.');
    k = atoi(ip2.substr(it2 - ip2.begin() + 1, it3 - it2).c_str());
    p = atoi(ip2.substr(it3 - ip2.begin() + 1, ip2.end() - it3).c_str());
    if (i < 0 || i > 255 || j < 0 || j > 255 || k < 0 || k > 255 || p < 0 || p > 255)
        return 1;
    if (e == (a & i) && f == (b & j) && g == (c & k) && h == (d & p))
    {
        return 0;
    }
    return 2;
}

int main()
{
    string mask, ip1, ip2;
    while (cin >> mask >> ip1 >> ip2)
    {
        cout << checkNetSegment(mask, ip1, ip2) << endl;
    }

    return 0;
}
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值