//所有的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
//输入:
//多行字符串。每行一个IP地址和掩码,已~隔开。如:
//10.70.44.68~255.254.255.0
//1.0.0.1~255.0.0.0
//192.168.0.2~255.255.255.0
//19..0.~255.255.255.0
//输出:
//统计A、B、C、D、E、错误IP地址或错误掩码、私有IP的个数,之间以空格隔开,根据上面的IP,可以得到:
//1.0.0.1~255.0.0.0 ----A类
//192.168.0.2~255.255.255.0 ----C类,私有
//10.70.44.68~255.254.255.0----错误的掩码
//19..0.~255.255.255.0-----错误的IP
//可以得到统计数据如下:
//1 0 1 0 0 2 1
//样例输入:
//10.70.44.68~255.254.255.0
//1.0.0.1~255.0.0.0
//192.168.0.2~255.255.255.0
//19..0.~255.255.255.0
//样例输出:
//1 0 1 0 0 2 1
#include <stdio.h> #include <stdlib.h> #include <string.h> int main(void){ int A=0,B=0,C=0,D=0,E=0,err_ip_mask=0,private_ip=0; int ip[4], netmask[4], myip[8]; int input_len, k, flag, one, one1; unsigned int temp, temp1; char input[100]; while(gets(input)){ flag = 0; memset(ip, -1, sizeof(int)*4); memset(netmask, -1, sizeof(int)*4); if(sscanf(input, "%d.%d.%d.%d~%d.%d.%d.%d", &ip[3], &ip[2], &ip[1], &ip[0], &netmask[3], &netmask[2], &netmask[1], &netmask[0])==-1){ //err_ip_mask++; break; } else{ for(k=0; k<4; k++){ if(ip[k]>=0&&ip[k]<=255) continue; else{ err_ip_mask++; flag = 1; break; } } if(flag) continue; for(k=0; k<4; k++){ if(netmask[k]>=0&&netmask[k]<=255) continue; else{ err_ip_mask++; flag = 1; break; } } if(flag) continue; temp = netmask[0]+(netmask[1]<<8)+(netmask[2]<<16)+(netmask[3]<<24); one = 0; temp1 = temp; while(temp1){ one++; temp1 = temp1&(temp1-1); } one1 = 0; temp1 = temp>>(32-one); while(temp1){ one1++; temp1 = temp1&(temp1-1); } if(one1!=one){ err_ip_mask++; continue; } if(ip[3]>=1&&ip[3]<=126) A++; if(ip[3]>=128&&ip[3]<=191) B++; if(ip[3]>=192&&ip[3]<=223) C++; if(ip[3]>=224&&ip[3]<=239) D++; if(ip[3]>=240) E++; if(ip[3]==10) private_ip++; if(ip[3]==172&&ip[2]>=16&&ip[2]<=31) private_ip++; if(ip[3]==192&&ip[2]==168) private_ip++; } } printf("%d %d %d %d %d %d %d", A,B,C,D,E,err_ip_mask,private_ip); system("pause"); return 0; }