首先写一个菜单和选项界面。(test.c)
void menu(){
printf("***************************************\n");
printf("****** 1.add 2.delete *******\n");
printf("****** 3.search 4.modfiy *******\n");
printf("****** 5.show *******\n");
printf("****** 0.exit *******\n");
printf("***************************************\n");
}
用一个switch语句来执行操作,do while语句循环。
do
{
menu();
printf("请选择你的操作:>\n");
scanf("%d", &input);
switch (input)
{
case add:
Addcontact(&con);
break;
case deleteby:
con_delete(&con);
break;
case search:
con_search(&con);
break;
case modfiy:
con_modfiy(&con);
break;
case conshow:
con_show(&con);
break;
case exit:
break;
default:
printf("选择错误\n");
break;
}
} while (input);
PS:一定要初始化结构体。
初始化函数:Initialize()
void Initialize(struct contact *p){
memset(p->pa, 0, sizeof(p->pa));
p->size = 0;
}
头文件声明结构体类型和函数。(contact.h)
#include<stdio.h>
#include<string.h>
enum max{
MAX=1000,
MAX_name=20,
MAX_sex=5,
MAX_num=20,
MAX_address=20,
};
enum options{
exit,
add,
deleteby,
search,
modfiy,
conshow,
};
struct people
{
char name[MAX_name];
int age;
char sex[MAX_sex];
char num[MAX_num];
char address[MAX_address];
};
struct contact
{
struct people pa[MAX];
int size;
};
void Initialize(struct contact *p);
void Addcontact(struct contact *p);
void con_show(const struct contact *p);
void con_delete(struct contact *p);
void con_search(const struct contact *p);
void con_modfiy(struct contact *p);
功能函数实现1:
添加通讯录:Addcontac()
void Addcontact(struct contact *p){
if(p->size == MAX)//判断通讯录是否已满
{
printf("通讯录已满");
}
else//没满则可以添加
{
printf("姓名:>");
scanf("%s", p->pa[p->size].name);