漢字簡體轉正體類及測試程式

做了一個將簡體漢字轉化成正體漢字的類ChnTrans,轉化賴字典文件。

其中用Open函式打開字典文件(有FILE *版本和CFile版本);用TransChar轉化一個漢字;Trans轉化一個字符串。

 

ChnTrans.h

文件內容:

  1. #pragma once
  2. #define UNICODE_FILE_FLAG 0xFEFF
  3. #define MAX_UNICODE_FILE_SIZE 65535
  4. class CChnTrans
  5. {
  6. private:
  7.     TCHAR * pBuffer;
  8.     //TChar字符計數
  9.     UINT uiBufferTCharCount;
  10.     //CFile版本
  11.     //BOOL IsUnicodeFile(CFile * const poFile);
  12.     //FILE *版本
  13.     BOOL IsUnicodeFile(FILE * pFile);
  14. public:
  15.     CChnTrans(void);
  16.     ~CChnTrans(void);
  17.     BOOL Open(const CString * const pFileName);
  18.     BOOL TransChar(TCHAR * const pTCh);
  19.     BOOL Trans(CString * const pstrLine);
  20. };

 

ChnTrans.cpp

文件內容:

  1. #include "StdAfx.h"
  2. #include "ChnTrans.h"
  3. CChnTrans::CChnTrans(void)
  4. {
  5.     pBuffer = NULL;
  6.     uiBufferTCharCount = 0;
  7. }
  8. CChnTrans::~CChnTrans(void)
  9. {
  10.     free(pBuffer);
  11.     pBuffer = NULL;
  12.     uiBufferTCharCount = 0;
  13. }
  14. #if 0
  15. //開啟文件函式,CFile版本
  16. BOOL CChnTrans::Open(const CString * const pFileName)
  17. {
  18.     if(NULL == pFileName)
  19.     {
  20.         return FALSE;
  21.     }
  22.     CFile foChineseMap;
  23.     if(TRUE != foChineseMap.Open(*pFileName, CFile::modeRead | CFile::shareDenyWrite, NULL))
  24.     {
  25.         return FALSE;
  26.     }
  27.     if(FALSE == IsUnicodeFile(&foChineseMap))
  28.     {
  29.         foChineseMap.Close();
  30.         return FALSE;
  31.     }
  32.     if(MAX_UNICODE_FILE_SIZE < foChineseMap.GetLength())
  33.     {
  34.         foChineseMap.Close();
  35.         return FALSE;
  36.     }
  37.     //檢測統一編碼字符文件減去文件頭的統一編碼標志後的字節數能否被TCHAR整除,不能則表示文件有問題
  38.     if(0 !=(foChineseMap.GetLength() - sizeof(TCHAR)) % sizeof(TCHAR))
  39.     {
  40.         foChineseMap.Close();
  41.         return FALSE;
  42.     }
  43.     uiBufferTCharCount = (UINT)((foChineseMap.GetLength() - sizeof(TCHAR)) / sizeof(TCHAR));
  44.     pBuffer = (TCHAR *)malloc(uiBufferTCharCount * sizeof(TCHAR));
  45.     if(NULL == pBuffer)
  46.     {
  47.         foChineseMap.Close();
  48.         return FALSE;
  49.     }
  50.     memset(pBuffer, 0, uiBufferTCharCount * sizeof(TCHAR));
  51.    
  52.     if((uiBufferTCharCount * sizeof(TCHAR)) != foChineseMap.Read(pBuffer, uiBufferTCharCount * sizeof(TCHAR)))
  53.     {
  54.         foChineseMap.Close();
  55.         free(pBuffer);
  56.         pBuffer = NULL;
  57.         return FALSE;
  58.     }
  59.     foChineseMap.Close();
  60.     return TRUE;
  61. }
  62. #endif
  63. //開啟文件函式,FILE *版本
  64. BOOL CChnTrans::Open(const CString * const pFileName)
  65. {
  66.     if(NULL == pFileName)
  67.     {
  68.         return FALSE;
  69.     }
  70.     FILE * pChineseMap = NULL;
  71.     pChineseMap = _wfopen(*pFileName, _T("rb"));
  72.     if(NULL == pChineseMap)
  73.     {
  74.         return FALSE;
  75.     }
  76.     if(FALSE == IsUnicodeFile(pChineseMap))
  77.     {
  78.         fclose(pChineseMap);
  79.         return FALSE;
  80.     }
  81.     /開始:檢測字典文件的大小,字典文件不得超過MAX_UNICODE_FILE_SIZE + 2字節,大于該大小按照該大小處理///
  82.     BYTE * pBufferTmp = (BYTE *)malloc(MAX_UNICODE_FILE_SIZE);
  83.     if(NULL == pBufferTmp)
  84.     {
  85.         fclose(pChineseMap);
  86.         return FALSE;
  87.     }
  88.     UINT uiFileSize = (UINT)fread(pBufferTmp, sizeof(BYTE), MAX_UNICODE_FILE_SIZE, pChineseMap);
  89.     if(0 != uiFileSize % sizeof(TCHAR))
  90.     {
  91.         free(pBufferTmp);
  92.         fclose(pChineseMap);
  93.         return FALSE;
  94.     }
  95.     uiBufferTCharCount = uiFileSize / sizeof(TCHAR);
  96.     /結束:檢測字典文件的大小,字典文件不得超過MAX_UNICODE_FILE_SIZE + 2字節,大于該大小按照該大小處理///
  97.     pBuffer = (TCHAR *)malloc(uiBufferTCharCount * sizeof(TCHAR));
  98.     if(NULL == pBuffer)
  99.     {
  100.         fclose(pChineseMap);
  101.         return FALSE;
  102.     }
  103.     memset(pBuffer, 0, uiBufferTCharCount * sizeof(TCHAR));
  104.     memcpy(pBuffer, pBufferTmp, sizeof(TCHAR) * uiBufferTCharCount);
  105.     free(pBufferTmp);
  106.     fclose(pChineseMap);
  107.     return TRUE;
  108. }
  109. #if 0
  110. //檢測輸入文件是否非統一編碼(Unicode)文件,CFile版本
  111. BOOL CChnTrans::IsUnicodeFile(CFile * const poFile)
  112. {
  113.     if(NULL == poFile)
  114.     {
  115.         return FALSE;
  116.     }
  117.     TCHAR FirstTChar = 0;
  118.     poFile->Read(&FirstTChar, sizeof(TCHAR));
  119.     if(UNICODE_FILE_FLAG != *pFirstTChar)
  120.     {
  121.         return FALSE;
  122.     }
  123.     return TRUE;
  124. }
  125. #endif
  126. //檢測輸入文件是否非統一編碼(Unicode)文件,FILE *版本
  127. BOOL CChnTrans::IsUnicodeFile(FILE * pFile)
  128. {
  129.     if(NULL == pFile)
  130.     {
  131.         return FALSE;
  132.     }
  133.     TCHAR FirstTChar = 0;
  134.     if((1 != fread(&FirstTChar, sizeof(TCHAR), 1, pFile)) || (UNICODE_FILE_FLAG != FirstTChar))
  135.     {
  136.         return FALSE;
  137.     }
  138.     return TRUE;
  139. }
  140. //轉換單個漢字函數
  141. BOOL CChnTrans::TransChar(TCHAR * const pTCh)
  142. {
  143.     if(NULL == pTCh || NULL == pBuffer || 2 > uiBufferTCharCount)
  144.     {
  145.         return FALSE;
  146.     }
  147.     UINT i = 0;
  148.     for(; i < uiBufferTCharCount; i += 3)
  149.     {
  150.         if(*(pBuffer + i) == *pTCh)
  151.         {
  152.             *pTCh = *(pBuffer + i + 1);
  153.             return TRUE;
  154.         }
  155.     }
  156.     return TRUE;
  157. }
  158. //轉換字符串函數
  159. BOOL CChnTrans::Trans(CString * const pstrLine)
  160. {
  161.     if(NULL == pstrLine || NULL == pBuffer || 2 > uiBufferTCharCount)
  162.     {
  163.         return FALSE;
  164.     }
  165.     if(0 == pstrLine->GetLength())
  166.     {
  167.         return TRUE;
  168.     }
  169.     TCHAR * ptr = pstrLine->GetBuffer();
  170.     int iLineTCharNum = pstrLine->GetLength();
  171.     int i = 0;
  172.     for(; i < iLineTCharNum; i++)
  173.     {
  174.         if(TRUE != TransChar(ptr))
  175.         {
  176.             pstrLine->ReleaseBuffer();
  177.             return FALSE;
  178.         }
  179.         ptr++;
  180.     }
  181.     pstrLine->ReleaseBuffer();
  182.     return TRUE;
  183. }

 

