字数 - ignoring/subtracting 双空格
Word count - ignoring/subtracting double spaces
我正在尝试学习一些编码以扩大我的知识范围,但我似乎 运行 陷入了一个难题。
我正在尝试创建一个程序来输出从文件中读入的字符数、数字、标点符号、空格、单词和行数。
这是我正在阅读的文本文件。
See Jack run. Jack can run fast. Jack runs after the cat. The cat's fur is black. See Jack catch the cat.
Jack says, "I caught the cat."
The cat says, "Meow!"
Jack has caught 1 meowing cat. Jack wants 5 cats, but can't find any more.
这是我的代码
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream lab3;
string word;
lab3.open("lab3.txt");
int countletters=0,countnum=0,countpunc=0,countspace=0,words=0,line=0;
char character;
if(!lab3)
{
cout << "Could not open file" << endl;
return 1;
}
while(lab3.get(character) && !lab3.eof())
{
if(isalpha(character))
{
countletters++;
}
if (isdigit(character))
{
countnum++;
}
if (ispunct(character))
{
countpunc++;
}
if (isspace(character))
{
countspace++;
}
if (isalpha(character) && (isspace(character++) || ispunct(character++)))
{
words++;
}
if(character=='\n')
{
line++;
}
}
cout << "There are " << countletters << " letters." << endl;
cout << "There are " << countnum << " numbers." << endl;
cout << "There are " << countpunc << " punctuations." << endl;
cout << "There are " << countspace << " spaces." << endl;
cout << "There are " << words << " words." << endl;
cout << "There are " << line << " sentences." << endl;
lab3.close();
return 0;
}
输出:
There are 167 letters.
There are 2 numbers.
There are 18 punctuations.
There are 52 spaces.
There are 0 words.
There are 4 sentences.
我希望学习的一些东西:
- 解释为什么字数统计不起作用,并替换为考虑到双空格的有效代码。
- 关于改进我的学习代码的建议 purposes/efficiency。
- 从文本文件读取信息的说明。无论是字母、数字、标点符号,无论您在进行此类编码时 运行。
我知道的一些事情:
using namespace std;
不是好的做法 - 什么是实际应用程序的最佳做法。
- 我是初学者,这可能不是编码的精华
`提前感谢您的帮助和建议:)
我还不能发表评论,所以我发布了一个答案,但我知道这不是一个真正的答案,因为我正在向您介绍另一个话题。
像他们在这里做的那样计算白色 space 的匹配项会给你一个字数...
Count number of matches
您或许可以修改此解决方案以计算其他项目。
没有正则表达式...
由于您正在遍历字符,因此您可以设置一个标记来跟踪 spaces,并在到达另一个非 space.
时增加字数
我相信将其放入您的代码中以替换您的 wordcount 和 spacecount 部分应该有效。您需要在顶部添加一个名为 spaceflag 的整数。您可能需要将字数加一才能获得准确的总数。
if (isspace(character))
{
countspace++;
spaceflag = 1;
}
else //else says it's not a space.
{
if(spaceflag == 1) //if the spaceflag has been set and we run into a different type of character then we've made it to a new word.
words++;
spaceflag = 0; //reset the spaceflag until next one is found.
}
if(character=='\n')
因为您获取下一个字符的方法是 lab3.get(character)
,所以增加字数检查中的字符不会获取下一个字符,只会改变您拥有的字符的值。
与其尝试 'look ahead',不如考虑保留最后读取的字符并在下一次迭代中对其进行检查以检测单词的结尾。
if (ispunct(character))
{
计数符号++;
如果(isaalpha(prevchar))
{
单词++;
}<br>
}
如果(空间(字符))
{
计数空间++;
如果(isaalpha(prevchar))
{
单词++;
}<br>
}
在循环开始之前将 prevchar
初始化为零,然后在所有检查之后在循环内设置等于 character
。
另请注意,您的数字检查实际上是在捕获数字,因此数据中的值 10 将在输出中计为 2 个数字,而不是一个。
我正在尝试学习一些编码以扩大我的知识范围,但我似乎 运行 陷入了一个难题。
我正在尝试创建一个程序来输出从文件中读入的字符数、数字、标点符号、空格、单词和行数。
这是我正在阅读的文本文件。
See Jack run. Jack can run fast. Jack runs after the cat. The cat's fur is black. See Jack catch the cat.
Jack says, "I caught the cat."
The cat says, "Meow!"
Jack has caught 1 meowing cat. Jack wants 5 cats, but can't find any more.
这是我的代码
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream lab3;
string word;
lab3.open("lab3.txt");
int countletters=0,countnum=0,countpunc=0,countspace=0,words=0,line=0;
char character;
if(!lab3)
{
cout << "Could not open file" << endl;
return 1;
}
while(lab3.get(character) && !lab3.eof())
{
if(isalpha(character))
{
countletters++;
}
if (isdigit(character))
{
countnum++;
}
if (ispunct(character))
{
countpunc++;
}
if (isspace(character))
{
countspace++;
}
if (isalpha(character) && (isspace(character++) || ispunct(character++)))
{
words++;
}
if(character=='\n')
{
line++;
}
}
cout << "There are " << countletters << " letters." << endl;
cout << "There are " << countnum << " numbers." << endl;
cout << "There are " << countpunc << " punctuations." << endl;
cout << "There are " << countspace << " spaces." << endl;
cout << "There are " << words << " words." << endl;
cout << "There are " << line << " sentences." << endl;
lab3.close();
return 0;
}
输出:
There are 167 letters.
There are 2 numbers.
There are 18 punctuations.
There are 52 spaces.
There are 0 words.
There are 4 sentences.
我希望学习的一些东西:
- 解释为什么字数统计不起作用,并替换为考虑到双空格的有效代码。
- 关于改进我的学习代码的建议 purposes/efficiency。
- 从文本文件读取信息的说明。无论是字母、数字、标点符号,无论您在进行此类编码时 运行。
我知道的一些事情:
using namespace std;
不是好的做法 - 什么是实际应用程序的最佳做法。- 我是初学者,这可能不是编码的精华
`提前感谢您的帮助和建议:)
我还不能发表评论,所以我发布了一个答案,但我知道这不是一个真正的答案,因为我正在向您介绍另一个话题。
像他们在这里做的那样计算白色 space 的匹配项会给你一个字数...
Count number of matches
您或许可以修改此解决方案以计算其他项目。
没有正则表达式... 由于您正在遍历字符,因此您可以设置一个标记来跟踪 spaces,并在到达另一个非 space.
时增加字数我相信将其放入您的代码中以替换您的 wordcount 和 spacecount 部分应该有效。您需要在顶部添加一个名为 spaceflag 的整数。您可能需要将字数加一才能获得准确的总数。
if (isspace(character))
{
countspace++;
spaceflag = 1;
}
else //else says it's not a space.
{
if(spaceflag == 1) //if the spaceflag has been set and we run into a different type of character then we've made it to a new word.
words++;
spaceflag = 0; //reset the spaceflag until next one is found.
}
if(character=='\n')
因为您获取下一个字符的方法是 lab3.get(character)
,所以增加字数检查中的字符不会获取下一个字符,只会改变您拥有的字符的值。
与其尝试 'look ahead',不如考虑保留最后读取的字符并在下一次迭代中对其进行检查以检测单词的结尾。
if (ispunct(character))
{
计数符号++;
如果(isaalpha(prevchar))
{
单词++;
}<br>
}
如果(空间(字符))
{
计数空间++;
如果(isaalpha(prevchar))
{
单词++;
}<br>
}
在循环开始之前将 prevchar
初始化为零,然后在所有检查之后在循环内设置等于 character
。
另请注意,您的数字检查实际上是在捕获数字,因此数据中的值 10 将在输出中计为 2 个数字,而不是一个。