一、如何提高编程能力?
1.对于简单问题:例如排序、查找,约瑟夫环、回文数、素数、润年,单向链表创建、删除、插入、查找、逆序,子字符串查找、系统字符处理函数的自己编写等;这部分严格要求自己,不需要机器调试、能够在纸质上写出,并能保证程序健壮性和尽可能的提高效率(包括内存使用率和性能)。
2.对于牵扯到工程问题:在常用IDE包括vc6.0,Linux下的gcc(vc6.0的c语法和规则可能更苛刻)等环境下,能迅速高效的完成。
二、以下是一道工程问题,做的可能还不到位
1.gcc//IP地址最大前缀匹配
#include <string.h>
#include <stdio.h>
int main()
{
printf("please input : IP,num,IPandMask list\n");
//
char* ipaddr = (char *)malloc(50);
scanf("%s",ipaddr);
char *p = NULL;
int ip[4];
int i = 0;
p = strtok(ipaddr,".");
while(p){
ip[i] = atoi(p);
i++;
p = strtok(NULL,".");
}
if(i < 3 ){
printf("error\n");
return -1;
}
//
int num;
scanf("%d",&num);
if(num <=0 ) return -1;
char iplist[num][50];
int k = 0;
for(k = 0;k<num;k++){
scanf("%s",iplist[k]);
}
///
int listip[num][4]; int listmask[num][4];
for(k=0;k<num;k++){
char* tmp = (char*) malloc(50);
strcpy(tmp,iplist[k]);
int i = 0;
p = iplist[k];
if(p!= NULL) p = strtok(iplist[k],".");
while(p){
if(p!=NULL) listip[k][i] = atoi(p);
i++;
if(i == 3){
if(p!=NULL) p = strtok(NULL,"/");
if(p!=NULL) listip[k][i] = atoi(p);
break;
}
if(p!=NULL) p = strtok(NULL,".");
}
if(i < 3){
printf("error\n");
return -1;
}
p = strtok(tmp,"/");
if(p == NULL) return -1;
p = strtok(NULL,".");
i = 0;
while(p){
if(p!=NULL) listmask[k][i] = atoi(p);
i++;
if(p!=NULL) p = strtok(NULL,".");
}
if(i < 3) return -1;
}
//
//int listip[num][4]; int listmask[num][4];
//int ip[4];
int result[num];
int j = 0;
for(k=0;k<num;k++){
result[k] = 0;
for(j=0;j<4;j++){
if((ip[j]&listmask[k][j]) != listip[k][j]){
result[k] = -1;
break;
}
}
if(result[k] == -1) continue;
for(j=0;j<4;j++){
int tmpip = ip[j];
int tmplistip = listip[k][j];
int tmp = 128;
for(tmp=128;tmp!=0;tmp=tmp/2){
if((tmpip/tmp) == (tmplistip/tmp)){
result[k]++;
tmpip = tmpip%tmp;
tmplistip = tmplistip%tmp;
}
else break;
}
}
}
///
int res = 0;
for(k=0;k<num;k++){
if(res<=result[k]) res = result[k];
}
printf("res is %d\n",res);
return res;
}
2.vc6.0——要求变量定义在所有函数使用之前!
//IP地址最大前缀匹配
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
char* ipaddr;
int main()
{
//printf("please input : IP,num,IPandMask list\n");
//
char* ipaddr = (char*)malloc(50);
char *p = NULL;
int ip[4];
int i = 0;
int num;
char iplist[50][50];
int listip[50][4]; int listmask[50][4];
int k = 0;
int result[50];
int j = 0;
int res = 0;
scanf("%s",ipaddr);
p = strtok(ipaddr,".");
while(p){
ip[i] = atoi(p);
i++;
p = strtok(NULL,".");
}
if(i < 3 ){
printf("error\n");
return -1;
}
//
scanf("%d",&num);
if(num <=0 ) return -1;
for(k = 0;k<num;k++){
scanf("%s",iplist[k]);
}
///
for(k=0;k<num;k++){
char* tmp = (char*) malloc(50);
strcpy(tmp,iplist[k]);
i = 0;
p = iplist[k];
if(p!= NULL) p = strtok(iplist[k],".");
while(p){
if(p!=NULL) listip[k][i] = atoi(p);
i++;
if(i == 3){
if(p!=NULL) p = strtok(NULL,"/");
if(p!=NULL) listip[k][i] = atoi(p);
break;
}
if(p!=NULL) p = strtok(NULL,".");
}
if(i < 3){
printf("error\n");
return -1;
}
p = strtok(tmp,"/");
if(p == NULL) return -1;
p = strtok(NULL,".");
i = 0;
while(p){
if(p!=NULL) listmask[k][i] = atoi(p);
i++;
if(p!=NULL) p = strtok(NULL,".");
}
if(i < 3) return -1;
}
//
//int listip[num][4]; int listmask[num][4];
//int ip[4];
for(k=0;k<num;k++){
result[k] = 0;
for(j=0;j<4;j++){
if((ip[j]&listmask[k][j]) != listip[k][j]){
result[k] = -1;
break;
}
}
if(result[k] == -1) continue;
for(j=0;j<4;j++){
int tmpip = ip[j];
int tmplistip = listip[k][j];
int tmp = 128;
for(tmp=128;tmp!=0;tmp=tmp/2){
if((tmpip/tmp) == (tmplistip/tmp)){
result[k]++;
tmpip = tmpip%tmp;
tmplistip = tmplistip%tmp;
}
else break;
}
}
}
///
for(k=0;k<num;k++){
if(res<=result[k]) res = result[k];
}
printf("res is %d\n",res);
return res;
}
3.测试
192.168.1.100
4
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
结果:
26
三、其他解法
网上有朋友使用了ssanf和sprint等格式化输入/输出函数,将字符串查找部分简化:
http://blog.csdn.net/wangshihui512/article/details/8850418