(C++编程)子网划分工具

问题:C++编程实现一个子网划分的工具,具体要求:
(1)能够根据用户输入的网络地址,判断出用户输入的合法性及网络的类别(A、B、C地址);
(2)能够根据借位数,计算出:子网掩码、子网数、每个子网的主机数、每个子网网络地址、可用IP范围及广播地址。
(3)应有相应的输入提示及良好的结果输出组织,易于用户使用和查看结果。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

#define IOS                      \
    ios::sync_with_stdio(false); \
    cin.tie(0)
//*writer:cjr
// *start on @date: 2021-05-10 19:47
ll printbinary(ll n) //判断IP的类别
{
    ll mm;
    ll i = 0, a[12];
    memset(a, 0, sizeof(a));
    while (n > 0) //循环过程的始终,判断n是否能被2除尽
    {
        a[i] = n % 2; //用数组存储每次除以2之后的余数,即断定奇偶性,对应二进制位上数值
        i = i + 1;
        n = n / 2;
    }
    if (a[7] == 1 && a[6] == 1 && a[5] == 0)
    {
        mm = 24;
        cout << "该网络的类别为C地址" << endl
             << endl;
    }
    else if (a[7] == 1 && a[6] == 0)
    {
        mm = 16;
        cout << "该网络的类别为B地址" << endl
             << endl;
    }
    else if (a[7] == 0)
    {
        mm = 8;
        cout << "该网络的类别为A地址" << endl
             << endl;
    }
    return mm;
}

