package cn.ifiv5.IP;
import java.io.*;
/**
*
* @author Arno
*/
public class Main {
*
* @author Arno
*/
public class Main {
public static void main(String[] args) {
new Main().go();
}
/**
* 程序主要逻辑
*/
public void go() {
String IP = null;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try{
IP = br.readLine();
}catch(IOException ex){
ex.printStackTrace();
}
IP = IP.replace(" ", "");
new Main().go();
}
/**
* 程序主要逻辑
*/
public void go() {
String IP = null;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try{
IP = br.readLine();
}catch(IOException ex){
ex.printStackTrace();
}
IP = IP.replace(" ", "");
String []temp = IP.split("/");
String []sIP = temp[0].split("\\.");
/**
* 判断输入格式是否正确
*/
if(temp.length < 2)
{
System.out.println("对不起,输入格式错误");
System.exit(0);
}
int Mask = Integer.parseInt(temp[1]);
int []ip = new int[4];
for(int i=0;i<sIP.length;i++)
{
ip[i] = Integer.parseInt(sIP[i]);
}
/**
* IP地址各段不能大于255
*/
for(int i=0; i < 4; i++)
{
if(ip[i] > 255)
{
System.out.println("对不起,IP输入错误");
System.exit(0);
}
}
/**
* 判断IP地址所属分类
*/
int ipclass = 0;
if(ip[0] < 127){
ipclass = 1;
}else if(ip[0] < 192){
ipclass = 2;
}else if(ip[0] < 224){
ipclass = 3;
}
/**
* 判断子网掩码是否输入正确
*/
if(Mask < 8 * ipclass)
{
System.out.println("对不起,子网掩码输入错误");
System.exit(0);
}
int b=0;
int a = Mask%8;
b=Mask/8;
StringBuffer sb = new StringBuffer();
for(int i=0;i<a;i++)
sb.append('1');
for(int i=0;i<8-a;i++)
sb.append('0');
int end =Integer.parseInt(sb.toString(),2);
String []sIP = temp[0].split("\\.");
/**
* 判断输入格式是否正确
*/
if(temp.length < 2)
{
System.out.println("对不起,输入格式错误");
System.exit(0);
}
int Mask = Integer.parseInt(temp[1]);
int []ip = new int[4];
for(int i=0;i<sIP.length;i++)
{
ip[i] = Integer.parseInt(sIP[i]);
}
/**
* IP地址各段不能大于255
*/
for(int i=0; i < 4; i++)
{
if(ip[i] > 255)
{
System.out.println("对不起,IP输入错误");
System.exit(0);
}
}
/**
* 判断IP地址所属分类
*/
int ipclass = 0;
if(ip[0] < 127){
ipclass = 1;
}else if(ip[0] < 192){
ipclass = 2;
}else if(ip[0] < 224){
ipclass = 3;
}
/**
* 判断子网掩码是否输入正确
*/
if(Mask < 8 * ipclass)
{
System.out.println("对不起,子网掩码输入错误");
System.exit(0);
}
int b=0;
int a = Mask%8;
b=Mask/8;
StringBuffer sb = new StringBuffer();
for(int i=0;i<a;i++)
sb.append('1');
for(int i=0;i<8-a;i++)
sb.append('0');
int end =Integer.parseInt(sb.toString(),2);
ip[b] = ip[b]&end;
for(int i=b+1;i<4;i++)
ip[i] = 0;
for(int i=0;i<4;i++)
{
System.out.print(ip[i]);
if(i!=3)
System.out.print(".");
}
}
}
ip[i] = 0;
for(int i=0;i<4;i++)
{
System.out.print(ip[i]);
if(i!=3)
System.out.print(".");
}
}
}
转载于:https://blog.51cto.com/fanghao/205322