华为机试题3道

一、身份证号码验证
题目描述:
我国公民的身份证号码特点如下:
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即可,不需要再做其他合法性判断。

#include <iostream>
using namespace std;

int verifyID(char* intID);
int getNum(char* intID, size_t pos, size_t len);
void main()
{
	char intID[19];
	cin>>intID;
	cout<<verifyID(intID)<<endl;
}

int verifyID(char* intID)
{
	if (strlen(intID) != 18)
	{
		return 1;
	}

	for (int i = 0; i < strlen(intID) - 1; i++)
	{
		if (intID[i] < '0' || intID[i] > '9')
		{
			return 2;
		}
	}

	char c = intID[strlen(intID) - 1];
	if (c != 'x' && (c < '0' || c > '9'))
	{
		return 3;
	}

	int year = getNum(intID,6,4);
	if (year < 1900 || year > 2100)
	{
		return 4;
	}

	int month = getNum(intID,10,2);
	if (month < 1 || month > 12)
	{
		return 5;
	}

	int day = getNum(intID,12,2);
	if (month & 1)
	{
		if (day < 1 || day > 31)
		{
			return 6;
		}
	}
	else
	{
		if (month != 2)
		{
			if (day < 1 || day > 30)
			{
				return 6;
			}
		}
		else
		{
			if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0))
			{
				if (day < 1 || day > 29)
				{
					return 6;
				}
			}
			else
			{
				if (day < 1 || day > 28)
				{
					return 6;
				}
			}
		}
	}

	return 0;
}

int getNum(char* intID, size_t pos, size_t len)
{
	char ch[10];
	memcpy(ch,intID+pos,4);
	ch[len] = '\0';
	return atoi(ch);
}

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

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

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

int verifyMsisdn(char* inMsisdn)
{
	if (strlen(inMsisdn) != 13)
	{
		return 1;
	}

	for (int i = 0; i < strlen(inMsisdn); i++)
	{
		if (inMsisdn[i] < '0' || inMsisdn[i] > '9')
		{
			return 2;
		}
	}

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

	return 0;
}

三、邮箱合法性验证
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

实现代码:

#include <iostream>
using namespace std;

void vConvertMsg(char *pInputStr, long lInputLen, char *pOutputStr);
void main()
{
	char input[20] = "huawei@hua@wei.com";
	//char input[20] = "aa@ddd@huawei.com";
	char output[20];
	vConvertMsg(input,strlen(input),output);
	cout<<output<<endl;
}

void vConvertMsg(char *pInputStr, long lInputLen, char *pOutputStr)
{
	int count = 0;
	for (int i = 0; i < strlen(pInputStr); i++)
	{
		if (((pInputStr[i] >= 'a' && pInputStr[i] <= 'z') ||
			(pInputStr[i] >= 'A' && pInputStr[i] <= 'Z') ||
			(pInputStr[i] >= 1 && pInputStr[i] <= 9) || 
			pInputStr[i] == '_' || pInputStr[i] == '.' ||
			pInputStr[i] == '@') == false)
		{
			strcpy(pOutputStr,"0");
			return;
		}
		else if (pInputStr[i] == ' ')
		{
			strcpy(pOutputStr,"0");
			return;
		}
		else if (pInputStr[i] == '@')
		{
			count++;
		}
	}

	if (count != 1)
	{
		strcpy(pOutputStr,"0");
		return;
	}

	char ch[5];
	memcpy(ch,pInputStr+strlen(pInputStr)-4,4);
	ch[4]='\0';
	int a = strcmp(ch,".com");
	if (strcmp(ch,".com") != 0)
	{
		strcpy(pOutputStr,"0");
		return;
	}

	strcpy(pOutputStr,"1");
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值