/**
* 划分子网
*/
//
import java.io.*;
public class subnetting{
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("→:");
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型
//截取x4
//A类
if(x1>0&&x1<=126){
System.out.println("本IP地址能划分划分的最大子网数为: "+((int)Math.pow(2,24-2)-2));
}
//B类
if(x1>=128&&x1<=191){
System.out.println("本IP地址能划分划分的最大子网数为: "+((int)Math.pow(2,16)-2));
}
//C类
if(x1>=192&&x1<=223){
System.out.println("本IP地址能划分划分的最大子网数为: "+((int)Math.pow(2,8-2)-2));
}
//输入要划分的子网数数:s
System.out.println("请输入要划分的子网数:");
System.out.print("→:");
String subnet_num= br.readLine();
s = Integer.parseInt(subnet_num);
br.close();
//求所输入子网数对应的二进制位数:n
for( n=1 ;(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("子网掩码为:");
//A类
if(x1>0&&x1<=126){
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);
}
}
//B类
if(x1>=128&&x1<=191){
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);
}
}
//C类
if(x1>=192&&x1<=223){
System.out.println("255.255.255."+sum);
}
//求每网段主机数
System.out.println("每网段主机数:");
//A类
if(x1>0&&x1<=126){
System.out.println(Math.pow(2,(32-n))-2);
}
//B类
if(x1>=128&&x1<=191){
System.out.println(Math.pow(2,(16-n))-2);
}
//C类
if(x1>=192&&x1<=223){
System.out.println(Math.pow(2,(8-n))-2);
}
//输出各子网IP范围
System.out.println("各子网IP范围如下:");
//A类
if(x1>0&&x1<=126){
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;
System.out.print("<"+i+">"+" ");
System.out.println(x1+"."+sub_id+".0."+1+
"---"+x1+"."+(int)(sub_id+Math.pow(2,(8-n))-1)+"."+255.254);
}
}
if(n>8&&n<=16){
int subnet_id =0;//定义子网号
for (int i=0; i<Math.pow(2,8);i++ ){
if(i==0){
for (int k=1,sub_id=0;k<=Math.pow(2,n-8)-1;k++){
sub_id = (int)Math.pow(2,(16-n))*k;
System.out.print("<"+(++subnet_id)+">"+" ");
System.out.println(x1+"."+i+"."+sub_id+"."+1+"------"+x1+"."+i+"."+(sub_id+(int)Math.pow(2,(16-n)))+"."+254);
}
}else{
for (int k=0,sub_id=0;k<=Math.pow(2,n-8)-1;k++){
if((i==255)&&k==(Math.pow(2,n-8)-1)){break;}
sub_id = (int)Math.pow(2,(16-n))*k;
System.out.print("<"+(++subnet_id)+">"+" ");
System.out.println(x1+"."+i+"."+(sub_id)+"."+1+"------"+x1+i+"."+(sub_id+(int)Math.pow(2,(16-n)))+"."+254);
}
}
}
}
if(n>16&&n<24){
int subnet_id =0;//定义子网号
for(int m=0;m<Math.pow(2,8);m++){
if(m==0){
for (int i=0; i<Math.pow(2,8);i++ ){
if(i==0){
for (int k=1,sub_id=0;k<=Math.pow(2,n-16)-1;k++){
sub_id = (int)Math.pow(2,(24-n))*k;
System.out.print("<"+(++subnet_id)+">"+" ");
System.out.println(x1+"."+m+"."+i+"."+(sub_id+1)+"------"+x1+"."+m+"."+i+"."+(sub_id+(int)Math.pow(2,(24-n))-2));
}
}else{
for (int k=0,sub_id=0;k<=Math.pow(2,n-16)-1;k++){
sub_id = (int)Math.pow(2,(24-n))*k;
System.out.print("<"+(++subnet_id)+">"+" ");
System.out.println(x1+"."+m+"."+i+"."+(sub_id+1)+"------"+x1+"."+m+"."+i+"."+(sub_id+(int)Math.pow(2,(24-n))-2));
}
}
}
}else{
for (int i=0; i<Math.pow(2,8);i++ ){
for (int k=0,sub_id=0;k<=Math.pow(2,n-16)-1;k++){
if(m==255&&(i==255)&&k==(Math.pow(2,n-16)-1)){break;}
sub_id = (int)Math.pow(2,(24-n))*k;
System.out.print("<"+(++subnet_id)+">"+" ");
System.out.println(x1+"."+m+"."+i+"."+(sub_id+1)+"------"+x1+"."+m+"."+i+"."+(sub_id+(int)Math.pow(2,(24-n))-2));
}
}
}
}
}
}
//B类
if(x1>=128&&x1<=191){
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;
System.out.print("<"+i+">"+" ");
System.out.println(x1+"."+x2+"."+sub_id+"."+1+"---"+x1+"."+x2+"."+(int)(sub_id+Math.pow(2,(8-n))-1)+"."+254);
}
}
if(n>8&&n<16){
int subnet_id =0;//定义子网号
for (int i=0; i<Math.pow(2,8);i++ ){
if(i==0){
for (int k=1,sub_id=0;k<=Math.pow(2,n-8)-1;k++){
sub_id = (int)Math.pow(2,(16-n))*k;
System.out.print("<"+(++subnet_id)+">"+" ");
System.out.println(x1+"."+x2+"."+i+"."+(sub_id+1)+"------"+x1+"."+x2+"."+i+"."+(sub_id+(int)Math.pow(2,(16-n))-2));
}
}else{
for (int k=0,sub_id=0;k<=Math.pow(2,n-8)-1;k++){
if((i==255)&&k==(Math.pow(2,n-8)-1)){break;}
sub_id = (int)Math.pow(2,(16-n))*k;
System.out.print("<"+(++subnet_id)+">"+" ");
System.out.println(x1+"."+x2+"."+i+"."+(sub_id+1)+"------"+x1+"."+x2+"."+i+"."+(sub_id+(int)Math.pow(2,(16-n))-2));
}
}
}
}
}
//C类
if(x1>=192&&x1<=223){
for (int i=1,sub_id=0; i<Math.pow(2,n)-1;i++ ){
sub_id= (int)Math.pow(2,(8-n))*i;
System.out.print("<"+i+">"+" ");
System.out.println(x1+"."+x2+"."+x3+"."+(sub_id+1)+"---"+x1+"."+x2+"."+x3+"."+((int)sub_id+Math.pow(2,(8-n))-2));
}
}
System.out.println("☆☆--子--网--☆--划---分--☆--完--毕--☆☆");
}
}