測試程式代碼:

Han.cpp

文件內容:

  1. // Han.cpp : 定義主控台應用程式的進入點。
  2. //
  3. #include "stdafx.h"
  4. #include "Han.h"
  5. #include "ChnTrans.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #endif
  9. // 僅有的一個應用程式物件
  10. CWinApp theApp;
  11. using namespace std;
  12. int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
  13. {
  14.     int nRetCode = 0;
  15.     // 初始化 MFC 並於失敗時列印錯誤
  16.     if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
  17.     {
  18.         // TODO: 配合您的需要變更錯誤碼
  19.         _tprintf(_T("嚴重錯誤: MFC 初始化失敗/n"));
  20.         nRetCode = 1;
  21.     }
  22.     
  23.     class CChnTrans * pTC = new CChnTrans; 
  24.     CString strFileName = _T("C://han.txt");
  25.     if(TRUE != pTC->Open(&strFileName))
  26.     {
  27.         _tprintf(_T("err: pTC->Open f!/n"));
  28.         delete pTC;
  29.         return FALSE;
  30.     }
  31.     CString p = _T("一国家汉汉字国");
  32.     pTC->Trans(&p);
  33.     delete pTC;
  34.     return nRetCode;
  35. }

字庫文件

c:/han.txt

格式這樣寫(用記事本保存成Unicode形式):

一個簡體漢字緊跟它的正體漢字空格一個簡體漢字緊跟它的正體漢字空格……

 

如:

 

汉漢 国國

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值