加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

华为面试题:识别有效的ip地址和子网掩码并分类 C语言源码

发布时间:2020-12-13 20:17:55 所属栏目:PHP教程 来源:网络整理
导读:辨认有效的ip地址和子网掩码并分类:按行输入多组数据 10.8.9.2~255.0.0.0180.8.9.2~255.254.0.0打印:A B C D E 毛病的ip或掩码 私有ip 的个数#include stdio.h#include stdlib.h#include string.h#define MAX_PATH 256int maze[10][10] = {0};int route[10
辨认有效的ip地址和子网掩码并分类: 按行输入多组数据 10.8.9.2~255.0.0.0 180.8.9.2~255.254.0.0 打印:A B C D E 毛病的ip或掩码 私有ip 的个数 #include "stdio.h" #include "stdlib.h" #include "string.h" #define MAX_PATH 256 int maze[10][10] = {0}; int route[100][2] = {0}; int impl(char *ip,char *mask,int *privateIp) { char *a = strtok(ip,"."); char *b = strtok(NULL,"."); char *c = strtok(NULL,"."); char *d = strtok(NULL,"."); if (!(a && b && c && d)) { return 5; } int ai = atoi(a); int bi = atoi(b); int ci = atoi(c); int di = atoi(d); // char *a1 = strtok(mask,"."); // char *b1 = strtok(NULL,"."); // char *c1 = strtok(NULL,"."); // char *d1 = strtok(NULL,"."); if(ai>0 && ai<=126 && strcmp(mask,"255.0.0.0")==0) { if (ai==10 && bi>=0 && bi<=255) { *privateIp = 1; } return 0; } else if(ai>=128 && ai<=191 && strcmp(mask,"255.255.0.0")==0) { if (ai==172 && bi>=16 && bi<=31) { *privateIp = 1; } return 1; } else if(ai>=192 && ai<=223 && strcmp(mask,"255.255.255.0")==0) { if (ai==192 && bi==168) { *privateIp = 1; } return 2; } else if(ai>=224 && ai<=239) { return 3; } else if(ai>=240 && ai<=255) { return 4; } else { return 5; } } int main() { // char test[4][100]={"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"}; int result[7] = {0}; char test[100] = {0}; while(scanf("%s",test)!=EOF) { char *ip = strtok(test,"~"); char *mask = strtok(NULL,"~"); int n = impl(ip,mask,&result[6]); result[n]++; } // for (int i=0;i<4;i++) // { // char *ip = strtok(test[i],"~"); // char *mask = strtok(NULL,"~"); // int n = impl(ip,&result[6]); // result[n]++; // } for (int i=0;i<7;i++) { printf("%d ",result[i]); } return 0; }


 

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读