2017-01-24 22 views
0

我正在尝试学习一些编码来扩大我的知识范围,而且我似乎遇到了一些难题。字数统计 - 忽略/减去双倍空间

我想创建一个程序来输出从文件中读入的字符,数字,标点符号,空格,单词和行的数量。

这里是文本文件,我在读

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. 

有些事情我希望学习:

  1. 一个解释为什么字数不起作用,并替换考虑双空格的工作代码。
  2. 对我的学习目的/效率的代码进行改进的建议。
  3. 从文本文件中读取信息的说明。无论你的字母,数字,标点符号是什么,都可能会遇到这种类型的编码。

有些事情我是知道的:

  1. using namespace std;是不是好的做法 - 什么是真实世界应用的最佳实践。
  2. 我是一个初学者,这可能不是绝对不是作物编码的奶油提前

`谢谢你的帮助和建议:)

+0

不知道其他的东西,但添加命名空间之前的单词像:std :: string更好,因为不同的命名空间可以使用相同的单词。 /建议改进我的学习目的/效率的代码/应该在codereview(http://codereview.stackexchange.com/)上询问 –

+0

为什么'character'增加了,更不用说隐藏在'if '? – chris

+0

你的输出是什么,你期望得到什么? –

回答

0

因为你获得下一个字符的方法是通过lab3.get(character),增加你的字数检查字符是不会得到下一个字符,只改变字符的值你有。

与其试图“向前看”,考虑保留最后一个字符读取并检查它,以检测下一次迭代中字的结尾。

if (ispunct(character)) { countpunc++; if (isaalpha(prevchar)) { words++; }
} if (isspace(character)) { countspace++; if (isaalpha(prevchar)) { words++; }
}

prevchar初始化为零循环开始之前,然后设置为character毕竟支票的循环中。

另请注意,您的数字检查功能确实可以捕获数字,因此您的数据中的值10会在您的输出中计为2个数字,而不是一个。

+0

非常感谢,该代码修复了结果。 我会修改数字检查以解释超过单个数字的数字。 非常感谢。 – XmalevolentX

+0

您可以通过创建变量“in_word”,“in_numb”,“in_punct”,“in_space”来解决所有这些问题。以所有值开始FALSE。如果遇到的第一个字符是alpha,则将in_word更改为TRUE。继续检查,直到下一个字符不再是alpha,然后将in_word更改为FALSE,增加字数,并根据您找到的字符开启适当的其他变量。只有当这些变量改变状态时才增加字数,数量等。 –

0

我无法评论,但所以我发布一个答案,但我知道这不是一个真正的答案,因为我把你引向另一个线程。

计数的比赛为空白像他们在这里会给你一个字计数......

Count number of matches

你也许可以修改这个解决方案来算其他项目也是如此。

没有正则表达式... 既然你循环的字符,你可以设置一个标志,记录空间,并增加字数,当它到达另一个非空间。

我相信把它放在你的代码中来替换你的wordcount和你的spacecount部分应该可以工作。您需要将int添加到名为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')

+0

我不熟悉'正则表达式“目前,在我开始之前,我宁愿坚持基本编码ncorporating内置功能。 – XmalevolentX