计算机网络-子网划分【Java版】

计算机网络-子网划分【Java版】

package com;

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
   

    // 输入流
    public static Scanner sc = new Scanner(System.in);
    // 网络地址
    public static String networkAddress;
    // 借位数
    public static int NUM;
    // 网络地址的4位数
    public static int[] intNetworkAddress = new int[4];
    // 网络类型
    public static String TYPE;
    // 判断是不是合法的网络地址格式
    public static boolean isNet1() {
   
        String regStr = "^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}$";
        return networkAddress.matches(regStr);
    }
    // 先分割网络地址,再判断是不是ABC三者之一的网络地址
    public static boolean isNet2() {
   
        if (isNet1()) {
   
            // 创建匹配器
            Matcher matcher = Pattern.compile("[0-9]+").matcher(networkAddress);
            // 循环取值
            int i = 0;
            while (matcher.find()) {
   
                //strNetworkAddress[i] = matcher.group(0);
                intNetworkAddress[i] = Integer.parseInt(matcher.group(0));
                i++;
            }
            if (intNetworkAddress[0] >= 0 && intNetworkAddress[0] <= 223) {
   
                return true;
            }
        }
        return false;
    }

    // 先求网络类型,再判断借位是否合法
    public static boolean isNet3() {
   
        if (isNet2()) {
   
            int xx;
            int numType = intNetworkAddress[0];
            if (numType <= 127) {
   
                TYPE = "A";
                xx = 8;
            } else if (numType <= 191) {
   
                TYPE = "B";
                xx = 16;
            } else {
   
                TYPE = "C";
                xx = 24;
            }
            if (xx + NUM <= 30) {
   
                return true;
            }
        }
        return false;
    }

    // 二进制  ==>  十进制数
    public static int erJinZhiToShiJinZhi(String str) {
   
        StringBuffer sb = new StringBuffer(str).reverse();
        int sum = 0;
        for (int i = 0; i < str.length(); i++) {
   
            int tmp = sb

你可能感兴趣的:(java,计算机网络,子网划分)