【发布时间】: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 << ...行。这些并不是那么糟糕,因为您会看到输出,但是如果这些单行代码不包含输出,那么调试起来就会困难得多。这些行并没有错,它们只是让调试过程变得更加困难。 -
我明白了,谢谢你的帮助。