gpt4 book ai didi

c - Boyer-Moore-Horspool 实现

转载 作者:太空宇宙 更新时间:2023-11-04 03:46:53 27 4
gpt4 key购买 nike

我想获得 Boyer-Moore-Horspool 实现来搜索文本文件中的某些字符串。这是我的代码:

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

int bmhSearch(char *needle) {
FILE *fp;
int find_result = 0;
char temp[512];

size_t nlen = strlen(needle);

size_t scan = 0;
size_t bad_char_skip[UCHAR_MAX + 1];
size_t last;

if((fopen_s(&fp, "book.txt", "r")) != NULL) {
return(-1);
}

while(fgets(temp, 512, fp) != NULL) {
size_t hlen = strlen(temp);
/* pre */
for (scan = 0; scan <= UCHAR_MAX; scan = scan + 1)
bad_char_skip[scan] = nlen;
last = nlen - 1;
for (scan = 0; scan < last; scan = scan + 1)
bad_char_skip[needle[scan]] = last - scan;

while (hlen >= nlen){
/* scan from the end of the needle */
char *ptemp = temp;
for (scan = last; ptemp[scan] == needle[scan]; scan = scan - 1){
if (scan == 0){
find_result++;
}
}

hlen -= bad_char_skip[ptemp[last]];
ptemp += bad_char_skip[ptemp[last]];
printf("%d\t%d\n", hlen, nlen);
}
}

if(fp) {
fclose(fp);
}
return find_result;
}

int main(void){
char needle[30] = {"some text like this"};
printf("Matches found: %d\n", bmhSearch(needle);
}

我相信,我做错了很多事情,但我真的找不到并修复它。我唯一得到的是程序在某个阶段不符合条件 while(hlen >= nlen)

最佳答案

如果问题是“代码有什么问题?”;这里有一些事情可以开始:

 printf("Matches found: %d\n", bmhSearch(needle); // Missing a final ')'.

 int main(void)
{
...
return(0); // Something like this line is required for a function that returns'int'.
}

     size_t bad_char_skip[UCHAR_MAX + 1];  // Requires: '#include <limits.h>

     if((fopen_s(&fp, "book.txt", "r")) != NULL) {

函数“fopen_s()”不返回指针。它返回一个 errno_t 类型,它是一个整数值。将整数与“NULL”进行比较就像打扮成牛仔出现在“印度战舞”中一样。也许以下更合适:

     if((fopen_s(&fp, "book.txt", "r")) != 0) {

相当于(我最喜欢的):

     if(fopen_s(&fp, "book.txt", "r")) {

        printf("%d\t%d\n", hlen, nlen);

上面的格式字符串不正确。应该是:

        printf("%zu\t%zu\n", hlen, nlen);

在严格的 C 意义上,需要注意以下几行:

        bad_char_skip[needle[scan]] = last - scan;

hlen -= bad_char_skip[ptemp[last]];
ptemp += bad_char_skip[ptemp[last]];

它们应更改为以下内容:

        bad_char_skip[(int)needle[scan]] = last - scan;

hlen -= bad_char_skip[(int)ptemp[last]];
ptemp += bad_char_skip[(int)ptemp[last]];

变量“ptemp”被定义为“char *”。因此; 'ptemp[n]' 评估为 'char' 类型;而数组索引号必须是“int”。

关于c - Boyer-Moore-Horspool 实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23598301/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com