gpt4 book ai didi

c - 为什么我的程序为模式匹配程序提供错误的输出?

转载 作者:行者123 更新时间:2023-11-30 19:50:52 25 4
gpt4 key购买 nike

我用“c”语言编写了一个模式匹配程序。但它总是向我显示错误的输出。使用相同的逻辑,我用“c++”语言编写了代码,它在那里工作得很好,但在“c”中却不行。

根据我的程序,首先我需要输入我想要输入的“文本”和“图案”的大小。然后根据大小,使用for循环在字符数组中存储相应数量的字符。但对于前任来说。如果我输入文本大小为 5,它只存储 2 个字符,然后进一步显示错误答案。

#include<stdio.h>
int p, t, c, LOC, i, j;

void main()

{

printf("Enter size of Text and Pattern\n");

scanf("%d %d",&t,&p);

char pat[p];

char txt[t];

printf("Enter the Text\n");
for(i=0;i<t;i++)
{
scanf("%c",&txt[i]);
}
printf("Enter the Pattern\n");
for(i=0;i<p;i++)
{
scanf("%c",&pat[i]);
}
int MAX=t-p+1;


for(i=0;i<MAX;i++)
{
int count=0;
c=0;
for(j=i;j<i+p;j++)
{
if(pat[count]==txt[j])
{
count++;
c++;
}
else
break;
}
if(c==p)
{

LOC=i+1;
break;
}
}
if(LOC!=0)
printf("Pattern found at location: %d",LOC);
else
printf("NOT FOUND\n");
}

预期:

Enter size of Text and Pattern

5 2

Enter the Text

abbca

Enter the Pattern

bc

Pattern found at location: 3

实际:

Enter size of Text and Pattern

5 2

Enter the Text

abb

Enter the Pattern

a

NOT FOUND

最佳答案

for(i=0;i<t;i++)
{
scanf("%c",&txt[i]);
}

如果您在输入文本时输入了太多字符,我的意思是超过t个字符的字符串,多余的字符将用于模式。另请注意\n 将在其中读取

为了在读取其他元素之后的字符串/字符时不出现这个问题,我鼓励您每次读取一行。

警告:pat 和 txt 的大小必须大一才能记住终止空字符

你可以这样做:

int main()
{
char *line;
size_t n;

printf("Enter size of Text and Pattern\n");
line = 0;
n = 0;
if (getline(&line, &n, stdin) == -1) {
puts("abort");
return -1;
}
if (sscanf(line, "%d %d",&t,&p) != 2) {
puts("invalid size of Text and Pattern");
return -1;
}
free(line);

char pat[p+1]; /* warning +1 */
char txt[t+1]; /* warning +1 */
char fmt[16];

printf("Enter the Text\n");
line = 0;
n = 0;
if (getline(&line, &n, stdin) == -1) {
puts("abort");
return -1;
}

sprintf(fmt, "%%%ds", t); /* makes the right format */
if ((sscanf(line, fmt, txt) != 1) || (strlen(txt) != t)) {
puts("txt is too small");
return -1;
}
free(line);

printf("Enter the Pattern\n");
line = 0;
n = 0;
if (getline(&line, &n, stdin) == -1) {
puts("abort");
return -1;
}
sprintf(fmt, "%%%ds", p); /* makes the right format */
if ((sscanf(line, fmt, pat) != 1) || (strlen(pat) != p)) {
puts("pat is too small");
return -1;
}
free(line);

int MAX=t-p+1;
...

执行:

Enter size of Text and Pattern
5 2
Enter the Text
abbca
Enter the Pattern
bc
Pattern found at location: 3

valgrind 下:

pi@raspberrypi:/tmp $ valgrind ./a.out
==5185== Memcheck, a memory error detector
==5185== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==5185== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==5185== Command: ./a.out
==5185==
Enter size of Text and Pattern
5 2
Enter the Text
abbca
Enter the Pattern
bc
Pattern found at location: 3==5185==
==5185== HEAP SUMMARY:
==5185== in use at exit: 0 bytes in 0 blocks
==5185== total heap usage: 5 allocs, 5 frees, 2,408 bytes allocated
==5185==
==5185== All heap blocks were freed -- no leaks are possible
==5185==
==5185== For counts of detected and suppressed errors, rerun with: -v
==5185== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 6 from 3)

注释:

  • 最好在打印位置后添加\n
  • 您使程序变得复杂,在读取它们之前询问文本和模式大小,您不需要在读取它们之前知道它们的大小,只需像 getline 那样使用堆中分配的内存而不是使用编译时未知的大小将它们放入堆栈

关于c - 为什么我的程序为模式匹配程序提供错误的输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54491907/

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