提问人:Colin Schmitt 提问时间:2/16/2023 最后编辑:Alan BirtlesColin Schmitt 更新时间:2/16/2023 访问量:43
C++ 输出给出不正确的数量 [重复]
C++ Output giving incorrect amount [duplicate]
问:
我正在尝试编写一个程序来计算单词数和句子数,然后取这些句子的平均值。它接受用户的输入,并在看到“@@@”时停止接受输入。我的代码如下:
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <string>
using namespace std;
int main() ///Main
{
//1. Declaration of variables
string text;
double word_count = 0.0;
double sentence_count = 0.0;
double avg_words_per_sentence;
//2. Prompt user input
cout << "Enter a paragraph..." << endl;
getline(cin, text, '@'); //3. Use getline to read whole paragraph and not first word entered
//4. Loop to read through text inputted and determine number of words and sentences
for (int i = 0; i < text.length(); i++)
{
if (text[i] == ' ')
{
word_count++;
}
else if (text[i] == '.' || text[i] == '!' || text[i] == '?')
{
sentence_count++;
}
}
//5. Provides error if there is no text
if (word_count == 0 && sentence_count == 0)
{
cout << "Word count: " << word_count << endl;
cout << "Sentence count: " << sentence_count << endl;
cout << "You did not enter any text!" << endl;
}
//6. Provides an error if there are no sentences
else if (sentence_count == 0)
{
cout << "Word count: " << word_count + 1 << endl;
cout << "Sentence count: " << sentence_count << endl;
cout << "You did not enter any sentences!" << endl;
}
//7. Calculates and outputs word/sentence count and average # of words
else
{
word_count++;
avg_words_per_sentence = word_count / sentence_count;
cout << "Word count: " << word_count << endl;
cout << "Sentence count: " << sentence_count << endl;
cout << "Average words per sentence: " << fixed << setprecision(1) << avg_words_per_sentence << endl;
}
return 0;
}
例如:如果我使用输入:
ttt
@@@
我以为我的字数是 1,但我被告知是 2。我不确定为什么它数 2。
我尝试调整某些 if/then 语句,例如字符串是否包含“@”以不计算单词或仅在正确字符后有空格时计算单词,但我仍然遇到相同不正确的单词数量
答:
0赞
Douglas B
2/16/2023
#1
对于初学者来说,您不需要将单词和句子计数存储为双精度值,这将导致您以后在条件比较中出现问题:永远不应该对非整数类型执行此操作。if (word_count == 0 && sentence_count == 0)
你的另一个“问题”是你自己做的,你最后有 3 个案例:
if (word_count == 0 && sentence_count == 0)
{
cout << "Word count: " << word_count << endl;
cout << "Sentence count: " << sentence_count << endl;
cout << "You did not enter any text!" << endl;
}
一切都符合预期(假设并且不是浮点)word_count
sentence_count
else if (sentence_count == 0)
{
cout << "Word count: " << word_count + 1 << endl;
cout << "Sentence count: " << sentence_count << endl;
cout << "You did not enter any sentences!" << endl;
}
当句子数为 0 但字数不为 0 时,将计数的字数加 1。这就是导致您描述的“问题”的原因。您可以使用以下方法执行类似操作:
else
{
word_count++;
avg_words_per_sentence = word_count / sentence_count;
cout << "Word count: " << word_count << endl;
cout << "Sentence count: " << sentence_count << endl;
cout << "Average words per sentence: " << fixed << setprecision(1) << avg_words_per_sentence << endl;
}
当您获得非零字数/句子数时,通过将字数增加 1。
我在那里看到了你的意图,但我认为要获得你想要的行为,你只想解释一个没有标点符号的单词,正如我添加的那样,去掉随机的 +1 字数,并使用整数进行比较:
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <string>
using namespace std;
int main() ///Main
{
//1. Declaration of variables
string text;
int word_count = 0;
int sentence_count = 0;
double avg_words_per_sentence;
//2. Prompt user input
cout << "Enter a paragraph..." << endl;
getline(cin, text, '@'); //3. Use getline to read whole paragraph and not first word entered
//4. Loop to read through text inputted and determine number of words and sentences
for (int i = 0; i < text.length(); i++)
{
if (text[i] == ' '|| text[i] == '\n')
{
word_count++;
}
else if (text[i] == '.' || text[i] == '!' || text[i] == '?')
{
sentence_count++;
}
}
//5. Provides error if there is no text
if (word_count == 0 && sentence_count == 0)
{
cout << "Word count: " << word_count << endl;
cout << "Sentence count: " << sentence_count << endl;
cout << "You did not enter any text!" << endl;
}
//6. Provides an error if there are no sentences
else if (sentence_count == 0)
{
cout << "Word count: " << word_count << endl;
cout << "Sentence count: " << sentence_count << endl;
cout << "You did not enter any sentences!" << endl;
}
//7. Calculates and outputs word/sentence count and average # of words
else
{
avg_words_per_sentence = word_count / sentence_count;
cout << "Word count: " << word_count << endl;
cout << "Sentence count: " << sentence_count << endl;
cout << "Average words per sentence: " << fixed << setprecision(1) << avg_words_per_sentence << endl;
}
return 0;
}
请注意,这仍然不是一个完美的方法,因为多个换行符将计为单词。对于任何更高级的东西,您可能需要研究状态机类型方法(例如,跟踪您是否在换行符之前遇到过字符,然后只计算单词)。此版本的一些示例输出如下:
Enter a paragraph...
ttt
@@@
Word count: 1
Sentence count: 0
You did not enter any sentences!
Enter a paragraph...
ttt hehehe.
@@@
Word count: 2
Sentence count: 1
Average words per sentence: 2.0
Enter a paragraph...
ttt heheh
@@@
Word count: 2
Sentence count: 0
You did not enter any sentences!
Enter a paragraph...
@@@
Word count: 2
Sentence count: 0
You did not enter any sentences!
评论
0赞
Ted Lyngmo
2/16/2023
添加了测试用例: - 没有空格,没有换行符。之前有多少个单词?ttt@@@
@
0赞
Douglas B
2/16/2023
@TedLyngmo你会很高兴地知道,这是留给读者的练习,也超出了本章:)的范围
评论
Word count: 0
Sentence count: 0
You did not enter any text!
ttt\n
2
word_count++;