import java.io.*;

public class Subnet { public static void main(String[] args) throws IOException { String ip; // 用于接收要划分的IP int s; // 用于接收要划分的子网数 int n; // 输入子网数对应的二进制位数

    System.out.println("---------------划分子网---------------");
    // 输入要划分的IP地址:ip
    System.out.println("请输入要划分的IP地址(格式为:x1.x2.x3.x4):");
    System.out.print("→:");
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    ip = br.readLine();

    String[] ipArray = ip.split("\\."); // 将IP地址按照"."分割成数组
    int x1 = Integer.parseInt(ipArray[0]); // 获取x1
    int x2 = Integer.parseInt(ipArray[1]); // 获取x2
    int x3 = Integer.parseInt(ipArray[2]); // 获取x3
    int x4 = Integer.parseInt(ipArray[3]); // 获取x4

    // 根据x1判断IP地址能划分的最大子网数
    int maxSubnets = 0;
    if (x1 > 0 && x1 <= 126) {
        maxSubnets = (int) Math.pow(2, 24 - 2) - 2;
    } else if (x1 >= 128 && x1 <= 191) {
        maxSubnets = (int) Math.pow(2, 16 - 2) - 2;
    } else if (x1 >= 192 && x1 <= 223) {
        maxSubnets = (int) Math.pow(2, 8 - 2) - 2;
    }
    System.out.println("本IP地址能划分划分的最大子网数为: " + maxSubnets);

    // 输入要划分的子网数:s
    System.out.println("请输入要划分的子网数:");
    System.out.print("→:");
    String subnetNumStr = br.readLine();
    s = Integer.parseInt(subnetNumStr);

    // 求所输入子网数对应的二进制位数:n
    n = 1;
    while (Math.pow(2, n) - 2 < s) {
        n++;
    }

    System.out.println("--------------------------------------");
    // 求子网掩码
    int sum = 0;
    for (int i = 0; i < n; i++) {
        sum = sum + (int) Math.pow(2, (7 - i));
    }
    System.out.println("子网掩码为:");
    if (x1 > 0 && x1 <= 126) { // A类
        if (n <= 8) {
            System.out.println("255." + sum + ".0.0");
        } else if (n > 8 && n <= 16) {
            n = n - 8;
            sum = 0;
            for (int i = 0; i < n; i++) {
                sum = sum + (int) Math.pow(2, (7 - i));
            }
            System.out.println("255.255." + sum + ".0");
        } else {
            n = n - 16;
            sum = 0;
            for (int i = 0; i < n; i++) {
                sum = sum + (int) Math.pow(2, (7 - i));
            }
            System.out.println("255.255.255." + sum);
        }
    } else if (x1 >= 128 && x1 <= 191) { // B类
        if (n <= 8) {
            System.out.println("255.255." + sum + ".0");
        } else {
            n = n - 8;
            sum = 0;
            for (int i = 0; i < n; i++) {
                sum = sum + (int) Math.pow(2, (7 - i));
            }
            System.out.println("255.255.255." + sum);
        }
    } else if (x1 >= 192 && x1 <= 223) { // C类
        System.out.println("255.255.255." + sum);
    }

    // 求每网段主机数
    System.out.println("每网段主机数:");
    int hostCount = (int) Math.pow(2, (32 - n)) - 2;
    if (x1 > 0 && x1 <= 126) { // A类
        System.out.println(hostCount);
    } else if (x1 >= 128 && x1 <= 191) { // B类
        System.out.println(hostCount);
    } else if (x1 >= 192 && x1 <= 223) { // C类
        System.out.println(hostCount);
    }

    // 输出各子网IP范围
    System.out.println("各子网IP范围如下:");
    if (x1 > 0 && x1 <= 126) { // A类
        if (n <= 8) {
            for (int i = 1, sub_id = 0; i < Math.pow(2, n) - 1; i++) {
                sub_id = (int) Math.pow(2, (8 - n)) * i;
                int startIp4 = 1;
                int endIp4 = (int) (sub_id + Math.pow(2, (8 - n)) - 2);
                System.out.print("<" + i + ">" + " ");
                System.out.println(
                        x1 + "." + sub_id + ".0." + startIp4 + "---" + x1 + "." + endIp4 + ".255." + (254 - startIp4));
            }
        } else if (n > 8 && n <= 16) {
            int subnet_id = 0; // 定义子网号
            for

原文地址: https://www.cveoy.top/t/topic/fDnJ 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录