C ++搜索一个唯一的单词并为其分配一个数字

时间:2014-12-16 16:05:19

标签: c++ search count

我正在查看读取输入文件和计算单词出现次数的各种示例。然后给它一个变量来计算。

让我们说我们有一个输入文件,你想查找单词" account"或者说"喜欢"出现并给它变量" 1.2"。因此,当您找到该单词时,计算它出现的次数,然后将其乘以1.2。

你会怎么做呢?

编辑:这是我知道的唯一方式。但是,这会预先搜索单词而不是让用户搜索

#include <iostream>
#include <fstream>
using namespace std;

int main(void)
{
    int option=0; //option number
    ifstream inputFile;
    string filename;

    cout << "Welcome\n" << endl;

    //Getting the input file
    cout << "Enter input data file name:";
    cin >> filename;
    cout << endl;

    fin.open(filename.c_str()); // change to C-string

    if (!inputFile) {// makes sure file exist
            cout << "Unable to open " << filename << endl;
            cin.get();
            return 1;
            }

    do {
            cout << "5- Count frequency of the following three words individually: I, like, is" << endl;

            cout << "6 - To quit";
            cout << endl;
            cin >> option;

            int iWord = 0;
            int likeWord = 0;
            int isWord = 0;

             if (option == 5) {
                    string word;
                    do {
                            inputFile >> word;
                            if (word == "I") iWord++;
                            else if (word== "like") likeWord++;
                            else if (word == "is") isWord++;
                            }while (inputFile.good());
                    cout << "The word I is repeated " << iWord << " times" << endl;
                    cout << "The word is has been repeated " << isWord << " times" << endl;
                    cout << "The word like is repeated " << likeWord << " times" << endl << endl;
                    }
            inputFile.clear(); // clear fail bit or seekg won't work!
            inputFile.seekg (0, ios::beg);
    }while (option != 6);
    return 0;
    }

1 个答案:

答案 0 :(得分:0)

不是最有效的方式(特别是对于一个单词),但也许这可以给你指路:

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <algorithm>
#include <unordered_map>

void tokenize( const std::string & line, std::unordered_map< std::string, size_t > & map_count )
{
    for( std::string::size_type pos_start = 0, pos_end = 0; ( pos_end != std::string::npos ) && ( pos_start = line.find_first_of( "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", pos_end ) ) != std::string::npos; )
    {
         pos_end = line.find_first_not_of( "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", pos_start );
         std::string current_word = ( pos_end == std::string::npos ) ? line.substr( pos_start ) : line.substr( pos_start, pos_end - pos_start );
         std::unordered_map< std::string, size_t >::iterator iter( map_count.find( current_word ) );
         if( iter == map_count.end() )
             map_count.insert( std::pair< std::string, size_t >( current_word, 1 ) );
         else
             ++( iter->second );
    }
}

void countWordsInFile( const std::string & filename, std::unordered_map< std::string, size_t > & map_count )
{
    std::ifstream in( filename );
    std::string line;
    size_t count = 0;
    if( in.is_open() )
        while( std::getline( in, line ).good() )
            tokenize( line, map_count );
}

double countAndWeigh( const std::string & filename, const std::string & word, double weight )
{
    std::unordered_map< std::string, size_t > map_count;
    countWordsInFile( filename, map_count );
    std::unordered_map< std::string, size_t >::const_iterator iter( map_count.find( word ) );
    return ( iter != map_count.end() ) ? iter->second * weight : 0;
}  


int main( int argc, char ** argv )
{
    if( argc == 4 )
        std::cout << countAndWeigh( argv[1], argv[2], strtod( argv[3], nullptr ) ) << std::endl;
    return 0;
}