通讯录学习
通讯录学习
学习自C语言实现通讯录
再检查发现好多小错误,笑
新学习的语法 realloc strerror
//电话通讯录
#include<stdio.h>
//声明个人信息结构体
typedef struct person
{
char name[20];
char sex[5];
int age;
int tele[11];
} person;
//声明包含所有人信息的结构体类型 (静态)
typedef struct contacts
{
person allhum[1000]//假设最多存1000人
int count;//count 来表示现有人数
} contacts;
//声明包含所有人信息的结构体类型 (动态)
typedef struct contacts
{
person* allhum;
int count;
int max;
}
//创建菜单
void menu()
{
printf("————————");
printf("|1.录入 2.查找|");
printf("|3.删除 4.修改|");
printf("|5.排序 6.显示|");
printf("| 0.退出 |");
printf("————————");
}
//用户选择
enmu select
{
quit,//0 枚举类型 和上面对应
add,
find,
delete,
change,
sort,
print
} ;
void test()
{
int n;
contacts con;;、//意义不明 之后在看
do
{
menu();
printf("请选择你的操作->");
scanf("%d",&n);
switch(n)
{
case quit:
printf("退出");
break;
/* case quit:
printf("退出");
free(con.human);
con.human=NULL;
break;//动态版本
*/
case add://添加
break;
case find://查找
break;
case delete://查找
break;
case change://修改
break;
case sort://排序
break;
case print://打印
break;
default:
printf("操作非法,重新选择\n");
break;
}
}while(n);
}
//初始化通讯录
//静态版本
void initcontacts(contacts* p)
{
p->count = 0;
memset(p->allhum,0,sizeof(p->allhum));
}
//动态版本 假设预存三个人信息
void initcontacts(contacts* p)
{
p->allhum=(person*)malloc(sizeof(person)*3);
p->count = 0;
p->max = 3;
}
//扩容
void enlarge(contacts *p)
{
p->max += 3;
person* temp=(person*)realloc(p->allhum,sizeof(people)*p->max);//指针地址变换
if(temp == NULL)
{
printf("%s\n",strerror(errno));
return;
}
p->allhum=temp;
}
//列出通讯录信息
void printcontacts(const contacts *p)
{
printf("-15s%-5s%-5d%-11d","姓名","性别","年龄","电话号码")
for(int i=0;i<-p->count;i++)
{
printf("-15s%-5s%-5d%-11d",p->allhum[i].name,p->allhum[i].sex,p->allhum[i].age,p->allhum[i].tele);
}
}
//录入信息
void addcontacts(contacts *p){
if(p->count==1000)//静态设置最多为1000
{
printf("通讯录满了");
return;
}
/*
if(p-count == p->max)
{
enlarge(p);
}动态修改版本
*/
printf("请输入姓名");
scanf("%s",p->allhum[p->count].name);//这是一个数组,最后一个元素编号即是count
~~~~~~//后面的类推 懒得打了
p->count++;
printf("录入完毕");
//查找姓名 返回对应人的下标
int findpeople(char *name_x,const contacts* p)
{
int i = 0;
for(i=0;i<p->count;i++)
{
if(strcmp(name_x,p->allhum[i].name)==0)
return i
}
return i;
}
}
//查询信息
void findinformation(const contacts* p)
{
char name_x[20];
printf("请输入姓名");
scanf("%s",name_x);
int i=findpeople(name_x,p);//返回对应名称在通讯录的下标
if (i == -1)
printf("查无此人\n");
else
{
printf("-15s%-5s%-5d%-11d","姓名","性别","年龄","电话号码")
printf("-15s%-5s%-5d%-11d",p->allhum[i].name,p->allhum[i].sex,p->allhum[i].age,p->allhum[i].tele);
}
}
//删除人的信息
void deletepeople(contacts* p)
{
char name_x[15];
printf("请输入姓名");
scanf("%s",name_x);
int i=findpeople(name_x,p);
if(i==-1)printf("查无此人");
else{
int j;
for(j=1;j<p->count-1;j++)
{
p->allhum[j]=p->allhum[j+1];//后面的替换前面的 后面全部来一遍
}
p->count--;
printf("删除成功\n");
}
}
//修改信息
void changepeople(contacts* p)
{
char name_x[15];
printf("请输入姓名");
scanf("%s",name_x);
int i=findpeople(name_x,p);
printf("请选择修改项目");
printf("——————");
printf("| 1.姓名 |");
printf("| 2.性别 |");
printf("| 3.年龄 |");
printf("|4.电话号码|");
printf("| 0.退出 |");
printf("——————");
enmu changeselect
{
skip,
name_x,
sex_x,
address_x,
telephone_x
}
int n;
scanf("%d",&n);
switch(n)
{
case name_x:
scanf("%s",p->allhum[i].name);
printf("修改完毕");
case sex_x:
scanf("%s",p->allhum[i].sex);
printf("修改完毕");
case age_x:
scanf("%s",p->allhum[i].age);
printf("修改完毕");
case address_x:
scanf("%s",p->allhum[i].tele);
printf("修改完毕");
case skip_x:
printf("取消修改");
break;
}
}