agudbd 2023-11-22 01:52 采纳率: 33.3%
浏览 26
已结题

c++ 代码小问题,请看下啊

问题见代码中的注释,小问题
看了看下面的回答,都是AI,gpt能解决,我来这里干什么?给我气笑了,就没人认真回答下吗?
问题解决了,在初始化t数组是最后一位没有加上'\0',虽然不知道这样为什么会出错,但也比下面回答中一直用chatgpt的答案浪费时间强

#include <bits/stdc++.h>
#define MAXSIZE 10000
using namespace std;

int BF(const char* s, const char* t,int pos)//BF算法函数
{//匹配到返回位置,匹配不到返回-1
    int i = pos;
    int j = 0;
    while (i < strlen(s) && j < strlen(t))/
    {
        if (s[i] == t[j]) { i++; j++; }
        else { i = i - j + 1; j = 0; }
    }
    if (j >= strlen(t))
    {
        return i - j;
    }
    else return -1;
}


bool EntityRecognition(const char* S, const char* T) {
   char t[6]={0};
   int sum=0;
if(T[0]!='*')
{
for(int i=0;i<6;i++)
{
t[i]=T[i];
}
for(int i=0;i<strlen(T);i++)
{
if(T[i]=='*')
{
    sum++;
}
}
int pos=0;
int pos1;
//cout<<BF(S,t,0);
while(BF(S,t,pos)!=-1)
{
    pos1=BF(S,t,pos);
for(int i=pos1+6;i<=pos1+6+3*sum-1;i++)
{
cout<<S[i];
}
cout<<endl;
cout<<BF(S,t,0);//问题是在这里为什么输出的会是-1呢?在已经进入了这个循环的情况下
pos=pos1+6+3*sum;
cout<<BF(S,t,pos);//这里为什么输出的也是-1?
}
//cout<<BF(S,t,0);
if(BF(S,t,0)==-1)
{return false;}
}

return true;
}
  • 写回答

9条回答 默认 最新

  • 叫兽-郭老师 新星创作者: Java技术领域 2023-11-22 09:18
    关注

    由人工智能和答主提供,可以参考如下,如果回答的不正确,及时评论区回复,我追加回答,谢谢。
    这段代码中存在一些问题,我将逐一解释并提供修改后的代码。

    问题1:在while(BF(S,t,pos)!=-1)循环中,cout<<BF(S,t,0);cout<<BF(S,t,pos);的输出结果都是-1,这是因为在进入循环后,pos的值已经发生了变化,所以在下一次循环时,pos的值不再是初始值0。为了解决这个问题,你可以在循环开始前将pos的值保存到一个变量中,然后在循环中使用这个变量。

    问题2:在for(int i=pos1+6;i<=pos1+6+3*sum-1;i++)循环中,你试图访问字符串S的索引i,但是没有检查i是否超出了字符串的长度。如果i超过了字符串的长度,这将导致未定义的行为。为了解决这个问题,你需要在访问S[i]之前检查i是否小于字符串的长度。

    以下是修改后的代码:

    #include <bits/stdc++.h>
    #define MAXSIZE 10000
    using namespace std;
    
    int BF(const char* s, const char* t, int pos) {
        int i = pos;
        int j = 0;
        while (i < strlen(s) && j < strlen(t)) {
            if (s[i] == t[j]) { i++; j++; }
            else { i = i - j + 1; j = 0; }
        }
        if (j >= strlen(t)) {
            return i - j;
        }
        else return -1;
    }
    
    bool EntityRecognition(const char* S, const char* T) {
        char t[6] = {0};
        int sum = 0;
        if (T[0] != '*') {
            for (int i = 0; i < 6; i++) {
                t[i] = T[i];
            }
            for (int i = 0; i < strlen(T); i++) {
                if (T[i] == '*') {
                    sum++;
                }
            }
            int pos = 0;
            int pos1;
            int init_pos = pos; // Save the initial value of pos
            while (BF(S, t, pos) != -1) {
                pos1 = BF(S, t, pos);
                for (int i = pos1 + 6; i <= pos1 + 6 + 3 * sum - 1 && i < strlen(S); i++) { // Check if i is within the length of S
                    cout << S[i];
                }
                cout << endl;
                pos = init_pos; // Use the saved value of pos
                cout << BF(S, t, pos);
            }
            if (BF(S, t, 0) == -1) { return false; }
        }
    
        return true;
    }
    

    注意:这段代码仍然存在一些问题,例如它可能无法正确地处理所有的输入情况。如果你能提供更多关于这段代码应该做什么的信息,我可能会提供更具体的帮助。

    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 11月22日
  • 修改了问题 11月22日
  • 修改了问题 11月22日
  • 创建了问题 11月22日