C++英文单词统计小程序

//WorldTest.h

<span style="font-family:Courier New;font-size:10px;"><span style="font-family:Courier New;font-size:10px;">/*
//Function	: count world
//Date		: 2015-09-24
//Author	: pengl
*/

#ifndef _WORLD_TEST_H_
#define _WORLD_TEST_H_

#include <string>
#include <iostream>
#include <map>
#include <vector>
#include <iterator>
#include <numeric>
#include <algorithm>
#include <fstream>

#include <tchar.h>

#if defined(_UNICODE) || defined(UNICODE)
#define StringBase std::wstring
#else
#define StringBase std::string
#endif


class WorldTest
{
public:
	WorldTest();
	WorldTest(StringBase _str_input_file_name, StringBase _str_output_file_name);
	~WorldTest();

public:
	bool run();

private:
	bool stringSplit(const StringBase &_str_source, std::vector<StringBase> &_vec_dst, const TCHAR _c_flag = _T('\0'));
	bool readIn();
	bool writeOut();

//	bool ReadStringW(std::wstring& rString);

private:
	StringBase					m_str_input_file_name;
	StringBase					m_str_output_file_name;
	std::wifstream					m_infile;
	std::wofstream					m_outfile;
	std::map<StringBase, int>		m_map_world_count;
};


#endif</span></span>



//WorldTest.cpp

#include "WorldTest.h"

using namespace std;

WorldTest::WorldTest()
{

}

WorldTest::WorldTest(StringBase _str_input_file_name, StringBase _str_output_file_name)
: m_str_input_file_name(_str_input_file_name), 
m_str_output_file_name(_str_output_file_name)
{
	m_infile.open(m_str_input_file_name);
	if (!m_infile.is_open())
	{
		wcout << _T("input file open field!") << endl;
	}

	m_outfile.open(m_str_output_file_name);
	if (!m_outfile.is_open())
	{
		wcout << _T("output file open field!") << endl;
	}
}

WorldTest::~WorldTest()
{
	m_infile.close();
	m_outfile.close();
}

bool WorldTest::run()
{
	bool success = true;

	success &= readIn();
	success &= writeOut();

	return (success);
}

bool WorldTest::readIn()
{
	bool success = true;

	// 读取文件的每一行
	StringBase str_temp = _T("");
	map<StringBase, int>::iterator it_map_pos;
	while (!m_infile.eof())
	{
		vector<StringBase> vec_temp;
		getline(m_infile, str_temp);
		stringSplit(str_temp, vec_temp); // 对每一行数据进行分割处理
		for (int i = 0; i < vec_temp.size(); ++i)
		{
			it_map_pos = m_map_world_count.find(vec_temp[i]);
			if (it_map_pos == m_map_world_count.end())
			{
				m_map_world_count.insert(make_pair(vec_temp[i], 1)); // 保存数据
			}
			else
			{
				++(it_map_pos->second);
			}
		}

	}

	return (success);
}

bool WorldTest::writeOut()
{
	bool success = true;

	// 写出到文件count.txt
	map<StringBase, int>::iterator it_map_pos;
	int isum = 0;
	for (it_map_pos = m_map_world_count.begin(); it_map_pos != m_map_world_count.end(); ++it_map_pos)
	{
		isum += it_map_pos->second;
		m_outfile << it_map_pos->first << _T(": ") << it_map_pos->second << endl;
	}
	m_outfile << endl << _T("all count: ") << isum << endl;
	
	return (success);
}

// 分割字符串
bool WorldTest::stringSplit(
	const StringBase &_str_source, 
	vector<StringBase> &_vec_dst, 
	const TCHAR _c_flag
	)
{
	bool success = true;

	StringBase::const_iterator cit_pos1;
	StringBase::const_iterator cit_pos2;
	StringBase::const_iterator cit_pos;

	if (_str_source == _T(""))
	{
		return false;
	}

	if (_c_flag == _T('\0')) // 默认非字母的都是分隔符
	{
		auto auto_lam_find =
			[](TCHAR _ch)->bool
		{
			if ((_ch >= _T('a') && _ch <= _T('z'))
				|| (_ch >= _T('A') && _ch <= _T('Z')))
			{
				return false;
			}
			return true;
		};

		cit_pos1 = _str_source.begin();
		cit_pos2 = find_if(cit_pos1, _str_source.end(), auto_lam_find);
		while (cit_pos2 != _str_source.end())
		{
			StringBase str_temp(cit_pos1, cit_pos2);
			if (str_temp != _T(""))
			{
				_vec_dst.push_back(str_temp);
			}
			cit_pos1 = cit_pos2 + 1;
			cit_pos2 = find_if(cit_pos1, _str_source.end(), auto_lam_find);
		}
		if (cit_pos1 != _str_source.end())
		{
			StringBase str_temp(cit_pos1, cit_pos2);
			if (str_temp != _T(""))
			{
				_vec_dst.push_back(str_temp);
			}
		}
	}
	else // 使用参数作为分隔符
	{
		auto auto_lam_find =
			[&_c_flag](TCHAR _ch)->bool
		{
			if (_ch == _c_flag)
			{
				return true;
			}
			return false;
		};

		cit_pos1 = _str_source.begin();
		cit_pos2 = find_if(cit_pos1, _str_source.end(), auto_lam_find);
		while (cit_pos2 != _str_source.end())
		{
			StringBase str_temp(cit_pos1, cit_pos2);
			if (str_temp != _T(""))
			{
				_vec_dst.push_back(str_temp);
			}
			cit_pos1 = cit_pos2 + 1;
			cit_pos2 = find_if(cit_pos1, _str_source.end(), auto_lam_find);
		}
		if (cit_pos1 != _str_source.end())
		{
			StringBase str_temp(cit_pos1, cit_pos2);
			if (str_temp != _T(""))
			{
				_vec_dst.push_back(str_temp);
			}
		}

	}

	return (success);
}




//main.cpp

#include "WorldTest.h"

// 若要程序支持unicode字符集,请注意在程序中使用wstring字符串
// 并在所有常量字符或字符串前加上_T()
// vs默认工程属性为unicode字符集。

int main()
{
	WorldTest wt(_T("world.txt"), _T("count.txt"));
	wt.run();
}



运行截图:



作者: http://blog.csdn.net/lp310018931

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值