借用編碼表,查詢對應漢字的五筆編碼:
小乓練題:
int main(int argc, char* argv[])
{
if (argc==1)
{
std::cout<<"用法: wubi <需要查詢編碼的漢字>"<<std::endl;
return 0;
}
// 文件路徑
char* szPath = "wubi.txt";
std::ifstream fin(szPath);
if (!fin)
{
std::cout<<"無法打開五筆編碼文件,文件名wubi.txt,請置于wubi.exe同目錄中"<<std::endl;
return -1;
}
// 通常我們這樣讀取一個文本文件的全文
std::string strText = std::string(std::istreambuf_iterator<char>(fin), std::istreambuf_iterator<char>());
typedef std::map<std::string, std::string> WubiMap;
WubiMap wubiMap;
int nLength = strText.length();
int nLeft = 0;
int nRight = -1;
// 讀入五筆編碼
std::string strWord;
std::string strCode;
while(nRight < nLength)
{
nLeft = nRight+1;
nRight = nLeft+1;
//tab前的部分為key:strWord
while (nRight<nLength && strText[nRight] != '\t')
{
nRight++;
}
if(nRight < nLength)
{
strWord = strText.substr(nLeft, nRight - nLeft);
}
nLeft = nRight + 1;
nRight = nLeft + 1;
//回車前的為Code
while (nRight<nLength && strText[nRight] != '\n')
{
nRight++;
}
if(nRight < nLength)
{
strCode = strText.substr(nLeft, nRight - nLeft);
wubiMap[strWord]= strCode;
}
}
// 循環查找相關漢字的編碼
std::string strLine=argv[1];
nLength= strLine.length();
for (int i=0; i<nLength; ++i)
{
// 判斷是漢字的前半部分
if (strLine[i]<0)
{
strWord= strLine.substr(i, 2);
std::cout<<strWord<<"\t"<<wubiMap[strWord]<<std::endl;
++i;
}else
{
std::cout<<strLine[i]<<"\t"<<"無對應編碼"<<std::endl;
}
}
return 0;
}
示例:
D:\>wubi 我是一本書
我 trnt
是 jghu
一 ggll
本 sgd
書 nnhy
附:python解決方案一個:
import sys
wbx={
'工':'aaaa',
'式':'aad',



'誶':'yywf',
'言':'yyyy',
'徃':'ttgg',
'不':'gii'}
if len(sys.argv)==1:
print('wubi <需要查找五筆編碼的漢字>')
sys.exit(1)
argv=sys.argv[1:]
for line in argv:
for c in line:
if c in wbx:
print(' %s %s'%(c, wbx[c]))
elif ord(c)<128 or c in ',。!~“”?':
print(' %s'%c)
else:
print(' %s -->編碼缺失'%c)
附:五筆編碼表一張:
wubi.7z 小乓加油 :D