---------------------------------------------------------------------------------
虽然很笨的办法,却非常有效
---------------------------------------------------------------------------------
Pinyin.h
---------------------------------------------------------------------------------
#pragma onceclass CPinyin
{
public:
CPinyin(void);
~CPinyin(void);
private:
CStringArray m_pyDataArray;
CString m_DataFileName;
bool m_isLoad;public:
bool LoadData(void);
bool LoadData( CString strFileName );
CString toPinyin(CString szHanzi, int iMode=0, CString szSplit=_T(" "), CString szDuoyinziSplit=_T(",") );
};
---------------------------------------------------------------------------------
Pinyin.cpp
---------------------------------------------------------------------------------
#include "StdAfx.h"
#include "Pinyin.h"
CPinyin::CPinyin()
{
m_DataFileName = _T("PinYinData_Unicode.txt");
LoadData();
}CPinyin::~CPinyin()
{
m_pyDataArray.RemoveAll();
}bool CPinyin::LoadData( CString strFileName )
{
m_DataFileName = strFileName;
return LoadData();
}bool CPinyin::LoadData(void)
{
CFile f;
CString str = _T("");
int i = 0;
int icnt = 0;
m_isLoad = false;m_pyDataArray.RemoveAll();
if( f.Open( m_DataFileName , CFile::modeRead ) )
{
f.Seek( 2L, CFile::begin); //unicode text file keep FF FE
UINT size = (UINT)f.GetLength()-2;
TCHAR *pData = (TCHAR *)malloc( size );
if( pData != NULL )
{
f.Read( pData, size);
while(AfxExtractSubString(str,pData,i,','))
{
++i;
if( str.GetLength() > 0 )
{
str.Replace(_T(" "),_T(""));
str.Replace(_T("\r\n"),_T(""));
m_pyDataArray.Add ( str );
icnt ++;
}
}
free( pData );
}
f.Close();
}
if( icnt > 0 ) m_isLoad = true;
return m_isLoad;
}//iMode 0 不显示多音字
// 1 显示多音字
CString CPinyin::toPinyin(CString szHanzi, int iMode, CString szSplit, CString szDuoyinziSplit )
{
int i = 0, j = 0, p1 = 0;
CString strPY = _T(""), str, strOneLine, strTmp, strHZData = _T("");
int iLen = szHanzi.GetLength();
if( iLen = 0 )
{
strHZData = strOneLine.Right(strOneLine.GetLength() - p1);
}
if( strHZData.Find( str ) != -1 )
{
isFindPinyin = true;
p1 = strOneLine.Find(_T(":"));
if( p1 > 0 )
{
strTmp = strOneLine.Left( p1 );
if( iduoyingzi > 0 )
{
if( iMode == 1 )
{
if( strPY.GetLength() > 0)
strPY += szDuoyinziSplit + strTmp;
else
strPY += strTmp;
}
}
else
{
if( strPY.GetLength()
下载代码