Java 子网划分算法实现 - 按需分配构造超网
import java.io.*;
public class Subnetting { /** * @param args */ public static void main(String[] args) throws IOException { // TODO Auto-generated method stub String ip; //用于接收要划分的IP int s; //用于接收以要划分的子网数
System.out.println('---------------划分子网---------------');
//输入要划分的IP地址:ip
System.out.println('请输入要划分的IP地址(格式为:x1.x2.x3.x4):');
System.out.print('→:');
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
ip = br.readLine();
//截取x1
int index1 = ip.indexOf('.');
String st1 = ip.substring(0, index1);
int x1 = Integer.parseInt(st1);//把String类型的st转化成int型
//截取x2
int index2 = ip.indexOf('.', index1 + 1);
String st2 = ip.substring(index1 + 1, index2);
int x2 = Integer.parseInt(st2);//把String类型的st转化成int型
//截取x3
int index3 = ip.indexOf('.', index2 + 1);
String st3 = ip.substring(index2 + 1, index3);
int x3 = Integer.parseInt(st3);//把String类型的st转化成int型
//判断IP类型
int mask = 0;
if (x1 >= 0 && x1 <= 127) {
System.out.println('本IP地址为A类地址,能划分的最大子网数为: ' + ((int) Math.pow(2, 24 - 2) - 2));
mask = 8;
} else if (x1 >= 128 && x1 <= 191) {
System.out.println('本IP地址为B类地址,能划分的最大子网数为: ' + ((int) Math.pow(2, 16 - 2) - 2));
mask = 16;
} else if (x1 >= 192 && x1 <= 223) {
System.out.println('本IP地址为C类地址,能划分的最大子网数为: ' + ((int) Math.pow(2, 8 - 2) - 2));
mask = 24;
} else {
System.out.println('该IP地址不是A、B、C类地址!');
System.exit(0);
}
//输入要划分的子网数数:s
System.out.println('请输入要划分的子网数:');
System.out.print('→:');
String subnet_num = br.readLine();
s = Integer.parseInt(subnet_num);
br.close();
//求所输入子网数对应的二进制位数:n
int n = (int) Math.ceil(Math.log(s + 2) / Math.log(2));
//求子网掩码
int sum = 0;
for (int i = 0; i < n; i++) {
sum = sum + (int) Math.pow(2, (7 - i));
}
System.out.println('子网掩码为:' + getMask(mask, n));
//求每网段主机数
System.out.println('每网段主机数:' + (int) Math.pow(2, (32 - mask - n)) + '个');
//输出各子网IP范围
System.out.println('各子网IP范围如下:');
int subnet_id = 0;
for (int i = 0; i < s; i++) {
subnet_id++;
int sub_id = subnet_id << (32 - mask - n);
System.out.print('<' + subnet_id + '> ');
System.out.println(getIP(x1, x2, x3, sub_id) + '---' + getIP(x1, x2, x3, sub_id + (int) Math.pow(2, (32 - mask - n)) - 1));
}
System.out.println('子网划分完毕');
}
//获取子网掩码
public static String getMask(int mask, int n) {
int sum = 0;
for (int i = 0; i < n; i++) {
sum = sum + (int) Math.pow(2, (7 - i));
}
String[] maskArray = {'0', '128', '192', '224', '240', '248', '252', '254', '255'};
int index = sum / 8;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < index; i++) {
sb.append(maskArray[8]);
sb.append('.');
}
sb.append(maskArray[sum % 8]);
for (int i = index + 1; i < 4; i++) {
sb.append('.');
sb.append(maskArray[0]);
}
return sb.toString();
}
//获取IP地址
public static String getIP(int x1, int x2, int x3, int sub_id) {
int y1 = (sub_id & 0xFF000000) >>> 24;
int y2 = (sub_id & 0x00FF0000) >>> 16;
int y3 = (sub_id & 0x0000FF00) >>> 8;
int y4 = (sub_id & 0x000000FF);
return x1 + '.' + x2 + '.' + x3 + '.' + y4;
}
}
原文地址: https://www.cveoy.top/t/topic/ocrU 著作权归作者所有。请勿转载和采集!