纳金网
标题:
如何判断全角半角?
[打印本页]
作者:
她。
时间:
2013-6-13 09:25
标题:
如何判断全角半角?
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;
}
}
欢迎光临 纳金网 (http://c-www.narkii.com/club/)
Powered by Discuz! X2.5