代码改变世界

判断汉字和字母

2017-02-18 17:31  雄风狂飙  阅读(120)  评论(0)    收藏  举报
#include <iostream>
using namespace std;


bool isGB2312(const char*pszBuf)
{
    return ( *(unsigned char*)pszBuf >= 0x81 && *(unsigned char*)pszBuf <= 0xFE
        && *(unsigned char*)(pszBuf+1) >= 0x40 && *(unsigned char*)(pszBuf+1) <= 0xFE );

}
bool isABC(const char*pszBuf)
{
    return ( *(unsigned char*)pszBuf <= 122 && *(unsigned char*)pszBuf >= 97 )
        || ( *(unsigned char*)pszBuf <= 90 && *(unsigned char*)pszBuf >= 65 );
}

bool isGB2312OrABC(const char*pszBuf)
{
    while(*pszBuf != '\0')
    {
        if( !isGB2312(pszBuf) )
        {
            if( !isABC(pszBuf) )
            {
                return false;
            }else
            {
                ++pszBuf;
            }
        
        }else
        {
            ++pszBuf;++pszBuf;
        }
    }
    return true;
}


int main()
{
    char str[24];

    memset(str,0,sizeof(str));
    cout<<"Input CustName:"<<endl;
    cin>>str;
    cout<<"CustName=="<<str<<endl;
    if(isGB2312OrABC(str))
    {
        cout<<"是汉字或者字母!"<<endl;
    }else
    {
        cout<<"存在非法字符!"<<endl;
    }
    
    return 0;

}