private static void WriteInput(List <VLSM.Branch> listBranches, VLSM.IPv4 rootIP) { Guides = ""; Guides += $"Root IP: {rootIP.ToDecimalString()}/{rootIP.Suffix}{Environment.NewLine}"; Guides += $"Branch"; Guides += branchesAmount <= 1 ? $": {Environment.NewLine}" :$"es (sorted descendant by hosts amount): {Environment.NewLine}"; foreach (Branch brch in listBranches) { Guides += $"{brch.Name}:{brch.HostsAmount}{Environment.NewLine}"; } Guides += Environment.NewLine; Guides += Environment.NewLine; Guides += $"Using the \"AND BIT\" operator foreach bit couple of the subnet mask & root IP:{Environment.NewLine}"; Guides += $"Subnet mask: {rootIP.SubnetMask.ToBinaryString()}{Environment.NewLine}"; Guides += $@" Root IP: {rootIP.ToBinaryString()}{Environment.NewLine}"; Guides += $@" Net address: {rootIP.NetAddress.ToBinaryString()} ({rootIP.NetAddress.ToDecimalString()}/{rootIP.Suffix}){Environment.NewLine}"; }
private static void WriteNetAddressFormat(VLSM.IPv4 ip, VLSM_Units vlsm_units) { Guides += $"{Environment.NewLine}"; Guides += $@" Subnet format: "; List <int> listBits = ip.ToBinaryList(); int startOctet = (32 - vlsm_units.BeforeHosts) / 8; int endOctet = (32 - vlsm_units.AfterHosts - 1) / 8; int startIndex = (32 - vlsm_units.BeforeHosts); int endIndex = (32 - vlsm_units.AfterHosts - 1); for (int i = 0; i < listBits.Count; ++i) { int currentOctetIndex = i / 8; if (currentOctetIndex >= startOctet && currentOctetIndex <= endOctet) { if (i >= startIndex && i <= endIndex) { Guides += 'x'; } else { Guides += $"{listBits[i]}"; } } else { if ((i + 1) % 8 == 0) { Guides += $"{Octet.ToDecimal(listBits.GetRange(i - 7, 8))}"; } } if ((i + 1) % 8 == 0) { if (i != 31) { Guides += '.'; } } } Guides += $"/{ip.Suffix + vlsm_units.BorrowedBitsAmount}"; }
private static void WriteHostFormat(VLSM.IPv4 ip, VLSM_Units vlsm_units) { Guides += $"{Environment.NewLine}"; Guides += $@" Host format: "; List <int> listBits = ip.ToBinaryList(); int hostOctetIndex = (32 - vlsm_units.AfterHosts) / 8; for (int i = 0; i < 32; ++i) { if (i / 8 >= hostOctetIndex) { if (i >= 32 - vlsm_units.AfterHosts) { Guides += 'x'; } else { Guides += listBits[i]; } } else { if (((i + 1) % 8) == 0) { Guides += Octet.ToDecimal(listBits.GetRange(i - 7, 8)); } } if (((i + 1) % 8) == 0) { if (i != 31) { Guides += "."; } } } Guides += $"/{ip.Suffix}"; }
public static List <VLSM__RESULT> GetQuickResult(List <VLSM.Branch> listBranches, VLSM.IPv4 rootIP) { branchesAmount = listBranches.Count; WriteInput(listBranches, rootIP); // Sắp xếp danh sách các chi nhánh giảm dần theo số Hosts Branch.SortDescendantByHostsAmount(listBranches); // List các mạng con được sinh ra List <IPv4> listSubnets = new List <VLSM.IPv4>(); listSubnets.Add(rootIP); // List kết quả List <VLSM__RESULT> listResult = new List <VLSM__RESULT>(); // Duyệt từng chi nhánh từ đầu đến cuối (host giảm dần) foreach (VLSM.Branch branch in listBranches) { Guides += $@"{Environment.NewLine}{Environment.NewLine}{Environment.NewLine} ————————————————————————————————————————————————————————————{Environment.NewLine}"; Guides += $@">>> {branch.Name} ({branch.HostsAmount}){Environment.NewLine}{Environment.NewLine}"; // Thêm một item kết quả VLSM__RESULT item = GetResult(listSubnets, branch); if (item == null) { return(null); } listResult.Add(item); } return(listResult); }