Hua Wei 机试题目一

一、身份证号码验证

题目描述:

我国公民的身份证号码特点如下:
1、 长度为18位;
2、 第1~17位只能为数字;
3、 第18位可以是数字或者小写英文字母x。
4、 身份证号码的第7~14位表示持有人生日的年、月、日信息。
例如:511 002 1988 08 08 0111或51100219880808011x。
请实现身份证号码合法性判断的函数。除满足以上要求外,需要对持有人生日的年、月、日信息进行校验。年份大于等于1900年,
小于等于2100年。需要考虑闰年、大小月的情况。
所谓闰年,能被4整除且不能被100整除 或 能被400整除的年份,闰年的2月份为29天,非闰年的2月份为28天。
其他情况的合法性校验,考生不用考虑。

int verifyID(char* inID)
函数返回值:
1) 如果身份证号合法,返回0;
2) 如果身份证号长度不合法,返回1;
3) 如果身份证号第1~17位含有非数字的字符,返回2;
4) 如果身份证号第18位既不是数字也不是英文小写字母x,返回3;
5) 如果身份证号的年信息非法,返回4;6-9
6) 如果身份证号的月信息非法,返回5;10-11
7) 如果身份证号的日信息非法,返回6(请注意闰年的情况);12-13
【注】除成功的情况外,以上其他合法性判断的优先级依次降低。也就是说,如果判断出长度不合法,直接返回1即可,不需要再做其他合法性判断。

二、手机号码验证

问题描述:
我国大陆运营商的手机号码标准格式为:国家码+手机号码,例如:8613912345678。特点如下:

1、 长度13位;
2、 以86的国家码打头;
3、 手机号码的每一位都是数字。

请实现手机号码合法性判断的函数要求:
1) 如果手机号码合法,返回0;
2) 如果手机号码长度不合法,返回1
3) 如果手机号码中包含非数字的字符,返回2;
4) 如果手机号码不是以86打头的,返回3;
【注】除成功的情况外,以上其他合法性判断的优先级依次降低。也就是说,如果判断出长度不合法,直接返回1即可,不需要再做其他合法性判断。
要求实现函数:
int verifyMsisdn(char* inMsisdn)
【输入】 char* inMsisdn,表示输入的手机号码字符串。
【输出】 无
【返回】 判断的结果,类型为int。
示例
输入: inMsisdn = “869123456789“
输出: 无
返回: 1
输入: inMsisdn = “88139123456789“
输出: 无
返回: 3
输入: inMsisdn = “86139123456789“
输出: 无
返回: 0
(给的例子都不是13位,应该都返回1)

三、邮箱合法性验证

Title Description:
Compile a function for verifying validity of a mailbox address. The mailbox address is valid if the following conditions are met:
1. 地址中只能有一个 '@' .
2.最后三位必须是 ".com".
3. 字符之间没有空格.
4.有效地字符: 1~9, a~z, A~Z, '.', '@', '_'.
返回结果1表示该邮箱是合法的. 返回 0 表示该邮箱不合法.
To complete the following function:
void vConvertMsg(char *pInputStr, long lInputLen, char *pOutputStr);
[Input]
char *pInputStr: a pointer pointing at an array
long lInputLen: length of the array
char *pOutputStr: output result displayed as character strings. '1' indicates a valid mailbox address. '0' indicates an invalid mailbox address. '\0' indicates the end of the character string.
[Return] None
[Note] You only need to complete the function algorithm, without any IO output or input.
Example
Input: huawei@huawei.com
Return: 1
Input: aa@ddd@huawei.com
Return: 0

实现如下:

一、身份验证
int isLeapYear(int year)
{
    if ((year%4==0 && year%100!=0) || year%400==0)
    {
        return 1;
    }
    else
        return 0;
}
int verifyID(char* inID)
{
    int len=strlen(inID);
    if (len!=18)
    {
        return 1;
    }
    for (int i=0;i<len-1;i++)
    {
        if (inID[i]<'0' || inID[i]>'9')
        {
            return 2;
        }
    }
    if (((inID[len-1]>='0' && inID[len-1]<='9') || inID[len-1]=='x')==false)
    {
        return 3;
    }
    string temp=inID;
    string temp1=temp.substr(6,4);
    int year=0;
    for (int i=0;i<4;i++)
    {
        year=year*10+temp1[i]-'0';
    }
    //cout<<year<<" ";
    if (year<1900 || year>2100)
    {
        return 4;
    }
    string temp2=temp.substr(10,2);
    int month=0;
    for (int i=0;i<2;i++)
    {
        month=month*10+temp2[i]-'0';
    }
    //cout<<month<<" ";
    if (month<1 || month>12)
    {
        return 5;
    }
    string temp3=temp.substr(12,2);
    int day=0;
    for (int i=0;i<2;i++)
    {
        day=day*10+temp3[i]-'0';
    }
    //cout<<day<<" ";
    if (month==2)
    {
        if (isLeapYear(year))
        {
            if (day<1 || day>30)
            {
                return 6;
            }
        }
        else
        {
            if (day<1 || day>29)
            {
                return 6;
            }
        }
    }
    else if (month==1 || month==3 || month==5 || month==7 ||  month==8 ||  month==10 ||  month==12)
    {
        if (day<1 || day>31)
        {
            return 6;
        }
    }
    else
    {
        if (day<1 || day>30)
        {
            return 6;
        }
    }
        
    return 0;
}

二、手机号码验证
int verifyMsisdn(char* inMsisdn)
{
    int len = strlen(inMsisdn);
    if (len!=13)
    {
        return 1;
    }
    int i=0;
    for (;i<len;i++)
    {
        if (inMsisdn[i]<'0' || inMsisdn[i]>'9')
        {
            return 2;
        }
    }

    if (inMsisdn[0]!='8' || inMsisdn[1]!='6')
    {
        return 3;
    }

    if (i==len)
    {
        return 0;
    }
    
}
三、邮箱地址验证
void vConvertMsg(char *pInputStr, long lInputLen, char *pOutputStr)
{
    int countOfAt=0;
    int countOfSpace=0;
    int countOfError=0;
    int i=0;
    for (;i<lInputLen;i++)
    {
        if (pInputStr[i]=='@')
        {
            countOfAt++;
        }
        if (pInputStr[i]==' ')
        {
            countOfSpace++;
        }
        if (((pInputStr[i]>='1' && pInputStr[i]<='9') || (pInputStr[i]>='a' 
            && pInputStr[i]<='z') || (pInputStr[i]>='A' && pInputStr[i]<='Z') 
            || pInputStr[i]=='_' || pInputStr[i]=='.' || pInputStr[i]=='@')==false)
        {
            countOfError++;
        }
    }
    if (countOfAt!=1 || countOfSpace!=0 || countOfError!=0)
    {
        *pOutputStr=0+'0';
        cout<<*pOutputStr<<endl;
        return;
    }
    if (pInputStr[i-1]!='m' || pInputStr[i-2]!='o' || pInputStr[i-3]!='c' || pInputStr[i-4]!='.')
    {
        *pOutputStr=0+'0';
    }
    else
        *pOutputStr=1+'0';

    cout<<*pOutputStr<<endl;

}

 

posted on 2014-07-05 09:31  yvictoryr  阅读(544)  评论(1编辑  收藏  举报

导航