华为机试2


/**
@date:2010/09/14
@author:weedge
@comment:
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}
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void take_num(const char *strIn, int *n, unsigned int *outArray){
if(strIn == NULL){
printf("this is null string.");
}else{
int i = 0;
int j = 0;
int len = strlen(strIn);
while(j<=len){
if(*strIn >= '0' && *strIn <='9'){
const char *begstr = strIn;
const char *endstr = strIn;
while(*strIn >= '0' && *strIn <='9'){
if(*strIn == '0'){
begstr = strIn;
}
endstr = strIn;
strIn++;
}
if(*endstr != '0'){
/*
*(++endstr) = '/0';
*/
outArray[i] = atoi(begstr);/*atoi() just read back the front number*/
i++;
}else{
outArray[i] = 0;
i++;
}
}
strIn++;
j++;
}
*n = i;
}
}
int main(){
char *strIn = "ab00cd+123fght456-25  3.005fgh";
int n=0;
unsigned int outArray[20];
take_num(strIn,&n,outArray);
printf("n:%d/n",n);
for(int i=0; i<n; i++){
printf("outArray[%d]=%u/n",i,outArray[i]);
}
system("pause");
return 0;
}


/**
@date:2011/09/02 review (not to use the fun atoi())
@author:weedge
*/
#include <stdio.h>
#include <stdlib.h>


void take_num(const char *strIn, int *n, unsigned int *outArray){
if(strIn == NULL){
printf("this is null string.");
}else{
int i = 0;
while(*strIn){
if(*strIn >= '0' && *strIn <='9'){
outArray[i] = 0;
while(*strIn >= '0' && *strIn <='9'){
outArray[i] = outArray[i]*10 + ((*strIn++) - '0');
}
i++;
}
strIn++;
}
*n = i;
}
}


void take_num1(const char *strIn, int *n, unsigned int *outArray){ //use atoi()
if(strIn == NULL){ 
printf("this is null string."); 
}else{ 
int i = 0; 
while(*strIn){ 
if(*strIn >= '0' && *strIn <='9'){ 
const char *begstr = strIn; 
while(*strIn >= '0' && *strIn <='9'){ 
strIn++; 

outArray[i] = atoi(begstr);/*atoi() just read back the front number*/ 
i++; 

strIn++;

*n = i; 




int main(){
char *strIn = "ab00cd+123000fght456-25  3.005fgh";
int n=0;
unsigned int outArray[20];
take_num1(strIn,&n,outArray);
printf("n:%d/n",n);
for(int i=0; i<n; i++){
printf("outArray[%d]=%u/n",i,outArray[i]);
}
system("pause");
return 0;
}

/**
@date:2010/09/14
@author:weedge
@comment:
问题描述: 
在路由器中,一般来说转发模块采用最大前缀匹配原则进行目的端口查找,具体如下:
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",
""
}
输出:n = 2
*/


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_BUFFER 8
#define MAX_NETADDR 100
void max_prefix_match(const char *ip_addr, const char *net_addr_array[], int *n){
bool matched = false;/*if ip address is matched with subnet, the matching is true.*/
if(ip_addr == NULL){
printf("ip address is null/n");
exit(-1);
}
/*get the ip address;*/
int ip_address[4] = {0,0,0,0};
while(*ip_addr != '.') {
ip_address[0] = ip_address[0]*10+(*ip_addr-'0');
ip_addr++;
}
ip_addr++;
while(*ip_addr != '.') {
ip_address[1] = ip_address[1]*10+(*ip_addr-'0');
ip_addr++;
}
ip_addr++;
while(*ip_addr != '.') {
ip_address[2] = ip_address[2]*10+(*ip_addr-'0');
ip_addr++;
}
ip_addr++;
while(*ip_addr != '/0') {
ip_address[3] = ip_address[3]*10+(*ip_addr-'0');
ip_addr++;
}
int max_prefix = 0;/*save the max prefix value each ip&subnet match.*/
int prefix[MAX_NETADDR];/*save the prefix value each ip&subnet match.*/
int i = 0;
while(strcmp(net_addr_array[i],"")){
int subnet_addr[4];
int mask_addr[4];
/*copy ip address to compute the binary of ip address*/
int copy_ip_addr[4];
copy_ip_addr[0] = ip_address[0];
copy_ip_addr[1] = ip_address[1];
copy_ip_addr[2] = ip_address[2];
copy_ip_addr[3] = ip_address[3];
/*
const char *begstr = net_addr_array[i];
const char *endstr = net_addr_array[i];
*/
/*get the subnet address;*/
subnet_addr[0] = atoi(net_addr_array[i]);
while(*net_addr_array[i] != '.') net_addr_array[i]++;
subnet_addr[1] = atoi(++net_addr_array[i]);
while(*net_addr_array[i] != '.') net_addr_array[i]++;
subnet_addr[2] = atoi(++net_addr_array[i]);
while(*net_addr_array[i] != '.') net_addr_array[i]++;
subnet_addr[3] = atoi(++net_addr_array[i]);
/*get the mask address;*/
while(*net_addr_array[i] != '/') net_addr_array[i]++;
mask_addr[0] = atoi(++net_addr_array[i]);
while(*net_addr_array[i] != '.') net_addr_array[i]++;
mask_addr[1] = atoi(++net_addr_array[i]);
while(*net_addr_array[i] != '.') net_addr_array[i]++;
mask_addr[2] = atoi(++net_addr_array[i]);
while(*net_addr_array[i] != '.') net_addr_array[i]++;
mask_addr[3] = atoi(++net_addr_array[i]);

if(subnet_addr[0] == (ip_address[0]&mask_addr[0])
  &&subnet_addr[1] == (ip_address[1]&mask_addr[1])
  &&subnet_addr[2] == (ip_address[2]&mask_addr[2])
  &&subnet_addr[3] == (ip_address[3]&mask_addr[3])
){/*ip address is matched with subnet address.*/
matched = true;
prefix[i] = 0;
int ip_buffer[MAX_BUFFER],subnet_buffer[MAX_BUFFER];
int k;
for(k=0; k<MAX_BUFFER; k++){
ip_buffer[k] = 0;
subnet_buffer[k] = 0;
}

for(int j=0; j<4; j++){
k = 0;
while(subnet_addr[j]>0){
subnet_buffer[k++] = subnet_addr[j]%2;
subnet_addr[j] = subnet_addr[j]/2;
}
k = 0;
while(copy_ip_addr[j]>0){
ip_buffer[k++] = copy_ip_addr[j]%2;
copy_ip_addr[j] = copy_ip_addr[j]/2;
}
k = 0;
while(ip_buffer[k] == subnet_buffer[k] && k<8){
prefix[i]++;
k++;
}
}
if(max_prefix<prefix[i]){/*get the max prefix and get the suffix of the net_addr_array[].*/
max_prefix = prefix[i];
/*
printf("i = %d/n",i);
*/
*n = i;
}
}else{/*ip address is not matched with subnet address.*/
prefix[i] = 0;
}
i++;
}/*end while*/
if(!matched) printf("there are not the matched net addresses! so max prefix is 0./n");

}
int main(){
/*test*/
char *ip_addr = "192.168.1.100";
const char *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",
""
};
int n = 0;/*the suffix of the net_addr_array[].*/
max_prefix_match(ip_addr,net_addr_array,&n);
printf("n = %d./n",n);
system("pause");
return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值