计算单词出现次数

时间:2014-05-18 17:52:28

标签: c++

在我的程序中,我读取文本文件然后小写全部并单独拆分它们并附加到字符串。

using namespace std;
int main() {

int lines=0,i=0,k=0;
FILE *pfile=fopen("myfile.txt", "r");
char line2[500];
char * tok;
string alltext[999];
string words[999];
if(!pfile){printf("Error\n");return 1;}

std::string line;
    std::ifstream myfile("eriala.txt");
    while (std::getline(myfile, line))
        ++lines;

for(i=0;i<lines;i++){

    fgets(line2,sizeof(line2),pfile);
    char * str = line2;
    for (int i =0;i<strlen(line2); ++i){
    line2[i]=tolower(line2[i]);}
    tok = strtok(str," ,.!\n\t():;-");
    while(tok !=NULL){
        alltext[k].append(tok);
        alltext[k].append("\n");
        tok=strtok(NULL," ,.!\n\t():;-");

        k++;}}

for(int i=0;i<k;i++){
    int amount=0;
    for(int j=0;j<k;j++){
        if(strcmp(alltext[i].c_str(),alltext[j].c_str())==0)
            amount++;}
    } 
}

我需要做的是计算一个单词出现在文本文件中的次数。它有点那样做。但我需要它们按降序显示。

1 个答案:

答案 0 :(得分:1)

这是一个完整的C ++ 11版本,它对单词进行计数并按降序打印它们:

#include <algorithm>
#include <cctype>
#include <fstream>
#include <iostream>
#include <map>
#include <string>

int main ()
{
    std::ifstream file ("myfile.txt"); // Try to open the file

    if (!file)
    {
        std::cerr << "Error: Cannot open file myfile.txt" << std::endl;
        return 1;
    }

    std::map<std::string, std::size_t> word_counter;

    std::string word;
    while (file >> word) // While a word can be read
    {
        // Remove every character that is not an alphanumeric one
        word.erase(std::remove_if(std::begin(word), std::end(word), [](char c) { return !::isalnum(c); }), std::end(word));
        if (!word.empty())
        {
            // Lower case the word
            std::transform(std::begin(word), std::end(word), std::begin(word), ::tolower);
            // Increment the counter for this word
            word_counter[word]++;
        }
    }

    // Print the counted words in descending order
    for (auto it = word_counter.crbegin() ; it != word_counter.crend() ; ++it)
        std::cout << it->first << " => " << it->second << std::endl;
}