识别有效的IP地址和掩码并进行分类统计

识别有效的IP地址和掩码并进行分类统计

题目

请解析IP地址和对应的掩码,进行分类识别。要求按照A/B/C/D/E类地址归类,不合法的地址和掩码单独归类。

所有的IP地址划分为 A,B,C,D,E五类

A类地址1.0.0.0~126.255.255.255;

B类地址128.0.0.0~191.255.255.255;

C类地址192.0.0.0~223.255.255.255;

D类地址224.0.0.0~239.255.255.255;

E类地址240.0.0.0~255.255.255.255

私网IP范围是:

10.0.0.0~10.255.255.255

172.16.0.0~172.31.255.255

192.168.0.0~192.168.255.255

子网掩码为二进制下前面是连续的1,然后全是0。(例如:255.255.255.32就是一个非法的掩码)

注意二进制下全是1或者全是0均为非法

注意:

\1. 类似于【0...*】的IP地址不属于上述输入的任意一类,也不属于不合法ip地址,计数时可以忽略

\2. 私有IP地址和A,B,C,D,E类地址是不冲突的?题目出处

思路

根据条件详细判断。

判断前面全1位的时候,循环比枚举好。

代码

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
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#pragma warning(disable : 4996)

typedef struct club
{
int ip[4]; //ip位
int cp[4]; //掩码
int type; //类型
int flag; //是否无效
int pro; //是否私有
}IP;
int dig(int i) //判断是否为前面全1的数
{
if ((i == 255) || (i == 254) || (i == 252) || (i == 248) || (i == 240) || (i == 224) || (i == 192) || (i == 128) || (i == 0))
return 0;
else return 1;
}
int main(void)
{
IP test[1000];
int i = 0, j, lenth, end;
while ((lenth = scanf("%d.%d.%d.%d~%d.%d.%d.%d", &test[i].ip[0], &test[i].ip[1], &test[i].ip[2], &test[i].ip[3], &test[i].cp[0], &test[i].cp[1], &test[i].cp[2], &test[i].cp[3])) != EOF)
{
test[i].flag = 1;
test[i].pro = 0;
if (lenth != 8) //判断是否为8个数字
{
test[i].flag = 0;
i++;
continue;

}
for (j = 0; j < 4; j++) //判断数字是否在255以内
{
if (test[i].ip[j] > 255 || test[i].cp[j] > 255)
test[i].flag = 0;
}
if ((test[i].cp[0] == 255) && (test[i].cp[1] == 255) && (test[i].cp[2] == 255) && (test[i].cp[3] == 255)) //全1
{
test[i].flag = 0;
i++;
continue;

}
if ((test[i].cp[0] == 0) && (test[i].cp[1] == 0) && (test[i].cp[2] == 0) && (test[i].cp[3] == 0)) //全0
{
test[i].flag = 0;
i++;
continue;
}
j = 0;
while (test[i].cp[j] == 255) //找到不是全1的掩码位
j++;
if (dig(test[i].cp[j])) //判断
{
test[i].flag = 0;
i++;
continue;
}
for (j++; j < 4; j++) //后面是否全为0
if (test[i].cp[j] != 0)
{
test[i].flag = 0;

};
if (test[i].flag == 0) //非法则退出
{
i++;
continue;
}//下面为笨拙的判断条件
/*if ((test[i].cp[0] == 255) && (test[i].cp[1] == 255) && (test[i].cp[2] == 255))
if (dig(test[i].cp[3]))
{
test[i].flag = 0;
i++;
continue;
}
if ((test[i].cp[0] == 255) && (test[i].cp[1] == 255) && (test[i].cp[3] == 0))
if (dig(test[i].cp[2]))
{
test[i].flag = 0;
i++;
continue;
}
if ((test[i].cp[0] == 255) && (test[i].cp[3] == 0) && (test[i].cp[2] == 0))
if (dig(test[i].cp[1]))
{
test[i].flag = 0;
i++;
continue;
}
if ((test[i].cp[3] == 0) && (test[i].cp[1] == 0) && (test[i].cp[2] == 0))
if (dig(test[i].cp[0]))
{
test[i].flag = 0;
i++;
continue;
}*/
if (test[i].flag == 1) //判断分类以及是否为私网
{
if (test[i].ip[0] > 0) {
if (test[i].ip[0] <= 126)
{
test[i].type = 1;

if (test[i].ip[0] == 10)
test[i].pro = 1;
}
else if (test[i].ip[0] < 128);
else if (test[i].ip[0] < 192)
{
test[i].type = 2;
if ((test[i].ip[0] == 172) && (test[i].ip[1] > 16) && (test[i].ip[1] < 32))
test[i].pro = 1;
}
else if (test[i].ip[0] < 224)
{
test[i].type = 3;
if ((test[i].ip[0] == 192) && (test[i].ip[1] == 168))
test[i].pro = 1;
}
else if (test[i].ip[0] < 240)
test[i].type = 4;
else if (test[i].ip[0] < 256)
test[i].type = 5;
}
}
i++;
}end = i;
int a = 0, b = 0, c = 0, d = 0, e = 0, f = 0, error = 0;
for (i = 0; i < end; i++) //统计数目
{
if (test[i].flag == 1)
{
if (test[i].type == 1)
a++;
if (test[i].type == 2)
b++;
if (test[i].type == 3)
c++;
if (test[i].type == 4)
d++;
if (test[i].type == 5)
e++;

if (test[i].pro == 1)
f++;
}
else error++;
}
printf("%d %d %d %d %d %d %d", a, b, c, d, e, error, f);
return 0;
}

反思

枚举使代码过于复杂,工作量大,善用循环解决问题。

打赏
  • 版权声明: 本博客所有文章除特别声明外,均采用 Apache License 2.0 许可协议。转载请注明出处!
  • © 2020 lsengard
  • PV: UV:

请我喝杯咖啡吧~

支付宝
微信