当前位置:编程学习 > C/C++ >>

C语言的编程题

答案:
#include<stdio.h>
#include<string.h>
/*手机通讯录结构定义*/
struct friends_list{
char name[10]; /* 姓名 */
int age; /* 年龄 */
char telephone[13]; /* 联系电话 */
};

int Count = 0; /* 定义全局变量Count,记录当前联系人总数 */
void new_friend(struct friends_list friends[ ] );
void search_friend(struct friends_list friends[ ], char *name);

int main(void)
{
int choice;
char name[10];
struct friends_list friends[50]; /* 包含50个人的通讯录 */

do{
printf("手机通讯录功能选项:1:新建 2:查询 0:退出\n");
printf("请选择功能:");
scanf("%d", &choice);
switch(choice){
case 1:
new_friend(friends);
break;
case 2:
printf("请输入要查找的联系人名:");
scanf("%s", name);
search_friend(friends, name);
break;
case 0: break;
}
}while(choice != 0);
printf("谢谢使用通讯录功能!\n");

return 0;
}

/*新建联系人*/
void new_friend(struct friends_list friends[ ])
{
struct friends_list f;

if(Count == 50){
printf("通讯录已满!\n");
return;
}
printf("请输入新联系人的姓名:");
scanf("%s", f.name);
printf("请输入新联系人的年龄:");
scanf("%d", &f.age);
printf("请输入新联系人的联系电话:");
scanf("%s", f.telephone);
friends[Count] = f;
Count++;
}

/*查询联系人*/
void search_friend(struct friends_list friends[ ], char *name)
{
int i, flag = 0;

if(Count == 0){
printf("通讯录是空的!\n");
return;
}
for(i = 0; i < Count; i++)
if(strcmp(name,friends[i].name) == 0){ /* 找到联系人*/
flag=1;
break;
}
if(flag){
printf("姓名: %s\t", friends[i].name);
printf("年龄: %d\t", friends[i].age);
printf("电话: %s\n", friends[i].telephone);
}
else
printf("无此联系人!");
}

上一个:C语言是什么?
下一个:C语言做课程设计

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,