check_ip

来源:互联网 发布:java 线程 static 编辑:程序博客网 时间:2024/06/03 22:40

 

一道笔试题,求ip~~的,忘了题目是什么了,挺简单的,主要考查sscanf库函数的应用!!! 

18#include <stdio.h>

 19 #include <iostream>

 20 using namespace std;

 21 bool is_ip_segment(char *start_ip,char *end_ip,char *check_ip,int *ip_count)

 22 {

 23     long int count_start,count_end,count_check;

 24     int ip_1,ip_2,ip_3,ip_4,int_ip_count;

 25     sscanf(start_ip,"%d.%d.%d.%d",&ip_1,&ip_2,&ip_3,&ip_4);

 26     count_start = ip_1 *256*256*256 + ip_2 * 256*256 + ip_3 * 256 + ip_4;

 27 

 28     sscanf(end_ip,"%d.%d.%d.%d",&ip_1,&ip_2,&ip_3,&ip_4);//注意sscanf用法

 29     count_end = ip_1 *256*256*256 + ip_2 * 256*256 + ip_3 * 256 + ip_4;

 30     //printf("%d,%d,%d,%d",ip_1,ip_2,ip_3,ip_4);

 31     sscanf(check_ip,"%d.%d.%d.%d",&ip_1,&ip_2,&ip_3,&ip_4);

 32     count_check = ip_1 *256*256*256 + ip_2 * 256*256 + ip_3 * 257 + ip_4;

 33     int_ip_count = count_end - count_start;

 34     *ip_count = int_ip_count;

 35     if(count_check > count_start && count_check < count_end)

 36         return true;

 37     else

 38         return false;

 39 }

 40 int main()

 41 {

 42     int temp = 0;

 43     int *a = &temp;

 44     bool is_ip;

 45     char start[16],end[16],check[16];

 46     scanf("%s %s %s",start,end,check);

 47     is_ip = is_ip_segment(start,end,check,a);

 48     printf("总共有%d",*a);

 49     is_ip ? printf("是在这个区间内"):printf("不是在这个区间内");

 50     return 0;

 51 }

 

原创粉丝点击