【问题标题】:debugging c++ program with <set> containers使用 <set> 容器调试 c++ 程序
【发布时间】:2016-10-22 19:58:27
【问题描述】:

问题是找到一个句子中所有偶数词的所有元音,换句话说,这些元音必须在句子中的任何偶数词中遇到。 但是我当我输入例如:“ewedyua aiuye dswidje ieuayj eeee eeeui dajhdfjcne aodijsbfe”。 我得到:你我 但是“e i”是预期的,因为最后一个偶数词不包含“u”(我在文本中使用“”只是为了分隔,不要在输出中使用它们)

程序:

int main(){
string str; 
char ch = ' '; 
set<char> strSet; 
set<char> resultSet;
set<char> tempSet;
int count = 1;
int i = 0;

cout << "Enter a line: ";
getline(cin, str);
str = delOverSpace(str); // delete excessive gaps<br>
do {
 ch = str.at(i);
 if(((count % 2) == 0) && (ch != ' ')){  // this is an even word and not a gap
  if(isVowel(upperToLower(ch)))  // this is a vowel
    tempSet.insert(upperToLower(ch));   
  }

  if (ch == ' ') {   // if we've passed through the word add inforamtion on it
   if(((count % 2) == 0) && (count / 2) == 1)
    strSet.insert(tempSet.begin(), tempSet.end());
   else if (((count % 2) == 0) && (count / 2) != 1){
    set_intersection(
     strSet.begin(),strSet.end(), tempSet.begin(), tempSet.end(),
     insert_iterator<set<char> >(resultSet, resultSet.begin())
    );

   strSet.clear();
   tempSet.clear();
   strSet.insert(resultSet.begin(), resultSet.end());
   resultSet.clear();
  }
  count++;
 }

 i++;
}while(ch != '.'); 

if (count == 2) cout << "Only one word was entered" << endl;
else if (strSet.empty()) cout << "No vowels were found" << endl;
else {

copy(strSet.begin(), strSet.end(), ostream_iterator<char>(cout, " ")); 
cout << endl;
}

return 0;
}

【问题讨论】:

  • 调试器是解决此类问题的正确工具。 询问 Stack Overflow 之前,您应该逐行逐行检查您的代码。如需更多帮助,请阅读How to debug small programs (by Eric Lippert)。至少,您应该 [编辑] 您的问题,以包含一个重现您的问题的 Minimal, Complete, and Verifiable 示例,以及您在调试器中所做的观察。
  • 在调试器中单步执行代码时,通常有助于将单独的语句放在单独的行上,否则调试器可能会跳过实际执行的语句。我说的是你的if (...) cout &lt;&lt; ... 行。这些并不是那么糟糕,因为您会看到输出,但是如果这些单行代码不包含输出,那么调试起来就会困难得多。这些行并没有错,它们只是让调试过程变得更加困难。
  • 我明白了,谢谢你的帮助。

标签: c++ debugging set


【解决方案1】:

尽量不要将一个计数器变量用于多种用途。 例如,您可以将count 用于当前单词中的当前字符计数,并使用words 用于单词计数。 在您的示例中,您让count 工作太辛苦了。

决定你要数什么。你想计算每个字符吗? 还是要统计当前单词的字符数?

当增加一个计数器时,试着把它放在需要它的逻辑旁边。 例如,i++ 应该在您使用它获取下一个字符后立即使用。 并且count 应该在处理字母的块中递增(即不是空格,不是点)。 如果计数器距离它们所跟踪的内容太远,您可能会引入错误。

首先移动空格检查并使用.检查句尾 清晰的缩进有助于遵循代码 - 我使用 4 个字符,但 2 或 3 个也可以。 1 个字符的缩进很难阅读。

使用空行分隔不同的逻辑块。

您可能需要添加逻辑来处理标点符号,例如 ,?! 等。

当您处理一个被分成单词的输入字符串时,在循环外处理输入结束条件通常更容易。 在这种情况下,循环在拾取最后一个字母后退出。 如果那不是.,那么您将在tempSet 中有一个未处理的单词。 您可以尝试在循环中预测这一点,但退出循环并检查是否还有任何需要处理的内容似乎更自然。

如果你将你的逻辑分成清晰的块,你可以在每个块内设置断点。 然后你就会知道你正在处理什么情况。您可以打破字尾逻辑并检查tempSetcount。 他们是你所期望的吗? 在else 部分,您可以检查ch 进入并验证它是否正确归类为元音。

int main() {

    string str; 
    char ch = ' '; 
    set<char> strSet; 
    set<char> tempSet;

    int count = 0, words = 0, i = 0;
    cout << "Enter a line: ";
    getline(cin, str);
    str = delOverSpace(str); // delete excessive gaps<br>

    while ( i < str.size() && ch != '.' ) {

        // Get next character
        ch = str.at(i);
        i++;

        if (ch == ' ' || ch == '.' ) {   // if we've passed through the word add inforamtion on it
            if( count > 0 && ((count % 2) == 0)) {
                 strSet.insert(tempSet.begin(), tempSet.end());
                 words++;
            }
            count = 0; // reset the char counter
            tempSet.clear();    // clear the temp vowel set
        }
        else {
            // letter - not a space or a dot, increase the character count
            count++;
            if(isVowel(upperToLower(ch)))  // this is a vowel
                tempSet.insert(upperToLower(ch));   
        }
    }
    // pick up any unprocessed word if we hit end of input without seeing '.'
    if (count > 0 && ((count % 2) == 0)) {
        strSet.insert(tempSet.begin(), tempSet.end());
        words++;
    }

    if (words == 0) 
        cout << "No words entered" << endl;
    else if (words == 1) 
        cout << "Only one word was entered" << endl;
    else if (strSet.empty()) 
        cout << "No vowels were found" << endl;
    else {
        copy(strSet.begin(), strSet.end(), ostream_iterator<char>(cout, " ")); 
        cout << endl;
    }
    return 0;
}

【讨论】:

  • John D,非常感谢您的回答,这真的很有帮助
猜你喜欢
  • 1970-01-01
  • 2021-08-14
  • 1970-01-01
  • 2011-06-07
  • 1970-01-01
  • 1970-01-01
  • 2017-10-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多