2629根据身份证号辨别所在地和生日按照以下形式输出:
He/She is from Zhejiang,and his/her birthday is on 10,12,1989 based on the table.
思路:身份证长度以及信息所在位置是固定的,用字符串截取函数截取出生日信息以及地址信息,根据switch找到地点,最后按格式输出,注意空格
若用string: 截取函数为substr
string s=“1234”;
s.substr(2) //从字符串下标为2的地方开始截取,截取到末尾
s.substr(0,2) //从字符串下标为2的地方开始截取,截取长度为2,
s.substr(1,2) //输出23
若用字符数组:
取字符串s1中的前n 个字符串,赋值给s2;
s2=strncpy(s2,s1, n);
#include<iostream>
#include<string>
using namespace std;
int main(){
int n,m;
cin>>n;
while(n--){
string str;
string str1,year,mon,day;
string des;
cin>>str;
str1=str.substr(0,2);//截取地址信息
year=str.substr(6,4);//生日年份信息
mon=str.substr(10,2);//生日月份信息
day=str.substr(12,2);//生日信息
int n=(str1[0]-'0')*10+str1[1]-'0';
switch(n){
case 33: des="Zhejiang";
break;
case 11: des="Beijing";
break;
case 71: des="Taiwan";
break;
case 81: des="Hong Kong";
break;
case 82: des="Macao";
break;
case 54: des="Tibet";
break;
case 21: des="Liaoning";
break;
case 31: des="Shanghai";
break;
}
cout<<"He/She is from "<<des<<",and his/her birthday is on ";
cout<<mon<<","<<day<<","<<year<<" based on the table."<<endl;
}
return 0;
}