拼音匹配汉字首字母



// PinYin.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdlib.h>
#include <iostream>
#include <string>
using namespace std;
#include <afx.h>
#define MAX_LEVEL2   16
char   CFontL2[MAX_LEVEL2*52];
bool haveL2 = false;

char * LAlpha(char * src)
{
 char * str = src;
 while ( *str != '\0')
 {
  if ( *str >='A' && *str<='Z')
  {
   *str += 32;
  }
  str ++;
 }
 return src;
}
bool _is_alpha(char c)
{
 return ((c>='a' && c<='z')||(c>='A' && c<='Z'));
}
bool IsSubStr( const char* str, const char* mother )
{
 int len1=strlen(str),len2=strlen(mother);
 if( len2>=len1 && !strncmp(LAlpha( (char *)str),LAlpha( (char *)mother),len1) )
  return true;
 return false;
}

void InitPy()
{
 char item[64];
 unsigned char str[512];
 memset( CFontL2, 0, 52 * MAX_LEVEL2 );
 for( int i=0;i<26;i++ )
 {
  sprintf(item,"%c",i+'A');
  GetPrivateProfileStringA("PYLEVEL2",item,"",(char*)str,sizeof(str),".\\pinying.ini");
  if( * str == 0 )
   continue;
  size_t uStrLen = strlen((const char*)str);
  for( int j=0,k=0;j<uStrLen;j+=2 )
  {
   if( str[j]>=0xa0 && str[j+1]>=0xa0 )
   {
    CFontL2[i*MAX_LEVEL2*2+k] = str[j];
    CFontL2[i*MAX_LEVEL2*2+k+1] = str[j+1];
    k+=2;
    haveL2 = true;
   }
  }
 }
}

bool MatchPyCode(char *name,char *key,int keys)
{
 static char *table[27] =
 {
  "啊","芭","擦","搭","蛾","发","噶","哈","击",
  "击","喀","垃","妈","拿","哦","啪","期","然",
  "撒","塌","挖","挖","挖","昔","压","匝","座"
 };
 short int  i, j, len, ch;
 char buf[16];
 bool sf, rlt = false;

 if( strlen(name)<=0 || strlen(key)<=0 )
  return false;
 len = strlen(key);
 for( i=0,j=0;i<len;i++ )
 {
  if( (unsigned)key[i]>=0x80 )
   return false;
  if( _is_alpha(key[i]) )
   j++;
 }
 if( !j )
  return false;
 memset( buf, 0, 16 );
 size_t uStrLen = strlen(name)-1;
 for( i=0,len=0;i<uStrLen;i++ )
 {
  if( (unsigned char)name[i]>=0xa0 )
  {
   buf[len*2]   = name[i];
   buf[len*2+1] = name[i+1];
   i ++;
   len ++;
  }
  else if( _is_alpha(name[i]) )
  {
   buf[len*2] = toupper(name[i]);
   len ++;
  }
  else if( name[i]>='0' && name[i]<='9' )
  {
   buf[len*2] = name[i];
   len ++;
  }
 }

 for( i=0; i<=len-keys; i++ )
 {
  rlt = true;
  for( j=0; j<keys; j++ )
  {
   sf = false;
   if( key[j]=='.' )
    sf = true;
   else if( _is_alpha(key[j])&&((unsigned char)buf[(i+j)*2]>=0xa0) )
   {
    ch = toupper(key[j])-'A';
    if( (strncmp(table[ch],buf+(i+j)*2,2)<=0&&strncmp(table[ch+1],buf+(i+j)*2,2)>0) )
     sf = true;
    else if( ((unsigned char)buf[(i+j)*2]==0xa3)&&((unsigned char)buf[(i+j)*2+1]==(0xc1+ch)) )
     sf = true;
    else if( haveL2 )
    {
     for( int m=0; m<MAX_LEVEL2; m++ )
      if( (buf[(i+j)*2]==CFontL2[ch*MAX_LEVEL2*2+m*2])&&(buf[(i+j)*2+1]==CFontL2[ch*MAX_LEVEL2*2+m*2+1]) )
       sf = true;
    }
   }
   else
   {
    if( buf[(i+j)*2]==toupper(key[j]) )
     sf = true;
   }
   if( !sf )
   {
    rlt = false;
    break;
   }
  }
  if( rlt )
   break;
 }

 return rlt;
}


int _tmain(int argc, _TCHAR* argv[])
{
 InitPy();
 while(1){
  string str;
  cin >> str;
  bool b = MatchPyCode("我操你大爷的干活",(char *)str.c_str(),str.size());
  if ( b )
  {
   cout << "匹配成功" << endl;
  }else{
   cout << "匹配失败" << endl;
  }
 }

 return 0;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值