11-1. 通讯录的录入与显示(10)
通讯录中的一条记录包含下述基本信息:朋友的姓名、出生日期、性别、固定电话号码、移动电话号码。 本题要求编写程序,录入N条记录,并且根据要求显示任意某条记录。
输入格式:
输入在第1行给出正整数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
//中国大学MOOC-翁恺-C语言程序设计习题集
//create by zlc on 12/16/2014
//11-1. 通讯录的录入与显示
#include <stdio.h>
#include <stdlib.h>
struct stu *create_list(int N);
struct stu{ //录入信息结构体
char name[11];
char birthday[11];
char sex;
char phone[17];
char mobile[17]; //题目要求:15位数字,还应包括 '+' '\0',所以最终数组大小设为17
struct stu *Next;
};
int main()
{
int N,K,i;
scanf("%d",&N);
struct stu *List,*P;
List=create_list(N);
scanf("%d",&K);
int *output; //录入要输出的记录编号
output=(int *)malloc(sizeof(int)*K);
for(i=0;i<K;i++)
scanf("%d",&output[i]);
for(i=0;i<K;i++)
{
int mask=1,cnt=0;
P=List;
while(mask)
{
if(P!=NULL)
{
if(cnt==output[i])
{
printf("%s %s %s %c %s\n",P->name,P->phone,P->mobile,P->sex,P->birthday);
mask=0;
}
else
{
P=P->Next;
cnt++;
}
}
else
{
printf("Not Found\n");
mask=0;
}
}
}
free(output);
return 0;
}
struct stu *create_list(int N) //通过链表录入通讯录
{
struct stu *head,*front,*new_node;
int i;
for(i=0;i<N;i++)
{
new_node=(struct stu*)malloc(sizeof(struct stu));
scanf("%s %s %c %s %s",&new_node->name,&new_node->birthday,&new_node->sex,&new_node->phone,&new_node->mobile);
if(i==0)
front=head=new_node;
else
front->Next=new_node;
new_node->Next=NULL;
front=new_node;
}
return(head);
}