PTA:7-34 通讯录的录入与显示 (10分)

  通讯录中的一条记录包含下述基本信息:朋友的姓名、出生日期、性别、固定电话号码、移动电话号码。 本题要求编写程序,录入N条记录,并且根据要求显示任意某条记录。

输入格式:
  输入在第一行给出正整数N(≤10);随后N行,每行按照格式姓名 生日 性别 固话 手机给出一条记录。其中姓名是不超过10个字符、不包含空格的非空字符串;生日按yyyy/mm/dd的格式给出年月日;性别用M表示“男”、F表示“女”;固话手机均为不超过15位的连续数字,前面有可能出现+

  在通讯录记录输入完成后,最后一行给出正整数K,并且随后给出K个整数,表示要查询的记录编号(从0到N−1顺序编号)。数字间以空格分隔。

输出格式:
  对每一条要查询的记录编号,在一行中按照姓名 固话 手机 性别 生日的格式输出该记录。若要查询的记录不存在,则输出Not Found

输入样例:

3
Chris 1984/03/10 F +86181779452 13707010007
LaoLao 1967/11/30 F 057187951100 +8618618623333
QiaoLin 1980/01/01 M 84172333 10086
2 1 7

输出样例:

LaoLao 057187951100 +8618618623333 F 1967/11/30
Not Found

下面给出网上看到思路比较清晰的代码:

#include <stdio.h>
#include <string.h>
 
struct contacts{
    char name[11];  //字符串结尾有0,长度需加1,下同 
    char birth[11];
    char gender;
    char fphone[17]; //固定电话fixed phone 
    char mphone[17]; //移动电话mobile phone 
};
 
void input();
void output();

int main()
{
    int n;
    scanf("%d", &n);
    struct contacts t[n];  //定义结构体数组,数组中每个元素都是一个结构体 
    input(t, n);
    output(t, n);
    
    return 0;
}
 
void input(struct contacts p[], int n)
{
    int i;
    for(i = 0; i < n; i++) {
        scanf("%s %s %c %s %s", p[i].name, p[i].birth, 
		&p[i].gender, p[i].fphone, p[i].mphone); //gender不是数组,别忘了& 
    }
}
 
void output(struct contacts q[], int n)
{
    int m;
    scanf("%d", &m);
    int a[m]; //用一个数组记录要查找的记录编号 
    
    int j;
    for(j = 0; j < m; j++) {
        scanf("%d", &a[j]);
    }
    
    for(j = 0; j < m; j++) {
        if(a[j] >=0 && a[j] < n) {  
            printf("%s %s %s %c %s\n", q[a[j]].name, q[a[j]].fphone, 
			q[a[j]].mphone, q[a[j]].gender, q[a[j]].birth);
        }
        else {
            printf("Not Found\n");
        }
    }    
}
发布了56 篇原创文章 · 获赞 101 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/weixin_43871127/article/details/104448432