通讯录满足以下要求:通讯录可以用来存储1000个人的信息,每个人的信息包括: 姓名、性别、年龄、电话、住址
通讯录有一下功能:1. 添加联系人信息
2. 删除指定联系人信息
3. 查找指定联系人信息
4. 修改指定联系人信息
5. 显示所有联系人信息
6. 清空所有联系人
7. 以名字排序所有联系人
函数头文件,为了方便通讯录用结构体来存储。
#ifndef __LIST_H_
#define __LIST_H_
#include #include #pragma warning(disable:4996)
#define MAX 1000
#define NAME_LENGTH 10
#define SEX_LENGTH 5
#define ADDR_LENGTH 10
#define NUMB_LENGTH 20
#define AGE_LENGTH 5
void menu();
int add_list(struct list* ls);
int delete_list(struct list* ls);
int find_list(struct list *ls);
int revise_list(struct list*ls);
int show_list(struct list* ls);
int revise_list(struct list*ls);
int empty_list(struct list*ls);
int name_list(struct list*ls);
struct people
{
char name[NAME_LENGTH];
char sex[SEX_LENGTH];
char age[AGE_LENGTH];
char numb[NUMB_LENGTH];
char addr[ADDR_LENGTH];
};
struct list
{
struct people peo[MAX];
int user ;
};
#endif
程序的main函数:
#include "list.h"
void menu() //菜单,提示使用者如何使用。
{
printf("*****************************************************************\n");
printf("*****1.添加联系人信息************2.删除指定联系人信息************\n");
printf("*****3.查找指定联系人信息********4.修改指定联系人信息 ***********\n");
printf("*****5.显示所有联系人信息********6.清空所有联系人****************\n");
printf("*****7.以名字排序所有联系人 *****0.exit**************************\n");
printf("*****************************************************************\n");
}
int main()
{
struct list ls;
ls.user = 0;
int input = 0;
struct list* p = &ls;
menu();
do
{
printf("Please Choose: \n ");
scanf("%d", &input);
switch (input)
{
case 0:
exit(0);
break;
case 1:
add_list(p); //添加联系人
break;
case 2:
delete_list(p);//删除指定联系人
break;
case 3:
find_list(p);//寻找联系人
break;
case 4:
revise_list(p);//修改指定联系人信息
break;
case 5:
show_list(p);//显示所有联系人信息
break;
case 6:
empty_list(p);//清空所有联系人信息
break;
case 7:
name_list(p);//以名字排序所有联系人信息
break;
default:
printf("Error Input\n");
break;
}
} while (1);
system("pause");
return 0;
}
main函数中switch语句中各个函数的实现:
#include "list.h"
int add_list(struct list* ls) //由于结构体有些大,使用指针传参,提升性能
{
printf("Please Enter name\n");
scanf("%s",ls->peo[ls->user].name );
printf("Please Enter sex\n");
scanf("%s", ls->peo[ls->user].sex);
printf("Please Enter age\n");
scanf("%s", ls->peo[ls->user].age);
printf("Please Enter numb\n");
scanf("%s", ls->peo[ls->user].numb);
printf("Please Enter addr\n");
scanf("%s", ls->peo[ls->user].addr);
(ls->user)++;
return 0;
}
int delete_list(struct list* ls)
{
int i = 0;
int ret = find_list(ls);
if (ret != -1)
{
for (i = 0; i <= ls->user ; i++)
{
ls->peo[i] = ls->peo[i + 1];
}
ls->user--;
printf("Delete Over\n");
return 1;
}
else
{
return -1;
}
}
int find_list(struct list *ls)
{
int i = 0;
char name[NAME_LENGTH];
printf("Please Enter Find Name\n ");
scanf("%s", &name);
for (i; i user; i++)
{
if(strcmp(name, ls->peo[i].name)==0)
{
printf("Name Exist\n");
return i;
}
else
{
printf("Not Exist\n");
return 0;
}
}
}
int revise_list(struct list*ls)
{
int i = 0;
int ret = find_list(ls);
printf("Please Enter Your Choose\n");
printf("######1.name\n#######");
printf("######2.sex\n########");
printf("######3.age\n########");
printf("######4.number\n#####");
printf("######5.addr\n#######");
int Choose;
scanf("%d", &Choose);
switch (Choose)
{
case 1:
scanf("%s", &ls->peo[ret].name);
break;
case 2:
scanf("%s", &ls->peo[ret].sex);
break;
case 3:
scanf("%s", &ls->peo[ret].age);
break;
case 4:
scanf("%s", &ls->peo[ret].numb);
break;
case 5:
scanf("%s", &ls->peo[ret].addr);
break;
default :
printf("Error input\n");
break;
}
return 0;
}
int show_list(struct list* ls)
{
int i = 0;
for (i; i < ls->user; i++)
{
printf("name is:%s\n", ls->peo[i].name);
printf("sex is:%s\n", ls->peo[i].sex);
printf("age is:%s\n", ls->peo[i].age);
printf("numb is:%s\n", ls->peo[i].numb);
printf("addr is:%s\n", ls->peo[i].addr);
}
return 0;
}
int empty_list(struct list*ls)
{
int i;
for (i = 0; i < ls->user; i++)
{
memset(ls->peo[i].addr, '\0', sizeof(ls->peo[i].addr[1]));
memset(ls->peo[i].age, '\0', sizeof(ls->peo[i].age));
memset(ls->peo[i].name, '\0', sizeof(ls->peo[i].name[1]));
memset(ls->peo[i].numb, '\0', sizeof(ls->peo[i].numb[1]));
memset(ls->peo[i].sex, '\0', sizeof(ls->peo[i].sex[1]));
}
printf("Empty Over \n ");
return 0;
}
int name_list(struct list*ls)
{
int i = 0;
int j = 0;
for (i = 0; i < ls->user ; i++)
{
for (j = 0; j < ls->user-i; j++)
{
if (strcmp(ls->peo[j].name,ls->peo[j + 1].name)>0)
{
char *tmp = ls->peo[j].name;
*ls->peo[j].name = *ls->peo[j + 1].name;
*ls->peo[j + 1].name = *tmp;
}
}
}
return 0;
}
由于学识有限, 编写此粗糙通讯录,若有见解,欢迎随时指教!