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();

    //将IP地址转化为二进制字符串
    String[] ipArray = ip.split("\\.");
    StringBuilder ipBinary = new StringBuilder();
    for (String s1 : ipArray) {
        String binary = Integer.toBinaryString(Integer.parseInt(s1));
        while (binary.length() < 8) {
            binary = "0" + binary;
        }
        ipBinary.append(binary);
    }

    //获取IP地址的类别
    int classType = Integer.parseInt(ipArray[0]);

    //输出子网掩码和每个子网的IP范围
    System.out.println("--------------------------------------");
    //A类
    if (classType >= 1 && classType <= 126) {
        System.out.println("本IP地址能划分划分的最大子网数为: " + ((int) Math.pow(2, 24 - 2) - 2));
        //输入要划分的子网数:s
        System.out.println("请输入要划分的子网数:");
        System.out.print("→:");
        s = Integer.parseInt(br.readLine());

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

        System.out.println("子网掩码为:255." + getMaskBinary(n) + ".0.0");

        int subnetNum = (int) Math.pow(2, n) - 2;
        int hostNum = (int) Math.pow(2, 24 - n) - 2;
        String[] subnetIdBinary = getSubnetIdBinary(ipBinary.toString(), n, subnetNum);

        for (int i = 0; i < subnetIdBinary.length; i++) {
            String startIpBinary = subnetIdBinary[i];
            String endIpBinary = getEndIpBinary(startIpBinary, hostNum);
            System.out.println("<" + (i + 1) + "> " + getIpString(startIpBinary) + " - " + getIpString(endIpBinary));
        }
    }
    //B类
    else if (classType >= 128 && classType <= 191) {
        System.out.println("本IP地址能划分划分的最大子网数为: " + ((int) Math.pow(2, 16 - 2) - 2));
        //输入要划分的子网数:s
        System.out.println("请输入要划分的子网数:");
        System.out.print("→:");
        s = Integer.parseInt(br.readLine());

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

        System.out.println("子网掩码为:255.255." + getMaskBinary(n) + ".0");

        int subnetNum = (int) Math.pow(2, n) - 2;
        int hostNum = (int) Math.pow(2, 16 - n) - 2;
        String[] subnetIdBinary = getSubnetIdBinary(ipBinary.toString(), n, subnetNum);

        for (int i = 0; i < subnetIdBinary.length; i++) {
            String startIpBinary = subnetIdBinary[i];
            String endIpBinary = getEndIpBinary(startIpBinary, hostNum);
            System.out.println("<" + (i + 1) + "> " + getIpString(startIpBinary) + " - " + getIpString(endIpBinary));
        }
    }
    //C类
    else if (classType >= 192 && classType <= 223) {
        System.out.println("本IP地址能划分划分的最大子网数为: " + ((int) Math.pow(2, 8 - 2) - 2));
        //输入要划分的子网数:s
        System.out.println("请输入要划分的子网数:");
        System.out.print("→:");
        s = Integer.parseInt(br.readLine());

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

        System.out.println("子网掩码为:255.255.255." + getMaskBinary(n));

        int subnetNum = (int) Math.pow(2, n) - 2;
        int hostNum = (int) Math.pow(2, 8 - n) - 2;
        String[] subnetIdBinary = getSubnetIdBinary(ipBinary.toString(), n, subnetNum);

        for (int i = 0; i < subnetIdBinary.length; i++) {
            String startIpBinary = subnetIdBinary[i];
            String endIpBinary = getEndIpBinary(startIpBinary, hostNum);
            System.out.println("<" + (i + 1) + "> " + getIpString(startIpBinary) + " - " + getIpString(endIpBinary));
        }
    }
    System.out.println("子网划分完毕");
    br.close();
}

/**
 * 获取二进制子网掩码
 *
 * @param n 子网数对应的二进制位数
 * @return 二进制子网掩码
 */
public static String getMaskBinary(int n) {
    StringBuilder maskBinary = new StringBuilder();
    for (int i = 0
修改下列代码使其可以按需分配构造超网:import javaio; public class ffdg 		 param args	 	public static void mainString argsthrows IOException 		 TODO Auto-generated method stub		String ip; 用于接收要划分的IP int s;

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

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