⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pinyin.cpp

📁 汉字转拼音程序
💻 CPP
字号:
//
//	File name: 	Pinyin.cpp : implementation file
//	Created:	2005/08/11   11:8:2005   12:55
//	author:		xielin
//
//	Purpose:
//	汉字转拼音
//
#include "stdafx.h"
#include "Pinyin.h"
#include <fstream>
#include <cassert>
#include <map>
typedef map<string,int> STRING2INT;
STRING2INT g_spellMap;

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

int GetCnAscii(int first, int second)
{
	int hightByte = 256 + first;
	int lowByte = 256 + second;
	int ascii = (256 * hightByte + lowByte) - 256 * 256;

	return ascii;
}

CPinYin::CPinYin()
{

}

CPinYin::~CPinYin()
{

}

///
// Function name: Initialize()
// Description	: 初始化拼音查找表
// Input:
//	lut	-	查找表文件
// Output:
//	None

bool CPinYin::Initialize(const char * lut)
{
	if (!g_spellMap.empty()) g_spellMap.clear();

	ifstream fs;
	fs.open (lut, ios::in);	if (!fs.is_open()) return false;
	string key;	int value;
	while ( fs >> key >> value )
	{
		g_spellMap.insert (STRING2INT::value_type (key,value));
	}

	fs.close();

	return true;
}

//根据ascii码值获得拼音
string CPinYin::GetSpellByAscii(int ascii)
{
	string sResult;

	if(ascii > 0 && ascii < 160)
	{ //ascii character
		sResult = ascii;
		return sResult;
	}

	if(ascii < -20319 || ascii > -10247)
	{ //unknow character
		sResult = "";
		return sResult;
	}

	STRING2INT::iterator iter = g_spellMap.begin();
	STRING2INT::iterator iter2 = iter;
	string key;
	int val;
	while (iter != g_spellMap.end())
	{
		key = iter->first;
		val= iter->second;
		if (ascii == val)
			return key;
		else if (ascii < val)
		{
			break;
		}
		iter2 = iter;
		iter++;
	} //while

	sResult = iter2->first;

	return sResult;
}

//获得单个汉字的拼音
string	CPinYin::GetSpell(const char * ch)
{
	assert (ch && (ch+1));

	int ascii = GetCnAscii (ch[0], ch[1]);
	return GetSpellByAscii (ascii);

}

//获得整个字符串的拼音
string CPinYin::GetFullSpell(const char *szIn)
{
	string sRet = "";

	int len = strlen (szIn);
	for (int i=0; i < len; i++)
	{
		int ascii = szIn[i];
		if (ascii > 0 && ascii < 160)
		{
			sRet += GetSpellByAscii (ascii);
		}
		else if (ascii < 0)
		{
			int ascii = GetCnAscii (szIn[i], szIn[i+1]);
			sRet += GetSpellByAscii (ascii);
			i++;
		}
	}

	return sRet;
}

⌨️ 快捷键说明

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