
用C语言编写通讯录,求程序代码
就是说写程序,要求编写成功后,能用其反查人名,可以同时获得他的电话,民族,地址等与其相关的信息,当然了,这些信息是提前已写入程序中的,要求人名等相关信息是用字母,数字组成...
就是说写程序,要求编写成功后,能用其反查人名,可以同时获得他的电话,民族,地址等与其相关的信息,当然了,这些信息是提前已写入程序中的,要求人名等相关信息是用字母,数字组成,不许有汉字,下面我写几个人名及其相关信息,程序必须可以查到他们的信息.要求查信息的时候,方便,快捷.
1.lihan liaoning huluodao 2. zhuqiang jiangsu changzhu
3.wangjiangang liaoning anshan 4.zhanghongwei shandong zhucheng
也就是,人名,省份,城市.上述的人均是汉族,男性,年龄分别为19,19,20,21. 展开
1.lihan liaoning huluodao 2. zhuqiang jiangsu changzhu
3.wangjiangang liaoning anshan 4.zhanghongwei shandong zhucheng
也就是,人名,省份,城市.上述的人均是汉族,男性,年龄分别为19,19,20,21. 展开
1个回答
展开全部
分析:
跟据楼主的意思!我们可以得出,这个程序只要求我们写查询通迅录的内容
而通迅录的内容,可以通过初始化得出!
简而言之:写一个查询函数
呵呵···把问题相通了,我们就开始写算法吧let's go!!!
----------------------------------------------------------------
算法:
1.获得用户的输入 (就是要查询的对象的名字)
2.查询 (在这我用穷举通迅录的方式查询了,^_^&&^_^)
3.输出查询结果
算法就这样被我们征服了!!!呵呵~~好有成就感哇!!
但我们现在还不能开始编码,我得们先想好怎么编,要做到胸有成竹!!!
那我现在来想一下该怎么编码吧!let's go!!!
----------------------------------------------------------------
要保存通迅的信息,那么我们得用一个结构体吧:
struct friends
{
char name[20]; /* 名字不能写得太长哦 */
char province[20]; /* 省份 */
char city[20]; /* 所在城市 */
char nation[20]; /* 民族 */
char sex[2]; /* 性别 M/F */
int age; /* 年龄 */
};
要获得用户输入,要用个 char search_name[20];
查询结果反回一个数,记录对象在通迅录中的位置:int index;
查询中有用要循环,用一个记数器: int i;
----------------------------------------------------------------
OK,该用的变量我们都想好了!算法我们也想好了。还等什么呢,开始编码吧
呵呵~~是不是等这个时候都等得急了~~~~~
-------------------------------------------------------------------
*******************************************************************
******* 程序实现:
*******************************************************************
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/* 定义保存通迅录的信息 */
struct friends
{
char name[20]; /* 名字 */
char province[20]; /* 省份 */
char city[20]; /* 所在城市 */
char nation[20]; /* 民族 */
char sex[2]; /* 性别 M/F */
int age; /* 年龄 */
};
void getname (char search_name[]);
int search (struct friends friend_list[], char search_name[]);
void print_result(struct friends friend_list[], int index);
int main (void)
{
int index;
char search_name[20];
struct friends friend_list[4] = {
{"lihan", "liaoning", "huluodao","han","M",19},
{"zhuqiang", "jiangsu", "changzhu", "han","M",19},
{"wangjiangang", "liaoning", "anshan","han","M",20},
{"zhanghongwei", "shandong", "zhucheng", "han","M",21},
};
(void) getname (search_name); /* 获得用户输入 */
index = search (friend_list, search_name); /* 查询 */
(void) print_result (friend_list,index); /* 打印结果 */
return 0;
}
/****************************************
*** 函数名:getname
*** 功能:获得用户要查询的对象的名字
****************************************/
void getname (char search_name[])
{
printf ("Pleace enter the name of your friends you want to search>>");
scanf ("%s", search_name);
}
/****************************************
*** 函数名:search
*** 功能:查询对象
****************************************/
int search (struct friends friend_list[], char search_name[])
{
int i;
/* 穷举通迅录 */
for (i = 0; i < 4; ++i)
{
if (strcmp(friend_list[i].name, search_name) == 0)
{
return (i);
}
}
if (i == 4)
{
printf ("I am sorry! there is nobody by the name you enter!\n");
fflush(stdin);
getchar();
exit (0);
}
}
/****************************************
*** 函数名:print_result
*** 功能:打印结果
****************************************/
void print_result(struct friends friend_list[], int index)
{
printf ("the imformation of %s:\n", friend_list[index].name);
printf ("------------------------------------------------\n");
printf (" NAME: %-s\n", friend_list[index].name);
printf ("PROVINCE: %-s\n", friend_list[index].province);
printf (" CITY: %-s\n", friend_list[index].city);
printf (" NATION: %-s\n", friend_list[index].nation);
printf (" SEX: %-s\n", friend_list[index].sex);
printf (" AGE: %-d\n", friend_list[index].age);
printf ("-------------------------------------------------\n");
fflush(stdin);
getchar();
}
*****************************************************************************
*****************************************************************************
呵呵~~一口气把它写出来了!!!前期写算法是很重要的!!
现在还没结束!!我们要先来测试一下!!
--------------------------------------
Pleace enter the name of your friends you want to search>>lihan
the imformation of lihan:
------------------------------------------------
NAME: lihan
PROVINCE: liaoning
CITY: huluodao
NATION: han
SEX: M
AGE: 19
-------------------------------------------------
--------------------------------------
Pleace enter the name of your friends you want to search>>lbmzwyy
I am sorry! there is nobody by the name you enter!
说明成功了~~~呵呵~~太高兴了~~
--------------------------------------
请记注一点:克制编码的诱惑
无论多么小的问题都要先分析好问题,想好算法,才能开始编码!!!我相信这样做一定对你有好处的!
跟据楼主的意思!我们可以得出,这个程序只要求我们写查询通迅录的内容
而通迅录的内容,可以通过初始化得出!
简而言之:写一个查询函数
呵呵···把问题相通了,我们就开始写算法吧let's go!!!
----------------------------------------------------------------
算法:
1.获得用户的输入 (就是要查询的对象的名字)
2.查询 (在这我用穷举通迅录的方式查询了,^_^&&^_^)
3.输出查询结果
算法就这样被我们征服了!!!呵呵~~好有成就感哇!!
但我们现在还不能开始编码,我得们先想好怎么编,要做到胸有成竹!!!
那我现在来想一下该怎么编码吧!let's go!!!
----------------------------------------------------------------
要保存通迅的信息,那么我们得用一个结构体吧:
struct friends
{
char name[20]; /* 名字不能写得太长哦 */
char province[20]; /* 省份 */
char city[20]; /* 所在城市 */
char nation[20]; /* 民族 */
char sex[2]; /* 性别 M/F */
int age; /* 年龄 */
};
要获得用户输入,要用个 char search_name[20];
查询结果反回一个数,记录对象在通迅录中的位置:int index;
查询中有用要循环,用一个记数器: int i;
----------------------------------------------------------------
OK,该用的变量我们都想好了!算法我们也想好了。还等什么呢,开始编码吧
呵呵~~是不是等这个时候都等得急了~~~~~
-------------------------------------------------------------------
*******************************************************************
******* 程序实现:
*******************************************************************
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/* 定义保存通迅录的信息 */
struct friends
{
char name[20]; /* 名字 */
char province[20]; /* 省份 */
char city[20]; /* 所在城市 */
char nation[20]; /* 民族 */
char sex[2]; /* 性别 M/F */
int age; /* 年龄 */
};
void getname (char search_name[]);
int search (struct friends friend_list[], char search_name[]);
void print_result(struct friends friend_list[], int index);
int main (void)
{
int index;
char search_name[20];
struct friends friend_list[4] = {
{"lihan", "liaoning", "huluodao","han","M",19},
{"zhuqiang", "jiangsu", "changzhu", "han","M",19},
{"wangjiangang", "liaoning", "anshan","han","M",20},
{"zhanghongwei", "shandong", "zhucheng", "han","M",21},
};
(void) getname (search_name); /* 获得用户输入 */
index = search (friend_list, search_name); /* 查询 */
(void) print_result (friend_list,index); /* 打印结果 */
return 0;
}
/****************************************
*** 函数名:getname
*** 功能:获得用户要查询的对象的名字
****************************************/
void getname (char search_name[])
{
printf ("Pleace enter the name of your friends you want to search>>");
scanf ("%s", search_name);
}
/****************************************
*** 函数名:search
*** 功能:查询对象
****************************************/
int search (struct friends friend_list[], char search_name[])
{
int i;
/* 穷举通迅录 */
for (i = 0; i < 4; ++i)
{
if (strcmp(friend_list[i].name, search_name) == 0)
{
return (i);
}
}
if (i == 4)
{
printf ("I am sorry! there is nobody by the name you enter!\n");
fflush(stdin);
getchar();
exit (0);
}
}
/****************************************
*** 函数名:print_result
*** 功能:打印结果
****************************************/
void print_result(struct friends friend_list[], int index)
{
printf ("the imformation of %s:\n", friend_list[index].name);
printf ("------------------------------------------------\n");
printf (" NAME: %-s\n", friend_list[index].name);
printf ("PROVINCE: %-s\n", friend_list[index].province);
printf (" CITY: %-s\n", friend_list[index].city);
printf (" NATION: %-s\n", friend_list[index].nation);
printf (" SEX: %-s\n", friend_list[index].sex);
printf (" AGE: %-d\n", friend_list[index].age);
printf ("-------------------------------------------------\n");
fflush(stdin);
getchar();
}
*****************************************************************************
*****************************************************************************
呵呵~~一口气把它写出来了!!!前期写算法是很重要的!!
现在还没结束!!我们要先来测试一下!!
--------------------------------------
Pleace enter the name of your friends you want to search>>lihan
the imformation of lihan:
------------------------------------------------
NAME: lihan
PROVINCE: liaoning
CITY: huluodao
NATION: han
SEX: M
AGE: 19
-------------------------------------------------
--------------------------------------
Pleace enter the name of your friends you want to search>>lbmzwyy
I am sorry! there is nobody by the name you enter!
说明成功了~~~呵呵~~太高兴了~~
--------------------------------------
请记注一点:克制编码的诱惑
无论多么小的问题都要先分析好问题,想好算法,才能开始编码!!!我相信这样做一定对你有好处的!
参考资料: 原创
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询