华为机试题(4)

1、     识别字符串中的整数并转换为数字形式

void take_num(const char *strIn, int *n, unsigned int *outArray)

【输入】 strIn:   输入的字符串

【输出】 n:       统计识别出来的整数个数

       outArray:识别出来的整数值,其中outArray[0]是输入字符串中从左到右第一个整数,

 outArray[1]是第二个整数,以此类推。数组地址已经分配,可以直接使用

【返回】 无

注:

I、     不考虑字符串中出现的正负号(+, -),即所有转换结果为非负整数(包括0和正整数)

II、    不考虑转换后整数超出范围情况,即测试用例中可能出现的最大整数不会超过unsigned int可处理的范围

III、   需要考虑 '0' 开始的数字字符串情况,比如 "00035" ,应转换为整数35;

        "000" 应转换为整数0;"00.0035" 应转换为整数0和35(忽略小数点:mmm.nnn当成两个数mmm和nnn来识别)

IV、   输入字符串不会超过100 Bytes,请不用考虑超长字符串的情况。

示例

输入:strIn = "ab00cd+123fght456-25  3.005fgh" 

输出:n = 6

outArray = {0, 123, 456, 25, 3, 5}

  1. #include <stdio.h>   
  2. #include <string.h>   
  3.   
  4. void take_num(const char *strIn, int *n, unsigned int *outArray)  
  5. {  
  6.     unsigned int res;  
  7.     /*unsigned int */  
  8.     int m=0;  
  9.     while(*strIn != '\0')  
  10.     {  
  11.         while(!(*strIn>='0' && *strIn <= '9') && *strIn != '\0')  
  12.             strIn++;  
  13.         if(*strIn != '\0')  
  14.         {  
  15.             res= *strIn-'0';  
  16.             strIn++;  
  17.             while(*strIn>='0' && *strIn <= '9')  
  18.             {  
  19.                 res = 10*res+(*strIn-'0');  
  20.                 strIn++;  
  21.             }  
  22.             outArray[m] =res;  
  23.             m++;  
  24.         }  
  25.     }  
  26.     *n = m;  
  27. }  
  28.   
  29. int main(/*int argc, char **argv*/)  
  30. {  
  31.     int num;  
  32.     int i;  
  33.     const char strIn[50]="ab00cd+123fght456-25  3.005fgh";  
  34.     unsigned int outArray[50];  
  35.     take_num(strIn,&num,outArray);  
  36.     for(i=0;i<num;i++)  
  37.         printf("%d ",outArray[i]);  
  38.   
  39.     system("pause");  
  40.     return 0;  
  41. }  
#include <stdio.h>
#include <string.h>

void take_num(const char *strIn, int *n, unsigned int *outArray)
{
	unsigned int res;
	/*unsigned int */
	int m=0;
	while(*strIn != '\0')
	{
		while(!(*strIn>='0' && *strIn <= '9') && *strIn != '\0')
			strIn++;
		if(*strIn != '\0')
		{
			res= *strIn-'0';
			strIn++;
			while(*strIn>='0' && *strIn <= '9')
			{
				res = 10*res+(*strIn-'0');
				strIn++;
			}
			outArray[m] =res;
			m++;
		}
	}
	*n = m;
}

int main(/*int argc, char **argv*/)
{
	int num;
	int i;
	const char strIn[50]="ab00cd+123fght456-25  3.005fgh";
	unsigned int outArray[50];
	take_num(strIn,&num,outArray);
	for(i=0;i<num;i++)
		printf("%d ",outArray[i]);

	system("pause");
	return 0;
}


1、    IP地址匹配(60分)

 

问题描述:

在路由器中,一般来说转发模块采用最大前缀匹配原则进行目的端口查找,具体如下:

IP地址和子网地址匹配:

IP地址和子网地址所带掩码做AND运算后,得到的值与子网地址相同,则该IP地址与该子网匹配。 

比如:

IP地址:192.168.1.100

子网:192.168.1.0/255.255.255.0,其中192.168.1.0是子网地址,255.255.255.0是子网掩码。

192.168.1.100&255.255.255.0 = 192.168.1.0,则该IP和子网192.168.1.0匹配

IP地址:192.168.1.100

子网:192.168.1.128/255.255.255.192

192.168.1.100&255.255.255.192 = 192.168.1.64,则该IP和子网192.168.1.128不匹配

最大前缀匹配:

任何一个IPv4地址都可以看作一个32bit的二进制数,比如192.168.1.100可以表示为:11000000.10101000.00000001.01100100,

192.168.1.0可以表示为11000000.10101000.00000001.00000000