int main()
{
    IOS;
    ll ip1, ip2, ip3, ip4, ip5;
    cout << "请输入IP地址:" << endl;
    while (1)
    {
        scanf("%lld.%lld.%lld.%lld/%lld", &ip1, &ip2, &ip3, &ip4, &ip5);
        if (ip1 >= 0 && ip1 <= 255 && ip2 >= 0 && ip2 <= 255 && ip3 >= 0 && ip3 <= 255 && ip4 >= 0 && ip4 <= 255 && ip5 >= 0 && ip5 <= 32)
        {
            cout << "1:IP地址输入合法且";

            ll m1 = printbinary(ip1); //判断IP的类别
            ll value = 0;
            if (m1 == 24) //如果IP为C类;200.200.200.0/27
            {
                cout << "2:该IP地址的子网掩码是:255.255.255.";
                ll t = 7;
                for (ll q = 1; q <= ip5 - 24; q++)
                {
                    value += pow(2, t);
                    t--;
                }
                cout << value << endl
                     << endl;
                cout << "3:子网数是:";
                ll zw = pow(2, ip5 - 24);
                cout << zw << endl
                     << endl;
                cout << "4:每个子网的主机数是:";
                ll zi = pow(2, 32 - ip5);
                cout << zi << endl
                     << endl;
                ll jia = zi;
                cout << "5:以下为每个子网网络地址、可用IP范围及广播地址:" << endl;
                ll iip = ip4;

                // cout << "111" << endl;
                for (ll z = 1; z <= zw; z++)
                {
                    cout << "(" << z << ")"
                         << "网络地址为:" << ip1 << "." << ip2 << "." << ip3 << ".";
                    if (z == 1)
                        cout << iip;
                    else
                        cout << ++iip;
                    cout << "      可用IP范围为: " << ip1 << "." << ip2 << "." << ip3 << "." << ++iip << "~" << ip1 << "." << ip2 << "." << ip3 << ".";
                    iip = iip + (zi - 3);
                    cout << iip;
                    cout << "      广播地址为:" << ip1 << "." << ip2 << "." << ip3 << "." << ++iip << endl
                         << endl;
                }
                goto re;
            }
            else if (m1 == 16) //如果IP为B类;172.16.0.0/20  172.16.0.0/26
            {

                ll mmm = ip5 - 16;
                ll zw = pow(2, mmm);
                //  cout << mmm << "  " << zw << endl;
                cout << "2:该IP地址的子网掩码是:255.255.";
                if (ip5 <= 24 && ip5 >= 16)
                {
                    ll t = 7;
                    for (ll q = 1; q <= ip5 - 16; q++)
                    {
                        value += pow(2, t);
                        t--;
                    }
                    cout << value << ".0" << endl
                         << endl;
                }
                else if (ip5 > 24 && ip5 <= 32)
                {
                    cout << "255.";
                    ll t = 7;
                    for (ll q = 1; q <= ip5 - 24; q++)
                    {
                        value += pow(2, t);
                        t--;
                    }
                    cout << value << endl
                         << endl;
                }
                cout << "3:子网数是:";
                cout << zw << endl
                     << endl;
                cout << "4:每个子网的主机数是:";
                ll zi = pow(2, 32 - ip5);
                cout << zi << endl
                     << endl;
                cout << "5:以下为每个子网网络地址、可用IP范围及广播地址:" << endl;
                if (ip5 >= 18 && ip5 <= 24) //172.16.0.0/20;
                {
                    ll q1 = 0, q2 = 255, q3 = 254, q4 = 1, q5;
                    for (ll z = 1; z <= zw; z++)
                    {
                        cout << "(" << z << ")"
                             << "网络地址为:" << ip1 << "." << ip2 << ".";
                        if (z == 1)
                            cout << ip3 << ".";
                        else
                        {
                            ip3 += 1;
                            cout << ip3 << ".";
                        }
                        cout << q1;
                        cout << "      可用IP范围为: " << ip1 << "." << ip2 << "." << ip3 << "." << q4 << "~" << ip1 << "." << ip2 << ".";
                        ip3 += zw - 1;

                        cout << ip3 << ".";

                        cout << q3;
                        cout << "      广播地址为:" << ip1 << "." << ip2 << "." << ip3 << "." << q2 << endl
                             << endl;
                    }
                    goto re;
                }
                else if (ip5 >= 25 && ip5 <= 30) //172.16.0.0/26
                {
                    ll q1 = 0, q2 = 255, q3 = 254, q4 = 1, q5, iip4 = ip4;
                    // cout << iip4;
                    // cin >> q1;
                    ll jj = 1;
                    for (; ip3 <= 255;)
                    {
                        for (ll z = 1; ip4 + zi <= 255; z++)
                        {
                            cout << "(" << jj++ << ")"
                                 << "网络地址为:" << ip1 << "." << ip2 << "." << ip3 << ".";
                            if (z == 1)
                            {
                                cout << ip4;
                                // ip4 += 1;
                            }
                            else
                            {
                                ip4 += 1;
                                cout << ip4;
                            }
                            ip4 += 1;
                            cout << "      可用IP范围为: " << ip1 << "." << ip2 << "." << ip3 << "." << ip4 << "~" << ip1 << "." << ip2 << "." << ip3 << ".";
                            ip4 += zi - 3;
                            cout << ip4;
                            ip4 += 1;
                            cout << "      广播地址为:" << ip1 << "." << ip2 << "." << ip3 << "." << ip4 << endl
                                 << endl;
                        }
                        ip3 += 1;
                        ip4 = iip4;
                    }
                    goto re;
                }
            }
            else if (m1 == 8) //如果IP为A类;
            {
                ll mmm = ip5 - 8;
                ll zw = pow(2, mmm);
                //  cout << mmm << "  " << zw << endl;
                cout << "2:该IP地址的子网掩码是:255.";
                if (ip5 > 8 && ip5 <= 16)
                {
                    ll t = 7;
                    for (ll q = 1; q <= ip5 - 8; q++)
                    {
                        value += pow(2, t);
                        t--;
                    }
                    cout << value << ".0.0" << endl
                         << endl;
                }
                else if (ip5 <= 24 && ip5 >= 16)
                {
                    cout << "255.";
                    ll t = 7;
                    for (ll q = 1; q <= ip5 - 16; q++)
                    {
                        value += pow(2, t);
                        t--;
                    }
                    cout << value << ".0" << endl
                         << endl;
                }
                else if (ip5 > 24 && ip5 <= 32)
                {
                    cout << "255.255.";
                    ll t = 7;
                    for (ll q = 1; q <= ip5 - 24; q++)
                    {
                        value += pow(2, t);
                        t--;
                    }
                    cout << value << endl
                         << endl;
                }
                cout << "3:子网数是:";
                cout << zw << endl
                     << endl;
                cout << "4:每个子网的主机数是:";
                ll zi = pow(2, 32 - ip5);
                cout << zi << endl
                     << endl;
                if (ip5 >= 10 && ip5 <= 16) //30.0.0.0/11
                {
                    cout << "5:以下为每个子网网络地址、可用IP范围及广播地址:" << endl;
                    ll q1 = 0, q2 = 255, q3 = 254, q4 = 1, q5, iip4 = ip4 + 1;
                    for (ll z = 1; z <= zw; z++)
                    {
                        cout << "(" << z << ")"
                             << "网络地址为:" << ip1 << ".";
                        ll ss = 256 / zw;
                        ll iip2 = ip2 + ss - 1;
                        if (z == 1)
                        {
                            cout << ip2 << ".";
                        }
                        else
                        {
                            //ip2 += ss;
                            cout << ip2 << ".";
                        }
                        cout << ip3 << "." << ip4;

                        cout << "      可用IP范围为: " << ip1 << "." << ip2 << "." << q2 << "." << q3 << "~" << ip1 << ".";

                        cout << iip2 << "." << q2 << "." << q3;
                        cout << "      广播地址为:" << ip1 << "." << iip2 << "." << q2 << "." << q2 << endl
                             << endl;
                        ip2 = iip2 + 1;
                        iip2 += ss;
                    }
                }
                goto re;
            }
        }
        else
        {
            cout << "IP地址输入不合法,请重新输入!" << endl;
        }
    re:
        cout << "输入1选择继续输入IP地址" << endl;
        ll tt;
        cin >> tt;
        if (tt != 1)
            break;
        else
        {
            system("cls"); ///清屏
            cout << "请输入IP地址:" << endl;
        }
    }
    system("pause");
    return 0;
}

效果截图:

在这里插入图片描述

评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值