没什么难的,不过一个cout<<让我纠结了很久
学到一个新函数:
“
函数名: strncmp
功 能: 串比较
用 法: int strncmp(char *str1, char *str2, int maxlen);
说明:此函数功能即比较
字符串str1和str2的前maxlen个字符。如果前maxlen
字节完全相等,返回值就=0;在前maxlen字节比较过程中,如果出现str1[n]与str2[n]不等,则返回(str1[n]-str2[n])。
”
#include <iostream>
#include <stdio.h>
#include <cstring>
using namespace std;
int main() {
int count;
char s[20];
string id[8] = {"33", "11", "71", "81", "82", "54", "21", "31"};
string place[8]={"Zhejiang","Beijing","Taiwan","Hong Kong","Macao","Tibet","Liaoning","Shanghai"};
scanf("%d",&count);
while(count--){
scanf("%s",&s);
printf("He/She is from ");
for(int i=0;i<8;i++){
if(strncmp(s, id[i].c_str(), 2) ==0 ){
cout<<place[i];//可能会有问题
}
}
printf(",and his/her birthday is on ");
printf("%c%c,%c%c,%c%c%c%c",s[10],s[11],s[12],s[13],s[6],s[7],s[8],s[9]);
printf(" based on the table.");
}
return 0;
}