最大前缀匹配要求IP地址同子网地址匹配的基础上,二进制位从左到右完全匹配的位数尽量多(从左到右子网地址最长)。比如:

IP地址192.168.1.100,同时匹配子网192.168.1.0/255.255.255.0和子网192.168.1.64/255.255.255.192,

但对于子网192.168.1.64/255.255.255.192,匹配位数达到26位,多于子网192.168.1.0/255.255.255.0的24位,

因此192.168.1.100最大前缀匹配子网是192.168.1.64/255.255.255.192。

 

请编程实现上述最大前缀匹配算法。 

要求实现函数:

void max_prefix_match(const char *ip_addr, const char *net_addr_array[], int *n)

【输入】ip_addr:IP地址字符串,严格保证是合法IPv4地址形式的字符串

        net_addr_array:子网地址列表,每一个字符串代表一个子网,包括子网地址和掩码,

                        表现形式如上述,子网地址和子网掩码用’/’分开,严格保证是

                        合法形式的字符串;如果读到空字符串,表示子网地址列表结束

【输出】n:最大前缀匹配子网在*net_addr_array[]数组中对应的下标值。如果没有匹配返回-1

示例

输入:

ip_addr = "192.168.1.100"

net_addr_array[] =

{

 

"192.168.1.128/255.255.255.192",

 

"192.168.1.0/255.255.255.0",

 

"192.168.1.64/255.255.255.192",

 

"0.0.0.0/0.0.0.0",

 

""

  1. #include <stdio.h>   
  2. #include <string.h>   
  3.   
  4. void max_prefix_match(const char *ip_addr, const char *net_addr_array[], int *n)  
  5. {  
  6.     int ip,subnet_ip,subnet_mask;  
  7.     int ip_pa[4],subnet_ip_pa[4],subnet_mask_pa[4];  
  8.     int index;  
  9.     int i,result,cnt=0;  
  10.     int maxi=0;  
  11.     sscanf(ip_addr,"%d.%d.%d.%d",&ip_pa[0],&ip_pa[1],&ip_pa[2],&ip_pa[3]);  
  12.     ip = (ip_pa[0]<<24)+(ip_pa[1]<<16)+(ip_pa[2]<<8)+(ip_pa[3]);  
  13.     *n =-1;  
  14.     for(index = 0;net_addr_array[index][0] != '\0';index++)  
  15.     {  
  16.         sscanf(net_addr_array[index],"%d.%d.%d.%d/%d.%d.%d.%d",&subnet_ip_pa[0],&subnet_ip_pa[1],&subnet_ip_pa[2],&subnet_ip_pa[3],&subnet_mask_pa[0],&subnet_mask_pa[1],&subnet_mask_pa[2],&subnet_mask_pa[3]);  
  17.         subnet_ip = (subnet_ip_pa[0]<<24)+(subnet_ip_pa[1]<<16)+(subnet_ip_pa[2]<<8)+(subnet_ip_pa[3]);  
  18.         subnet_mask = (subnet_mask_pa[0]<<24)+(subnet_mask_pa[1]<<16)+(subnet_mask_pa[2]<<8)+(subnet_mask_pa[3]);  
  19.         if((ip & subnet_mask) == subnet_ip)  
  20.         {  
  21.             //统计子网掩码中1的个数   
  22.             for(i=0;i<sizeof(subnet_mask)*8;i++)  
  23.             {  
  24.                 result = subnet_mask & 1;  
  25.                 subnet_mask=subnet_mask>>1;  
  26.                 cnt+=result;  
  27.             }  
  28.             if(cnt>maxi)  
  29.             {  
  30.                 maxi = cnt;  
  31.                 *n = index;  
  32.             }  
  33.         }  
  34.   
  35.     }  
  36.   
  37. }  
  38.   
  39. int main(/*int argc, char **argv*/)  
  40. {  
  41.     int n;  
  42.     const char ip_addr[50] = "192.168.1.100";  
  43.     const char *net_addr_array[256]=  
  44.     {  
  45.   
  46.         "192.168.1.128/255.255.255.192",  
  47.   
  48.         "192.168.1.0/255.255.255.0",  
  49.   
  50.         "192.168.1.64/255.255.255.192",  
  51.   
  52.         "0.0.0.0/0.0.0.0",  
  53.         ""  
  54.           
  55.   
  56.     };  
  57.     max_prefix_match(ip_addr,net_addr_array,&n);  
  58.   
  59.     printf("n=%d\n ",n);  
  60.   
  61.     system("pause");  
  62.     return 0;  
  63. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值