用C语言设计与实现 子网划分工具 1、设计要求 (1)编程实现一个子网划分的简单工具。 (2)能够根据用户输入的网络地址和子网掩码,判断出用户输入的合法性及网络的类别(A、B、C地址)。 (3)能够计算出下列参数:子网数及有效子网、每个子网的主机数、每个子网IP范围及广播地址。 (4)应有相应的帮助提示及良好的结果输出组织,易于用户使用和查看结果。
时间: 2024-03-05 14:51:30 浏览: 82
以下是一个使用C语言实现的子网划分工具的示例代码,实现了上述要求:
```c
#include <stdio.h>
#include <string.h>
// 判断IP地址是否合法
int is_valid_ip(char *ip) {
int a, b, c, d;
if (sscanf(ip, "%d.%d.%d.%d", &a, &b, &c, &d) != 4) {
return 0;
}
if (a >= 0 && a <= 255 && b >= 0 && b <= 255 && c >= 0 && c <= 255 && d >= 0 && d <= 255) {
return 1;
}
return 0;
}
// 判断子网掩码是否合法
int is_valid_subnet_mask(char *mask) {
int a, b, c, d;
if (sscanf(mask, "%d.%d.%d.%d", &a, &b, &c, &d) != 4) {
return 0;
}
int ip = (a << 24) | (b << 16) | (c << 8) | d;
int mask_int = ~ip + 1;
int mask_bits = 0;
while (mask_int) {
mask_bits++;
mask_int = (mask_int & (mask_int - 1));
}
if (mask_bits == 0 || mask_bits > 30) {
return 0;
}
return 1;
}
// 根据IP地址和子网掩码计算网络地址
int get_network_address(char *ip, char *mask) {
int a1, a2, a3, a4;
int b1, b2, b3, b4;
sscanf(ip, "%d.%d.%d.%d", &a1, &a2, &a3, &a4);
sscanf(mask, "%d.%d.%d.%d", &b1, &b2, &b3, &b4);
int network_address = (a1 & b1) << 24 | (a2 & b2) << 16 | (a3 & b3) << 8 | (a4 & b4);
return network_address;
}
// 根据子网掩码计算子网数
int get_subnet_count(char *mask) {
int a, b, c, d;
sscanf(mask, "%d.%d.%d.%d", &a, &b, &c, &d);
int mask_int = (a << 24) | (b << 16) | (c << 8) | d;
int mask_bits = 0;
while (mask_int) {
mask_bits++;
mask_int = (mask_int & (mask_int - 1));
}
int subnet_count = 1;
for (int i = 0; i < 32 - mask_bits; i++) {
subnet_count *= 2;
}
return subnet_count;
}
// 计算每个子网的主机数
int get_host_count(char *mask) {
int a, b, c, d;
sscanf(mask, "%d.%d.%d.%d", &a, &b, &c, &d);
int mask_int = (a << 24) | (b << 16) | (c << 8) | d;
int mask_bits = 0;
while (mask_int) {
mask_bits++;
mask_int = (mask_int & (mask_int - 1));
}
int host_count = 1;
for (int i = 0; i < 32 - mask_bits - 2; i++) {
host_count *= 2;
}
return host_count;
}
// 根据子网掩码计算每个子网的IP范围和广播地址
void get_subnet_range(char *mask, int *subnet_ranges, int *broadcast_addresses) {
int a, b, c, d;
sscanf(mask, "%d.%d.%d.%d", &a, &b, &c, &d);
int mask_int = (a << 24) | (b << 16) | (c << 8) | d;
int mask_bits = 0;
while (mask_int) {
mask_bits++;
mask_int = (mask_int & (mask_int - 1));
}
int host_bits = 32 - mask_bits - 2;
int subnet_count = 1;
for (int i = 0; i < host_bits; i++) {
subnet_count *= 2;
}
int ip_range = (1 << host_bits) - 1;
int subnet_mask = (1 << host_bits) - 1;
for (int i = 0; i < subnet_count; i++) {
subnet_ranges[i] = (i << host_bits) | 1;
broadcast_addresses[i] = ((i + 1) << host_bits) - 2;
}
}
int main() {
char ip[20], mask[20];
printf("请输入IP地址和子网掩码,用空格分隔:\n");
scanf("%s %s", ip, mask);
if (!is_valid_ip(ip)) {
printf("输入的IP地址不合法\n");
return 0;
}
if (!is_valid_subnet_mask(mask)) {
printf("输入的子网掩码不合法\n");
return 0;
}
int network_address = get_network_address(ip, mask);
int subnet_count = get_subnet_count(mask);
int host_count = get_host_count(mask);
int subnet_ranges[subnet_count];
int broadcast_addresses[subnet_count];
get_subnet_range(mask, subnet_ranges, broadcast_addresses);
printf("IP地址:%s\n", ip);
printf("子网掩码:%s\n", mask);
printf("网络地址:");
printf("%d.%d.%d.%d\n", (network_address >> 24) & 0xFF, (network_address >> 16) & 0xFF, (network_address >> 8) & 0xFF, network_address & 0xFF);
printf("子网数:%d\n", subnet_count);
printf("每个子网的主机数:%d\n", host_count);
printf("每个子网IP范围和广播地址:\n");
for (int i = 0; i < subnet_count; i++) {
printf("%d.%d.%d.%d ~ %d.%d.%d.%d,广播地址:%d.%d.%d.%d\n",
(subnet_ranges[i] >> 24) & 0xFF, (subnet_ranges[i] >> 16) & 0xFF, (subnet_ranges[i] >> 8) & 0xFF, subnet_ranges[i] & 0xFF,
((subnet_ranges[i] >> 24) & 0xFF) | 0xFF, ((subnet_ranges[i] >> 16) & 0xFF) | 0xFF, ((subnet_ranges[i] >> 8) & 0xFF) | 0xFF, (subnet_ranges[i] & 0xFF) | (1 << (32 - mask_bits - 1)) - 1,
(broadcast_addresses[i] >> 24) & 0xFF, (broadcast_addresses[i] >> 16) & 0xFF, (broadcast_addresses[i] >> 8) & 0xFF, broadcast_addresses[i] & 0xFF);
}
return 0;
}
```
这个程序可以根据用户输入的IP地址和子网掩码,计算出网络地址、子网数、每个子网的主机数、每个子网的IP范围和广播地址,并输出结果。用户可以根据输出结果进行相应的操作和判断。
阅读全文