读书人

务实现求思路一个有点类似数据库的

发布时间: 2012-09-12 09:21:30 作者: rapoo

求实现,求思路,一个有点类似数据库的查询的功能实现
11|192.168.1.1|192.168.0.4|egrteavsdfs|
12|192.168.1.1|192.168.0.4|abcd2221111|
13|192.168.0.0|egrteavsdfs|abcd1111112|
14|192.168.0.2|abcdwgwsefg|...........|
15|192.168.0.3|abcdsafrtgt|...........|
16|192.168.0.4|abcdsadfasf|...........|
17|192.168.1.1|...........|192.168.0.3|
18|192.168.1.1|192.168.0.4|...........|
19|...........|192.168.0.4|192.168.0.3|
20|...........|192.168.1.1|egrteavsdfs|

表如上图所示。

例如: 表中的12则对应它后头的 |192.168.1.1|192.168.0.4|abcd2221111| 这3个值

如果选择输入值是 192.168.1.104 , 则输出key : 11 , 12, 16, 18 , 19
输入值是 192.168.1.1, 则输出key: 11, 12, 17 , 18 , 20

怎么实现?



[解决办法]

C/C++ code
//11|192.168.1.1|192.168.0.4|egrteavsdfs|//12|192.168.1.1|192.168.0.4|abcd2221111|//13|192.168.0.0|egrteavsdfs|abcd1111112|//14|192.168.0.2|abcdwgwsefg|...........|//15|192.168.0.3|abcdsafrtgt|...........|//16|192.168.0.4|abcdsadfasf|...........|//17|192.168.1.1|...........|192.168.0.3|//18|192.168.1.1|192.168.0.4|...........|//19|...........|192.168.0.4|192.168.0.3|//20|...........|192.168.1.1|egrteavsdfs|////表table.txt如上图所示。////例如: 表中的12则对应它后头的 |192.168.1.1|192.168.0.4|abcd2221111| 这3个值////如果选择输入值是 192.168.0.4 , 则输出key : 11 , 12, 16, 18 , 19//  输入值是 192.168.1.1, 则输出key: 11, 12, 17 , 18 , 20//#include <stdio.h>#include <string.h>#define MAXCHARS 100#define MAXLINES 100000char s[MAXLINES][MAXCHARS];char ip[20];int i,n;FILE *f;int main() {    f=fopen("table.txt","r");    if (NULL==f) {        printf("Can not open file table.txt!\n");        return 1;    }    i=0;    while (1) {        if (NULL==fgets(s[i],MAXCHARS,f)) break;        if ('\n'!=s[i][strlen(s[i])-1]) {            printf("line %d of table.txt too long!(>%d characters)",i+1,MAXCHARS-1);            fclose(f);            return 2;        }        i++;        if (i>=MAXLINES) {            printf("Warning:>line %d ignored!\n",MAXLINES);            break;        }        if (0==(i%1000)) printf("%d lines imported from file table.txt\n",i);    }    fclose(f);    printf("Total %d lines imported from file table.txt\n",i);    n=i;    while (1) {        printf("Input IP:");        fflush(stdout);        ip[0]='|';        fgets(ip+1,19,stdin);        if (strlen(ip)<9) break;        if ('\n'==ip[strlen(ip)-1]) ip[strlen(ip)-1]=0;        strcat(ip,"|");        for (i=0;i<n;i++) {            if (strstr(s[i],ip)) printf("%.2s,",s[i]);        }        printf("\n");    }    return 0;}//Input IP:192.168.0.4//11,12,16,18,19,//Input IP:192.168.1.1//11,12,17,18,20,//Input IP:
[解决办法]
数字定不定长无所谓啊,文件里取出的都是buf,转换后才是数字类型。

读书人网 >C语言

热点推荐