import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
/*0~255 非则不合法 子网掩码 前面全1 后面全0 否则不合法
* A类 1~126 B类 128~191 C类 192~223 D类 224~239 E类 240~255
* 类似于"0.*.*.*"和"127.*.*.*"的IP地址不属于上述的任意一类 也不属于不合法的IP地址*/
/*
* 10.70.44.68~1.1.1.5
1.0.0.1~255.0.0.0
192.168.0.2~255.255.255.0
19..0.~255.255.255.0*/
Scanner scanner = new Scanner(System.in);
String IPAndSubnetMaskStrings="";
while (scanner.hasNextLine()){
String input=scanner.nextLine().trim();
if (input.equals("")){
break;
}
IPAndSubnetMaskStrings+=input;
IPAndSubnetMaskStrings+=" ";
}
String[] IPAndSubnetMask = IPAndSubnetMaskStrings.split(" ");//将字符串分为单个的IP和子网掩码
String[] IP = new String[IPAndSubnetMask.length];//IP数组
String[] SubnetMask = new String[IPAndSubnetMask.length];//IPSubnetMask数组
//初始化需要的相关数据类型
int AIPAddress = 0, BIPAddress = 0, CIPAddress = 0, DIPAddress = 0, EIPAddress = 0;
int privateIpAddress = 0;
int numberOfIncorrectIPOrSubnetMasks = 0;
boolean flag=true;
for (int i = 0; i < IPAndSubnetMask.length; i++) {//遍历数组 将IP和子网掩码分别存储在对应的数组中
IP[i] = IPAndSubnetMask[i].split("~")[0];
SubnetMask[i] = IPAndSubnetMask[i].split("~")[1];//获取每个IP和对应的子网掩码
// System.out.println(IP[i]+"~"+SubnetMask[i]);
//判断IP合法性和类别 合法性 以.号分隔 若小于0大于255不合法 然后判断五类类别 根据第一位判断
//私有类别等于10或172.(16~31)或192.168
int _1IP = 0, _2IP = 0, _3IP = 0, _4IP = 0, _1SubnetMask = 0, _2SubnetMask = 0, _3SubnetMask = 0, _4SubnetMask = 0;
try {
_1IP = Integer.parseInt(IP[i].split("\\.")[0]);
_2IP = Integer.parseInt(IP[i].split("\\.")[1]);
_3IP = Integer.parseInt(IP[i].split("\\.")[2]);
_4IP = Integer.parseInt(IP[i].split("\\.")[3]);
_1SubnetMask = Integer.parseInt(SubnetMask[i].split("\\.")[0]);
_2SubnetMask = Integer.parseInt(SubnetMask[i].split("\\.")[1]);
_3SubnetMask = Integer.parseInt(SubnetMask[i].split("\\.")[2]);
_4SubnetMask = Integer.parseInt(SubnetMask[i].split("\\.")[3]);
} catch (Exception e) {
/*System.out.println("IP地址" + IP[i] + "不合法");*///这里捕获到 IP空串异常后 数量+1 但是
numberOfIncorrectIPOrSubnetMasks++;
flag=false;
}
if (_1IP < 0 || _1IP > 255 || _2IP < 0 || _2IP > 255 || _3IP < 0 || _3IP > 255 || _4IP < 0 || _4IP > 255) {
/*System.out.println("IP地址" + IP[i] + "不合法");*/
numberOfIncorrectIPOrSubnetMasks++;
} else if (_1IP == 0 || _1IP == 127) {
//System.out.println("不属于五类IP地址 也不属于合法IP地址");
} else {
//IP地址合法后判断子网掩码是否合法 合法后进行统计 否则舍去
if (_1SubnetMask < 0 || _1SubnetMask > 255 || _2SubnetMask < 0 || _2SubnetMask > 255
|| _3SubnetMask < 0 || _3SubnetMask > 255 || _4SubnetMask < 0 || _4SubnetMask > 255
|| (_1SubnetMask == 255 && _2SubnetMask == 255 && _3SubnetMask == 255 && _4SubnetMask == 255) ||
(_1SubnetMask == 0 && _2SubnetMask == 0 && _3SubnetMask == 0 && _4SubnetMask == 0)
|| !isValidSubnetMask(SubnetMask[i])
) {
/*System.out.println("子网掩码" + SubnetMask[i] + "不合法");*/
if (flag==false){
}else {
numberOfIncorrectIPOrSubnetMasks++;
}
} else {
if (_1IP >= 1 && _1IP <= 126) {
/*System.out.println("A类地址");*/
AIPAddress++;
if (_1IP == 10) {
/*System.out.println("私有IP地址");*/
privateIpAddress++;
}
} else if (_1IP >= 128 && _1IP <= 191) {
/*System.out.println("B类地址");*/
BIPAddress++;
if (_1IP == 172 && _2IP >= 16 && _2IP <= 31) {
/*System.out.println("私有IP地址");*/
privateIpAddress++;
}
} else if (_1IP >= 192 && _1IP <= 223) {
/*System.out.println("C类地址");*/
CIPAddress++;
if (_1IP == 192 && _2IP == 168) {
/*System.out.println("私有IP地址");*/
privateIpAddress++;
}
} else if (_1IP >= 224 && _1IP <= 239) {
/*System.out.println("D类地址");*/
DIPAddress++;
} else if (_1IP >= 240 && _1IP <= 255) {
/*System.out.println("E类地址");*/
EIPAddress++;
}
}
}
}
System.out.println(AIPAddress+" "+BIPAddress+" "+CIPAddress+" "
+DIPAddress+" "+EIPAddress+" "+numberOfIncorrectIPOrSubnetMasks+" "+privateIpAddress);
}
public static boolean isValidSubnetMask(String subnetMask) {
// 将IP地址转换为32位二进制字符串
String binary = ipToBinary(subnetMask);
if (binary == null) return false;
// 检查是否由连续的1后跟连续的0组成
boolean foundZero = false;
for (char c : binary.toCharArray()) {
if (c == '0') {
foundZero = true;
} else if (foundZero && c == '1') {
// 发现0之后又出现1,不符合要求
return false;
}
}
return true;
}
private static String ipToBinary(String ip) {
try {
String[] octets = ip.split("\\.");
if (octets.length != 4) return null;
StringBuilder binary = new StringBuilder(32);
for (String octet : octets) {
int value = Integer.parseInt(octet);
if (value < 0 || value > 255) return null;
binary.append(String.format("%8s", Integer.toBinaryString(value)).replace(' ', '0'));
}
return binary.toString();
} catch (NumberFormatException e) {
return null;
}
}
}