北航研究生复试2008上机第三题:字符串匹配

题目

从string.in中读入数据,然后用户输入一个短字符串。要求查找string.in中和短字符串的所有匹配,输出行号、匹配字符串到string.out文件中。匹配时不区分大小写,并且可以有一个用中括号表示的模式匹配。如“aa[123]bb”,就是说aaa1bb、aa2bb、aa3bb都算匹配。

主要部分:
1、读写文件
2、KMP匹配或者BF匹配算法
3、不区分大小写的比较
4、对于输入的处理(主要是带‘[ ]’的处理)

提示:因为北航上机的要求,编译器为VC6.0。如果用vs2015会存在部分函数不安全的提示如fopen等。

代码:



#include<stdio.h>
#include<stdlib.h>
#include<string.h>

char* file_in = "string.in";    //要读的文件
char* file_out = "string.out";  //要写的文件
int temp = 0;       //标记   是否有匹配项 有则改为1

char* ReadFile(char* filename);                 //读文件到字符数组
char** GetInputStr(int &kind, int &len);        //从控制台得到短字符串
void Match_KMP(char* note, int size, char* str, int len);   //KMP匹配  同时将匹配字符写入文件file_out
void GetNext(char* str,int len,int* next);      //求KMP辅助数组NEXT
void WriteFile(int line, char* str, int len);   //写文件
bool Equal(char a,char b);                      //比较两个字符是否相等,忽略大小写

int main(){
    char* note = NULL;      //记录文件中的字符串 
    note = ReadFile(file_in);
    int size = strlen(note);
    printf("the size of file :%d\n",size);
    printf("the content of file is: \n%s\n",note);

    int kind = 0;           //输入的短字符的种类,如果有[],则分为多组
    int len = 0;            //短字符每组的的大小
    char** str = NULL;      //记录短字符串数组
    str = GetInputStr(kind,len);
    printf("the kind of str :%d\n",kind);
    printf("the size of str :%d\n",len);
    int i;
    for(i = 0; i < kind; i++)
        printf("the content of str is:%s\n",str[i]);

    for(i = 0; i < kind; i++)//对每一组段字符串进行匹配
        Match_KMP(note,size,str[i],len);
    if(temp == 0){
        printf("文件中没有匹配项\n");
    }
    return 0;
}

char* ReadFile(char* filename){
    //estimate the size of file
    int size = 0;
    int ch;
    FILE* file;
    if((file = fopen(filename,"r")) == NULL){
        printf("The file %s can not be opened\n", filename);
        exit(1);
    }
    while((ch = fgetc(file)) != EOF){       //第一次遍历得到文件的大小
        ++size;
    }
    rewind(file);

    char* note = (char* )malloc(sizeof(char) * (size+1));
    note[size] = '\0';
    int i;
    for(i = 0; i < size; i++){              //第二次遍历取文件的数据
        ch = fgetc(file);
        note[i] = ch;
    }

    fclose(file);
    return note;
}

char** GetInputStr(int &kind, int &len){
    printf("输入短字符串大小: ");
    scanf("%d",&len);
    printf("输入一个短字符串:");
    char* str = (char* )malloc(sizeof(char) * (len + 1));
    scanf("%s", str);
    str[len] = '\0';
    char** arr;

    //有没有aa[123]bb形式
    int tag1 = 0;       //记录'['在字符串中的位置
    int tag2 = 0;       //记录']'在字符串中的位置
    int i,j,k;
    for(i = 0; i < len; i++){
        if(str[i] == '[')
            tag1 = i;
        if(str[i] == ']')
            tag2 = i;
    }
    if(tag1 && tag2 && tag2 > tag1){    //有多组需要匹配
        int num = tag2 - tag1 - 1;      //短字符串的个数
        arr = (char** ) malloc(sizeof(char* ) * num);
        for(i = 0; i < num; i++){
            arr[i] = (char* )malloc(sizeof(char) * (len - num));
        }

        k = 0;
        for(i = 0; i < num; i++){       //赋值每个段字符串
            for(j = 0; j < len; j++){
                if(j < tag1)
                    arr[i][k++] = str[j];
                if(j == tag1) 
                    arr[i][k++] = str[tag1 + 1 + i];
                if(j >= tag2)
                    arr[i][k++] = str[j + 1];
            }
            k = 0;
        }
        kind = num;                     //短字符串的个数
        len = len - num - 1;            //每个段字符串的大小
    }   
    else{                               //只有一组短字符串
        arr = (char** ) malloc(sizeof(char* ));
        kind = 1;
        arr[0] = str;
    }
    return arr;
}

void Match_KMP(char* note, int size, char* str, int len){
    if(len <= 0){
        printf("子串输入错误\n");
        return ;
    }
    int i,j;
    int *next = (int *)malloc(sizeof(int) * len);
    GetNext(str,len,next);              //计算NEXT[]辅助数组
    //
    printf("the Next[]:");
    for(i = 0; i < len; i++)
        printf("%d ",next[i]);
    printf("\n");

    j = 0;
    int line = 0;                       //记录行号
    for(i = 0; i < size;){              //进行匹配
        if(note[i] == '\n')             
            ++line;
        if(Equal(note[i],str[j])){      //进行字符比较
            ++i;++j;
            if(j == len){               //成功完全匹配
                WriteFile(line,&note[i - j],len);   //写入文件
                temp = 1;               //存在匹配项
            }
        }else{
            if(j == 0){
                ++i;
            }else{
                j = next[j - 1];
            }
        }
    }

}

void GetNext(char* str,int len,int* next){
    next[0] = 0;
    int i,j;
    i = 1;j = 0;
    for(; i < len ;){
        if(j == 0 || Equal(str[i-1],str[j-1])){
            next[i] = ++j;
            i++;
        }else{
            j = next[j - 1];
        }
    }
}

void WriteFile(int line, char* str, int len){
    FILE* file;
    int i;
    if((file = fopen(file_out,"a"))  == NULL){
        printf("The file %s can not be wirtten\n",file_out);
        exit(1);
    }
    fputc((line + 1 + '0'),file);
    fputc(' ',file);
    for(i = 0; i < len; i++)
        fputc(str[i],file);
    fputc('\n',file);
    fclose(file);
}

bool Equal(char a,char b){
    if(b >='0' && b <= '9')
        if(b == a)
            return true;

    if(b >= 'a' && b <= 'z')
        if(b == a || b - ('a' - 'A') == a)
            return true;

    if(b >= 'A' && b <= 'Z')
        if(b == a || b +    ('a' - 'A') == a)
            return true;

    return false;
}

例子(本人随便弄的):
1、
string.in:
Who has seen the winD?
Neither I nor you;
But when the leaves hang trembling,
The wInd is passing through.
Who has seen the WIND?
Neither you nor I;
But when the trees bow down their heads,
The Wind is passing by.
–Christina Rossett

控制台输入:
4
wind

2、
string.in:
Aa1bb
aA2Bb
aa3BB
aa4Ab
aA5bB

控制台输入:
9
aa[123]bb

结果:
1、
这里写图片描述
这里写图片描述
这里写图片描述

2、
这里写图片描述
这里写图片描述
这里写图片描述

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值