He/She is from Zhejiang,and his/her birthday is on 10,12,1989 based on the table.*/
方法一:
#include
#include
void result(char *a, int from, int to);
char provin[100][20] = {[33] = "Zhejiang", [11] = "Beijing", [71] = "Taiwan",
[81] = "Hong Kong", [82] = "Macao", [54] = "Tibet",
[21] = "Liaoning", [31] = "Shanghai"};//用二位数组存省份
int main()
{
int n, i;
char a[1000];
int sum;
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
scanf("%s", a);
printf("He/She is from ");
result(a, 0, 1); //provin
printf(",and his/her birthday is on ");
result(a, 10, 11); //month
putchar(',');
result(a, 12, 13); //date
putchar(',');
result(a, 6, 9); //year
printf(" based on the table.\n");
}
return 0;
}
void result(char *a, int from, int to)
{
int i, sum;
if (from == 0)
{
sum = (a[0] - '0') * 10 + (a[1] - '0');//-‘0’是将字符变为数字
printf("%s", provin[sum]);
return;
}
for (i = from; i <= to; i++)
{
printf("%d", a[i] - '0');//-‘0’是将字符变为数字
}
}
方法二:
#include
#include
void SubString(char *dest, char *origin, int from, int to) {
int i = 0;
for (i = from; i <= to; i++) {
dest[i - from] = origin[i];//将原数组指定位数存在新数组中
}
dest[to - from + 1] = '\0';//因为存入新数组是一个字符一个字符存储,所以末尾家‘\0’
}
int main()
{
char idno[20], year[5], month[3], day[3], pro[20];//建立年,月,日,省的新数组
int n;
scanf("%d", &n);
while (n--) {
scanf("%s", idno);
SubString(pro, idno, 0, 1);
SubString(year, idno, 6, 9);
SubString(month, idno, 10, 11);
SubString(day, idno, 12, 13);
if (strcmp(pro, "33") == 0)//省去二维数组的麻烦,利用strcmp函数比较开头两个字符,写出省份
strcpy(pro, "Zhejiang");
else if (strcmp(pro, "11") == 0)
strcpy(pro, "Beijing");
else if (strcmp(pro, "71") == 0)
strcpy(pro, "Taiwan");
else if (strcmp(pro, "81") == 0)
strcpy(pro, "Hong Kong");
else if (strcmp(pro, "82") == 0)
strcpy(pro, "Macao");
else if (strcmp(pro, "54") == 0)
strcpy(pro, "Tibet");
else if (strcmp(pro, "21") == 0)
strcpy(pro, "Liaoning");
else if (strcmp(pro, "31") == 0)
strcpy(pro, "Shanghai");
printf("He/She is from %s,and his/her birthday is on %s,%s,%s based on the table.\n", pro, month, day, year);
}
return 0;
}