题目:
信息查询
从键盘中输入姓名和电话号码,以"#"结束,编程实现输入姓名,查询电话号码的功能。
源代码:
#include <stdio.h>
#include <string.h>
#define MAX 10
struct information
{
char name[20];
char Telephone_number[20];
};
void insert_information(struct information * infor,int max);
int select_information(struct information * infor,int max,char * select_name);
int main()
{
struct information infor[MAX];
char select_name[20];
int select_location;
insert_information(infor,MAX);
printf("\n请输入你要查询的姓名: ");
scanf("%s",select_name);
select_location = select_information(infor,MAX,select_name);
if( select_location != 0)
{
printf("姓名: %s\n电话号码: %s\n",infor[select_location-1].name,infor[select_location-1].Telephone_number);
}
else printf("查询失败\n");
return 0;
}
void insert_information(struct information * infor,int max)
{
printf("请输入要插入的信息(输入#停止输入): \n");
for(int i = 0;i < max;i++)
{
scanf("%s",infor[i].name);
if(infor[i].name[0] == '#') break;
scanf("%s",infor[i].Telephone_number);
}
}
int select_information(struct information * infor,int max,char * select_name)
{
for(int i = 0;i < max;i++)
{
if(!strcmp(select_name,infor[i].name)) return i+1;
}
return 0;
}
演示效果:
如果朋友你感觉文章的内容对你有帮助,可以点赞,关注文章和专栏以及关注我哈,嘿嘿嘿我会定期更新文章的,谢谢朋友你的支持哈