清风竹林

ぷ雪飘绛梅映残红
   ぷ花舞霜飞映苍松
     ----- Do more,suffer less

五笔编码查询


  借用编码表,查询对应汉字的五笔编码:

  小乓练题:
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

 

posted on 2008-11-20 16:54 李现民 阅读(440) 评论(0)  编辑 收藏 引用 所属分类: 小乓练题


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理