问题:
输入一个字符串,并且进行验证。
条件:
长度必须为11
第一位必须为1
第二位必须为3,5,6,8中的一个
其余任意一位必须为0-9中的一个
分析:
根据规则进行判断即可。
代码:
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <algorithm>
#include <iomanip>
#define MAX 1000
using namespace std;/* run this program using the console pauser or add your own getch, system("pause") or input loop */
bool isPhoneNum(char * str){//1.长度必须为11位int length=strlen(str) ;if(length != 11){return false;}else{//2.第一位必须为1 第二位为3,5,6,8 其余位必须为0-9中的一个数if(str[0] == '1') {if(str[1] == '3' || str[1] == '5'||str[1] == '6'||str[1] == '8'){//3.其余位必须为0-9中的一个数for(int i=2;i<length;i++) {if(str[i] >'9' || str[i] < '0'){return false;}}}else{return false;}}else{return false;}}return true;
}
int main(int argc, char** argv) {/*freopen("file/input.txt","r",stdin);freopen("file/output.txt","w",stdout);*/char str[MAX];while(gets(str)){if(isPhoneNum(str)){printf("是一个正确的电话号码!\n");}else{printf("不是一个正确的电话号码!\n");} }return 0;
}
/*测试用例:15735186582157351865822257351865821373518658216735186582187351865821273518658217735186582197351865821573518658a
*/