-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathString_10.java
More file actions
183 lines (162 loc) · 3.9 KB
/
Copy pathString_10.java
File metadata and controls
183 lines (162 loc) · 3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package HuaweiCoding;
import java.util.Scanner;
/*
*
*/
public class String_10 {
public static int A=0;
public static int B=0;
public static int C=0;
public static int D=0;
public static int E=0;
public static int wrongIp=0;
public static int wrongMask=0;
public static int privateIp=0;
public static boolean privateIpFlag;
public static void toBinary(String mask,StringBuilder resultMask) {
if("0".equals(mask)) {
resultMask.append("00000000");
}
int parseInt = Integer.parseInt(mask);
while(parseInt!=0) {
resultMask.append(parseInt%2);
parseInt=parseInt/2;
}
}
public static void netMask(StringBuilder resultMask) {
String stringMask = resultMask.toString();
int index=0;
for(int i=0;i<stringMask.length();i++) {
if(stringMask.charAt(i)=='0') {
index=i;
break;
}
}
for(int j=index;j<stringMask.length();j++) {
if(stringMask.charAt(j)=='1') {
wrongMask+=1;
break;
}
}
}
public static boolean ipPrivate(String ip) {
String[] inputIp = ip.split("\\.");
if(Integer.parseInt(inputIp[0])==10) {
for(int i=1;i<inputIp.length;i++) {
int num=Integer.parseInt(inputIp[i]);
if(num<0 || num>255) {
privateIpFlag=false;
return false;
}
}
privateIpFlag=true;
privateIp+=1;
return true;
}
if(Integer.parseInt(inputIp[0])==172) {
int secondIp = Integer.parseInt(inputIp[0]);
if(secondIp<16 || secondIp>31) {
privateIpFlag=false;
return false;
}
for(int i=2;i<inputIp.length;i++) {
int num=Integer.parseInt(inputIp[i]);
if(num<0 || num>255) {
privateIpFlag=false;
return false;
}
}
privateIpFlag=true;
privateIp+=1;
return true;
}
if(Integer.parseInt(inputIp[0])==192) {
int secondIp = Integer.parseInt(inputIp[0]);
if(secondIp!=168) {
privateIpFlag=false;
return false;
}
for(int i=2;i<inputIp.length;i++) {
int num=Integer.parseInt(inputIp[i]);
if(num<0 || num>255) {
privateIpFlag=false;
return false;
}
}
privateIpFlag=true;
privateIp+=1;
return true;
}
privateIpFlag=false;
return false;
}
public static boolean ipAddress(String ip) {
String[] inputIp = ip.split("\\.");
for (String strIp : inputIp) {
if("".equals(strIp)) {
wrongIp+=1;
return false;
}
int parseInt = Integer.parseInt(strIp);
if(parseInt<0 || parseInt>255) {
wrongIp+=1;
return false;
}
}
return true;
}
public static void ipRange(String ip) {
String[] inputIp = ip.split("\\.");
int firstIp = Integer.parseInt(inputIp[0]);
if(!privateIpFlag) {
if(firstIp>=1 && firstIp<=126) {
A+=1;
}
if(firstIp>=128 && firstIp<=191) {
B+=1;
}
if(firstIp>=192 && firstIp<=223) {
C+=1;
}
if(firstIp>=224 && firstIp<=239) {
D+=1;
}
if(firstIp>=240 && firstIp<=255) {
E+=1;
}
}
}
public static void ipAddressAndnetMask(String ip,String netmask) {
//计算mask
String[] inputMask = netmask.split("\\.");
StringBuilder resultMask=new StringBuilder();
for (String mask : inputMask) {
toBinary(mask,resultMask);
}
netMask(resultMask);
//计算Ip是否合法
boolean resultIp = ipAddress(ip);
privateIpFlag=false;
//检查私有IP
if(resultIp) {
ipPrivate(ip);
}
//这里不对
if(resultIp && privateIpFlag==false) {
ipRange(ip);
}
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
while(sc.hasNextLine()) {
String inputLineTemp = sc.nextLine();
if("".equals(inputLineTemp)) {
break;
}
String[] inputLine=inputLineTemp.split("~");
ipAddressAndnetMask(inputLine[0], inputLine[1]);
}
sc.close();
System.out.println(A+" "+B+" "+C+" "+D+" "+E+" "+(wrongIp+wrongMask)+" "+privateIp);
}
}