5-34 通讯录的录入与显示

通讯录中的一条记录包含下述基本信息:朋友的姓名、出生日期、性别、固定电话号码、移动电话号码。 本题要求编写程序,录入 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

解题思路:

主要是结构类型的运用。没什么难度。就是测试点要注意。比如查找的条目为负数。比如电话号码是 15 位,而且还有可能有额外的 '+' 号。

解题代码:

#include<stdio.h>

int main ()
{
	int N;
	scanf ("%d", &N);
	
	struct contacts {
		char name[11];
		char birth[11];
		char gender[2];
		char tel[17];
		char mobile[17];
	} contact[N]; 
	
	int i, count = 0;
	for (i=0; i<N; i++) {
		while (count < 5) {
			switch (count) {
				case 0: scanf ("%s", &contact[i].name); count ++; break;
				case 1: scanf ("%s", &contact[i].birth); count ++; break;
				case 2: scanf ("%s", &contact[i].gender); count ++; break;
				case 3: scanf ("%s", &contact[i].tel); count ++; break;			
				case 4: scanf ("%s", &contact[i].mobile); count ++; break;	
			}
		}
		count = 0;
	}
	
	int K;
	scanf ("%d", &K);
	
	int number;	
	for (i=0; i<K; i++) {
		scanf ("%d", &number);
		if (number < N && number>=0) {
			printf ("%s ", contact[number].name);
			printf ("%s ", contact[number].tel);
			printf ("%s ", contact[number].mobile);
			printf ("%s ", contact[number].gender);
			printf ("%s\n", contact[number].birth);			
		} else {
			printf ("Not Found\n"); 
		}
	}
	
	return 0; 
}
posted @ 2016-08-04 22:23  文之  阅读(665)  评论(0编辑  收藏  举报