- 最后登录
- 2017-5-15
- 注册时间
- 2012-3-1
- 阅读权限
- 90
- 积分
- 32973
- 纳金币
- 32806
- 精华
- 12
|
int IsChineseOrEnglish(LPCSTR pText)
{
unsigned char sqChar[10];
sqChar[0]=*pText;
sqChar[1]='/0';
if (sqChar[0]>=0xa1)
{
if (sqChar[0]==0xa3)
{
return 1;//全角字符 }
else
{
return 2;//汉字 }
}
else
{
return 0;//英文,英文标点,数字
}
}
bool GetNumChineseAndNumEnglish(const char*psrc, int &nNumChiRet, int &nNumEngRet)//获得汉字与英文的个数
{
if (pSrc == NULL)
{
return false;
}
else
{
int nLen = strlen(pSrc);
char *pTxt = new char[nLen +1];
char *pHeader = pTxt;
//LPCSTR pTxt = pSrc;
strcpy(pTxt, pSrc);
pTxt[nLen] = '/0';
while (*pTxt != '/0')
{
int nRet = IsChineseOrEnglish(pTxt);
switch (nRet)
{
case 0:
++ nNumEngRet;
pTxt++;
break;
case 1:
case 2:
++ nNumChiRet;
pTxt++;
pTxt++;
break;
default:
break;
}
}
if (pHeader != NULL)
{
delete [] pHeader;
pHeader = NULL;
}
return true;
}
}
下面是完整的测试程序:
#include <windows.h>
#include <iostream>
using namespace std;
int IsChineseOrEnglish(LPCSTR pText);
bool GetNumChineseAndNumEnglish(const char*pSrc, int &nNumChiRet, int &nNumEngRet);ÎĵÄ×Ö·û¸öÊý
int main(int argc,char**argv)
{
int nNumChin = 0;
int nNumEng = 0;
GetNumChineseAndNumEnglish("£áÑîСӱYanger7", nNumChin, nNumEng);
cout << "Number of Chinese:" << nNumChin << endl;
cout << "Number of English:" << nNumEng << endl;
return 0;
}
int IsChineseOrEnglish(LPCSTR pText)
{
unsigned char sqChar[10];
sqChar[0]=*pText;
sqChar[1]='/0';
if (sqChar[0]>=0xa1)
{
if (sqChar[0]==0xa3)
{
return 1; }
else
{
return 2;//ºº×Ö
}
}
else
{
return 0; }
}
bool GetNumChineseAndNumEnglish(const char*pSrc, int &nNumChiRet, int &nNumEngRet){
if (pSrc == NULL)
{
return false;
}
else
{
int nLen = strlen(pSrc);
char *pTxt = new char[nLen +1];
char *pHeader = pTxt;
//LPCSTR pTxt = pSrc;
strcpy(pTxt, pSrc);
pTxt[nLen] = '/0';
while (*pTxt != '/0')
{
int nRet = IsChineseOrEnglish(pTxt);
switch (nRet)
{
case 0:
++ nNumEngRet;
pTxt++;
break;
case 1:
case 2:
++ nNumChiRet;
pTxt++;
pTxt++;
break;
default:
break;
}
}
if (pHeader != NULL)
{
delete [] pHeader;
pHeader = NULL;
}
return true;
}
}
|
|