testwordcount.cpp

来自「一个很好的计算文字数目的例子,现在一个缺点就是计算中文的时候可能不是很准确」· C++ 代码 · 共 72 行

CPP
72
字号
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include "wordcount.h"


using namespace std;//::cout;

typedef vector<string>::size_type vec_sz;

bool output_already(const string& word, const vector<string>& output_words)
{
    vec_sz size = output_words.size();
    for(vec_sz i = 0; i < size; ++i)
	{
		if(word == output_words[i]) 
			return true;
	}
    return false;
}

void main()
{
	wordcount counter;
	string word;
	string qut="/#";
    vector<string> words;
    vector<string> output_words;

	fstream file;

	
	cout<<"input words:";
	for ( ; ; )
		if ( counter.readin() )
			counter.count();
		else
		{
			counter.count();
			break;
		}

	file.open("data.txt",ios::in);

	while( file >> word) 
	{	
		if (word!=qut)
			words.push_back(word);
		else
			break;
	}

	const vec_sz words_size = words.size();
    for(vec_sz i = 0; i < words_size; ++i)
    {
        if (output_already(words[i], output_words)) continue;
              
        int count = 0;
        for(vec_sz j = 0; j < words_size; ++j)
            if(words[i] == words[j]) ++count;

        cout << "There are/is " << count << " \"" << words[i] << "\" in your input." << endl;
        output_words.push_back(words[i]);
    }

	file.close();

	cout<<" total "<<counter.getcount()<<" words "<<endl;

	system("pause");
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?