简体   繁体   English

strpbrk 不起作用

[英]strpbrk doesn't work

char operators[] = "+-*/^(";  
    char* input = new char[100];  
    char* output = new char[100];  
    char* operadores = new char[100];  
    char* pch = input;  
    char* pch2 = input;  
    cout << "Expresion: " <<endl; cin.getline(input,100);  
    cout << input <<endl;  
    pch2 = strpbrk (pch2, operators);  
    pch = strtok (pch, "+-*/^(");  
    while (pch != NULL){  
        strcat (output, pch);  
        pch = strtok (NULL, "+-*/^(");  
        strcat (operadores, pch2);  
    }  

    cout << "Salida: " << output <<endl;
    cout << "Operadores: " << operadores <<endl;
    cout << "Entrada: " << input <<endl;
    cout << "pch2 = " << pch2 <<endl;

Hi, my problem is that the function strpbrk doesnt work, it doesn't return NULL.嗨,我的问题是 function strpbrk 不起作用,它不返回 NULL。 I've proved it, But I need a char to put in a stack.我已经证明了,但是我需要一个字符来放入堆栈。 and cout doesn't show me what character is pointed by pch2.并且 cout 没有告诉我 pch2 指向什么字符。

You are confusing yourself - the program is designed to confuse.你自己弄糊涂了——这个程序就是用来弄糊涂的。

You have both pch and pch2 pointing at the same input string - input . pchpch2都指向同一个输入字符串 - input You call strpbrk() to find one of the operators, saving that position in pch2 .您调用strpbrk()来查找其中一个运算符,将 position 保存在pch2中。 You then call strtok() on pch , and it finds the character that strpbrk() just found, and writes a NUL '\0' over it.然后在pch上调用strtok() ,它会找到strpbrk()刚刚找到的字符,并在其上写入 NUL '\0' So, it appears that pch2 points to the NUL at the end of a string.因此, pch2似乎指向字符串末尾的 NUL。 In the body of the loop, you then concatenate the empty string that pch2 points to onto your target list of operators.然后,在循环体中,将pch2指向的空字符串连接到目标运算符列表中。

Personally, I avoid using strtok() precisely because it mangles the input string.就个人而言,我避免使用strtok()正是因为它会破坏输入字符串。 If you are going to use it, you are likely to need to work on a duplicate of the string, because strtok() writes NUL bytes over it.如果要使用它,您可能需要处理字符串的副本,因为strtok()会在其上写入 NUL 字节。

Your diagnostic output at the end should show that the first section of the input - up to the first operator - only.您最后的诊断 output 应该显示输入的第一部分 - 直到第一个操作员 - 仅限。

Beware of casting aspersions at standard library functions or compilers 'not working'.谨防对标准库函数或编译器“不工作”进行诽谤。 It is the mark of a tyro;它是泰罗的标志; 99.9999% of the time, it is a user error and not a system error. 99.9999% 的时间,这是用户错误而不是系统错误。 On those very, very, very rare occasions when you're correct (ooh, look - I've just won my third multi-million lottery prize; that's more likely, even though I don't buy lottery tickets), the way that you describe the problem is different.在那些非常、非常、非常罕见的情况下,你是正确的(哦,看 - 我刚刚中了第三个数百万彩票大奖;即使我不买彩票,这种可能性也更大),这样你描述的问题不一样。 You describe the issue as one of utter astonishment;你把这个问题描述为完全惊讶之一; you document the working test cases;您记录工作测试用例; then you explain how the edge case you've found should work and the result - and you still aren't sure whether it is a bug in your code or in the system.然后你解释你发现的边缘情况应该如何工作以及结果 - 你仍然不确定它是你的代码还是系统中的错误。


As others diagnosed, you do not initialize operadores to an empty string, so concatenating to it leads to undefined behaviour.正如其他人所诊断的那样,您不会将operadores为空字符串,因此连接到它会导致未定义的行为。

There's really no need to allocate the 100-byte strings:真的没有必要分配 100 字节的字符串:

char input[100];  // Cleaner, simpler, more reliable

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM