void locating(char name[],char keys[]) { int i=1; //i为行号 int j=0; //j为列号(该行第几个单词) int k; //每一行出现的次数 char text; //存放进行比对的一行的字符串 int count=0; //出现总次数 //打开目标文件 FILE *fp; fp=fopen(name,"r"); if(fp==NULL) { printf("File Opening Failure"); exit(0); } keys[0]=length(keys); get_next(keys,next); printf("%s\n",&keys[1]); //打印关键字 while(!feof(fp)) { k=0; fgets(&text[1],MaxSize,fp); text[0]=length(text); j=KMP_match(text,keys,j+1); if(j!=0) { if(text[j+keys[0]]==' '||text[j+keys[0]]=='"'||text[j+keys[0]]==','text[j+keys[0]]=='!'||text[j+keys[0]]=='.'||text[j+keys[0]]=='?'||text[j+keys[0]]==':'||text[j+keys[0]]==';')//判断是否为完整单词 { printf("row=%d,column=%d",i,j); k++; } } while(j!=0) { j=KMP_match(text,keys,j+1); //调用KMP算法从刚找到的列号后一字符起匹配 if (j!=0) { if(text[j+keys[0]]==' '||text[j+keys[0]]=='"'||text[j+keys[0]]==','text[j+keys[0]]=='!'||text[j+keys[0]]=='.'||text[j+keys[0]]=='?'||text[j+keys[0]]==':'||text[j+keys[0]]==';')//判断是否为完整单词 { printf(",%d",j); //若匹配成功,则打印列号 k++; } } printf("\n"); i++; count=count+k; } fclose(fp); if(count) { printf("%s共出现%d次\n",&keys[1],count); } else { printf("Not Found the Keyword(s)\n"); } }
时间: 2024-01-28 10:05:51 浏览: 19
这段代码中存在一些问题:
1. 在判断完整单词时,多个判断条件之间缺少了 `||` 运算符,这会导致编译器报错。请将两个判断条件之间加上 `||` 运算符,如下所示:
```
if (text[j + keys[0]] == ' ' || text[j + keys[0]] == '"' || text[j + keys[0]] == ',' || text[j + keys[0]] == '!' || text[j + keys[0]] == '.' || text[j + keys[0]] == '?' || text[j + keys[0]] == ':' || text[j + keys[0]] == ';')
```
2. 在判断完整单词时,你的代码中判断字符的位置可能有问题。应该是 `text[j + keys[0]]` 而不是 `text[j - keys[0]]`。
3. 在判断完整单词时,你的代码中缺少了一个空格,这可能导致判断不准确。应该在逗号、感叹号、句号等字符之间加上空格,如下所示:
```
if (text[j + keys[0]] == ' ' || text[j + keys[0]] == '"' || text[j + keys[0]] == ',' || text[j + keys[0]] == '!' || text[j + keys[0]] == '.' || text[j + keys[0]] == '?' || text[j + keys[0]] == ':' || text[j + keys[0]] == ';')
```
4. 在打印列号时,第一次打印的列号前面应该没有逗号。
5. 在打印列号时,应该使用 `printf` 函数,而不是 `cout`。
经过修改后的代码如下所示:
```
void locating(char name[], char keys[]) {
int i = 1; // i为行号
int j = 0; // j为列号(该行第几个单词)
int k; // 每一行出现的次数
char text[MaxSize]; // 存放进行比对的一行的字符串
int count = 0; // 出现总次数
// 打开目标文件
FILE* fp;
fp = fopen(name, "r");
if (fp == NULL) {
printf("File Opening Failure");
exit(0);
}
keys[0] = length(keys);
get_next(keys, next);
printf("%s\n", &keys[1]); // 打印关键字
while (!feof(fp)) {
k = 0;
fgets(&text[1], MaxSize, fp);
text[0] = length(text);
j = KMP_match(text, keys, j + 1);
if (j != 0) {
if (text[j + keys[0]] == ' ' || text[j + keys[0]] == '"' || text[j + keys[0]] == ',' || text[j + keys[0]] == '!' || text[j + keys[0]] == '.' || text[j + keys[0]] == '?' || text[j + keys[0]] == ':' || text[j + keys[0]] == ';') // 判断是否为完整单词
{
printf("row=%d,column=%d", i, j); // 第一次打印的列号前面没有逗号
k++;
}
}
while (j != 0) {
j = KMP_match(text, keys, j + 1); // 调用KMP算法从刚找到的列号后一字符起匹配
if (j != 0) {
if (text[j + keys[0]] == ' ' || text[j + keys[0]] == '"' || text[j + keys[0]] == ',' || text[j + keys[0]] == '!' || text[j + keys[0]] == '.' || text[j + keys[0]] == '?' || text[j + keys[0]] == ':' || text[j + keys[0]] == ';') // 判断是否为完整单词
{
printf(",%d", j); // 使用 printf 函数打印列号
k++;
}
}
}
printf("\n");
i++;
count = count + k;
}
fclose(fp);
if (count) {
printf("%s共出现%d次\n", &keys[1], count);
}
else {
printf("Not Found the Keyword(s)\n");
}
}
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)