2023-12-11 23:23:15 +08:00
|
|
|
#include <stdio.h>
|
2023-12-12 13:10:23 +08:00
|
|
|
#include <stdlib.h>
|
2023-12-12 23:48:07 +08:00
|
|
|
#include <string.h>
|
2023-12-12 13:10:23 +08:00
|
|
|
|
2023-12-12 23:48:07 +08:00
|
|
|
int contact_digit=0;//存入了多少位置
|
2023-12-12 17:13:42 +08:00
|
|
|
|
2023-12-12 13:10:23 +08:00
|
|
|
struct Contact
|
2023-12-11 23:51:59 +08:00
|
|
|
{
|
2023-12-12 23:48:07 +08:00
|
|
|
char name[20];//名字
|
|
|
|
char sex[20];//性别
|
2023-12-11 23:51:59 +08:00
|
|
|
char email[20];//电子邮箱
|
2023-12-12 17:13:42 +08:00
|
|
|
char number[11];//电话号码
|
|
|
|
struct Contact *next;//下一个联系人
|
2023-12-12 13:10:23 +08:00
|
|
|
};
|
2023-12-12 17:13:42 +08:00
|
|
|
struct Contact *con[99];//声明结构体
|
|
|
|
|
2023-12-12 23:48:07 +08:00
|
|
|
void view_contact(void)//查看联系人
|
|
|
|
{
|
|
|
|
int contact_number;//第几个联系人
|
|
|
|
for(contact_number=1;contact_number<contact_digit;contact_number++)//查找非空联系人
|
|
|
|
{
|
|
|
|
if(con[contact_number]!=NULL)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(contact_digit==0 ||(contact_digit==contact_number && con[contact_number]==NULL))
|
|
|
|
{
|
|
|
|
printf("通讯录没有联系人\n");
|
|
|
|
while(getchar()!='\n');//删除缓存
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
while(1)
|
|
|
|
{
|
|
|
|
printf("\n姓名:%s ",con[contact_number]->name);
|
|
|
|
printf("性别:%s ",con[contact_number]->sex);
|
|
|
|
printf("电子号码:%s ",con[contact_number]->number);
|
2023-12-13 01:29:31 +08:00
|
|
|
printf("电子邮箱:%s",con[contact_number]->email);
|
2023-12-12 23:48:07 +08:00
|
|
|
if(con[contact_number]->next==NULL)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
con[contact_number]=con[contact_number]->next;//指向下一个联系人
|
|
|
|
}
|
|
|
|
}
|
2023-12-13 01:29:31 +08:00
|
|
|
printf("\n");
|
2023-12-12 23:48:07 +08:00
|
|
|
while(getchar()!='\n');//清除缓存
|
|
|
|
}
|
|
|
|
|
|
|
|
int find_numbering(const char name[])//查找编号
|
|
|
|
{
|
|
|
|
int i=0;//查看的次数
|
|
|
|
int contact_number;//第几个联系人
|
|
|
|
const int length=strlen(name);//需要查找的名字长度
|
|
|
|
for(contact_number=1;contact_number<contact_digit;contact_number++)//查找非空联系人
|
|
|
|
{
|
|
|
|
if(con[contact_number]!=NULL)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(contact_digit==0 ||(contact_digit==contact_number && con[contact_number]==NULL))
|
|
|
|
{
|
|
|
|
while(getchar()!='\n');//删除缓存
|
|
|
|
return 100;
|
|
|
|
}
|
|
|
|
while(1)
|
|
|
|
{
|
|
|
|
for(i=0;i<length;i++)
|
|
|
|
{
|
|
|
|
if(name[i]!=con[contact_number]->name[i])
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(length==i)
|
|
|
|
{
|
|
|
|
return contact_number;
|
|
|
|
}
|
|
|
|
else if(con[contact_number]->next==NULL)
|
|
|
|
{
|
|
|
|
return 100;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
con[contact_number]=con[contact_number]->next;//指向下一个联系人
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void remove_contact(int contact_number)//删除联系人
|
|
|
|
{
|
|
|
|
if(contact_number==100)
|
|
|
|
{
|
|
|
|
printf("\n查无此人\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
free(con[contact_number]);//释放这个联系人内容
|
|
|
|
con[contact_number]=NULL;//将它指向空
|
|
|
|
for(contact_number--;contact_number>0;contact_number--)//查找前一个有效地址
|
|
|
|
{
|
|
|
|
if(con[contact_number]!=NULL)
|
|
|
|
{
|
|
|
|
int i=0;//后面一位的有效值
|
|
|
|
if(i==contact_digit&&con[i]==NULL)//如果没有找到后一个就标记空
|
|
|
|
{
|
|
|
|
con[contact_number]->next=NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
for(i=contact_number+1;i<=contact_digit;i++)//查找后一位有效值
|
|
|
|
{
|
|
|
|
if(con[i]!=NULL)//如果查找到后一位就将前一位链接到最后一位
|
|
|
|
{
|
|
|
|
con[contact_number]->next=con[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
printf("删除成功\n");
|
|
|
|
}
|
|
|
|
|
2023-12-13 01:29:31 +08:00
|
|
|
void revise_contact(const int contact_number)//修改信息
|
|
|
|
{
|
|
|
|
if(contact_number==100)
|
|
|
|
{
|
|
|
|
printf("\n查无此人\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
int pick=0;
|
|
|
|
start:
|
|
|
|
printf("\n1.修改名字\n");
|
|
|
|
printf("2.修改性别\n");
|
|
|
|
printf("3.修改电话号码\n");
|
|
|
|
printf("4.修改电子邮箱\n");
|
|
|
|
printf("5.返回主页\n");
|
|
|
|
printf("请输入:");
|
|
|
|
while((scanf("%d",&pick))!=1)
|
|
|
|
{
|
|
|
|
while(getchar()!='\n');//清除缓存
|
|
|
|
printf("请输入合法的数字:");
|
|
|
|
scanf("%d",&pick);
|
|
|
|
}
|
|
|
|
switch(pick)
|
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
{
|
|
|
|
printf("请输入姓名:");
|
|
|
|
scanf("%s",con[contact_number]->name);
|
|
|
|
printf("修改成功!\n当前姓名为:%s",con[contact_number]->name);
|
|
|
|
while(getchar()!='\n');//清除缓存
|
|
|
|
goto start;//回到主菜单
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 2:
|
|
|
|
{
|
|
|
|
printf("请输入性别:");
|
|
|
|
scanf("%s",con[contact_number]->sex);
|
|
|
|
printf("修改成功!\n当前性别为:%s",con[contact_number]->sex);
|
|
|
|
while(getchar()!='\n');//清除缓存
|
|
|
|
goto start;//回到主菜单
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 3:
|
|
|
|
{
|
|
|
|
printf("请输入电话号码:");
|
|
|
|
number_tab:
|
|
|
|
scanf("%s",con[contact_number]->number);
|
|
|
|
for(int i=0;i<11;i++)
|
|
|
|
{
|
|
|
|
if(strlen(con[contact_number]->number)!=11||con[contact_number]->number[i]<'0'||con[contact_number]->number[i]>'9')
|
|
|
|
{
|
|
|
|
printf("请输入11位数字:");
|
|
|
|
while(getchar()!='\n');//清除缓存
|
|
|
|
goto number_tab;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
printf("修改成功!\n当前电话号码为:%s",con[contact_number]->number);
|
|
|
|
while(getchar()!='\n');//清除缓存
|
|
|
|
goto start;//回到主菜单
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 4:
|
|
|
|
{
|
|
|
|
printf("请输入电子邮箱:");
|
|
|
|
email_tab:
|
|
|
|
scanf("%s",con[contact_number]->email);
|
|
|
|
for(int i=0;i<=strlen(con[contact_number]->email);i++)
|
|
|
|
{
|
|
|
|
if(con[contact_number]->email[i]=='@')
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if(i==strlen(con[contact_number]->email))
|
|
|
|
{
|
|
|
|
while(getchar()!='\n');
|
|
|
|
printf("请输入正确格式的邮箱:");
|
|
|
|
goto email_tab;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
printf("修改成功!\n当前电子邮箱为:%s",con[contact_number]->email);
|
|
|
|
while(getchar()!='\n');//清除缓存
|
|
|
|
goto start;//回到主菜单
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 5:
|
|
|
|
{
|
|
|
|
while(getchar()!='\n');//清除缓存
|
|
|
|
return;//结束程序
|
|
|
|
}
|
|
|
|
default :
|
|
|
|
{
|
|
|
|
while(getchar()!='\n');//清除缓存
|
|
|
|
printf("请输入对应的模式\n");
|
|
|
|
goto start;//回到主菜单
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-12 23:48:07 +08:00
|
|
|
void see_contact(const int contact_number)//查看单个联系人
|
|
|
|
{
|
|
|
|
if(contact_number==100)
|
|
|
|
{
|
|
|
|
printf("\n查无此人\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
printf("\n姓名:%s ",con[contact_number]->name);
|
|
|
|
printf("性别:%s ",con[contact_number]->sex);
|
|
|
|
printf("电子号码:%s ",con[contact_number]->number);
|
|
|
|
printf("电子邮箱:%s\n",con[contact_number]->email);
|
|
|
|
}
|
|
|
|
|
|
|
|
void add_contact(void)//新增联系人
|
2023-12-12 17:13:42 +08:00
|
|
|
{
|
2023-12-13 01:29:31 +08:00
|
|
|
int contact_number=1;//增加位置变量
|
2023-12-12 23:48:07 +08:00
|
|
|
//判断增加的位置
|
2023-12-13 01:29:31 +08:00
|
|
|
for(contact_number=1;contact_number<=contact_digit;contact_number++)
|
2023-12-12 23:48:07 +08:00
|
|
|
{
|
|
|
|
if(con[contact_number]==NULL)//查找有无空位
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if(contact_number==contact_digit)//需要新增的话
|
|
|
|
{
|
|
|
|
contact_number++;//设置本次的位置
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
//分配内存
|
2023-12-12 17:13:42 +08:00
|
|
|
con[contact_number]=(struct Contact *)malloc(sizeof(struct Contact));//给新联系人分配内存
|
|
|
|
//姓名
|
|
|
|
printf("请输入姓名:");
|
|
|
|
scanf("%s",con[contact_number]->name);
|
|
|
|
//性别
|
2023-12-12 23:48:07 +08:00
|
|
|
printf("请输入性别:");
|
|
|
|
scanf("%s",con[contact_number]->sex);
|
|
|
|
//电话号码
|
|
|
|
printf("请输入电话号码:");
|
|
|
|
number_tab:
|
|
|
|
scanf("%s",con[contact_number]->number);
|
|
|
|
for(int i=0;i<11;i++)
|
2023-12-12 17:13:42 +08:00
|
|
|
{
|
2023-12-12 23:48:07 +08:00
|
|
|
if(strlen(con[contact_number]->number)!=11||con[contact_number]->number[i]<'0'||con[contact_number]->number[i]>'9')
|
|
|
|
{
|
|
|
|
printf("请输入11位数字:");
|
|
|
|
while(getchar()!='\n');//清除缓存
|
|
|
|
goto number_tab;
|
|
|
|
}
|
2023-12-12 17:13:42 +08:00
|
|
|
}
|
|
|
|
//电子邮箱
|
2023-12-12 23:48:07 +08:00
|
|
|
printf("请输入电子邮箱:");
|
|
|
|
email_tab:
|
2023-12-12 17:13:42 +08:00
|
|
|
scanf("%s",con[contact_number]->email);
|
2023-12-12 23:48:07 +08:00
|
|
|
for(int i=0;i<=strlen(con[contact_number]->email);i++)
|
|
|
|
{
|
|
|
|
if(con[contact_number]->email[i]=='@')
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if(i==strlen(con[contact_number]->email))
|
|
|
|
{
|
|
|
|
while(getchar()!='\n');
|
|
|
|
printf("请输入正确格式的邮箱:");
|
|
|
|
goto email_tab;
|
|
|
|
}
|
|
|
|
}
|
2023-12-12 17:13:42 +08:00
|
|
|
//下一个地址
|
2023-12-12 23:48:07 +08:00
|
|
|
if(contact_number==contact_digit)//最后一个
|
2023-12-12 17:13:42 +08:00
|
|
|
{
|
2023-12-12 23:48:07 +08:00
|
|
|
for(int i=contact_number-1;i>0;i--)//查找前一个有效地址
|
|
|
|
{
|
|
|
|
if(con[i]!=NULL)
|
|
|
|
{
|
|
|
|
|
|
|
|
con[i]->next=con[contact_number];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2023-12-12 17:13:42 +08:00
|
|
|
con[contact_number]->next=NULL;
|
|
|
|
}
|
2023-12-12 23:48:07 +08:00
|
|
|
else//从中间新增
|
2023-12-12 17:13:42 +08:00
|
|
|
{
|
2023-12-12 23:48:07 +08:00
|
|
|
for(int i=contact_number-1;i>0;i--)//查找前一个有效地址
|
|
|
|
{
|
|
|
|
if(con[i]!=NULL)
|
|
|
|
{
|
|
|
|
|
|
|
|
con[i]->next=con[contact_number];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for(int i=contact_number+1;i<=contact_digit;i++)//查找后一位有效值
|
|
|
|
{
|
|
|
|
if(con[i]!=NULL)
|
|
|
|
{
|
|
|
|
con[contact_number]->next=con[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2023-12-12 17:13:42 +08:00
|
|
|
}
|
2023-12-13 01:29:31 +08:00
|
|
|
while(getchar()!='\n');//清除缓存
|
|
|
|
contact_digit++;//增加总数量
|
2023-12-12 23:48:07 +08:00
|
|
|
printf("\n新建成功");
|
|
|
|
see_contact(contact_number);//输出单个信息
|
2023-12-12 17:13:42 +08:00
|
|
|
}
|
2023-12-12 23:48:07 +08:00
|
|
|
|
2023-12-12 17:13:42 +08:00
|
|
|
int homepage(void)//主页
|
2023-12-12 13:10:23 +08:00
|
|
|
{
|
2023-12-12 17:13:42 +08:00
|
|
|
printf("————通讯录————\n");
|
|
|
|
printf("1.查看联系人\n");
|
|
|
|
printf("2.查找联系人\n");
|
2023-12-13 01:29:31 +08:00
|
|
|
printf("3.修改联系人\n");
|
2023-12-12 23:48:07 +08:00
|
|
|
printf("4.删除联系人\n");
|
|
|
|
printf("5.新建联系人\n");
|
|
|
|
printf("6.退出通讯录\n");
|
2023-12-12 17:13:42 +08:00
|
|
|
int accept = 0;//接受用户输入的模式
|
|
|
|
printf("请输入:");
|
2023-12-12 23:48:07 +08:00
|
|
|
accept_tab://接受输入标签
|
2023-12-12 17:13:42 +08:00
|
|
|
while( ( scanf("%d",&accept) ) != 1)
|
|
|
|
{
|
|
|
|
while(getchar() != '\n');//清楚缓存
|
|
|
|
printf("请输入合法数字:");
|
|
|
|
scanf("%d",&accept);
|
|
|
|
}
|
2023-12-12 23:48:07 +08:00
|
|
|
if(accept>0&&accept<6)
|
2023-12-12 17:13:42 +08:00
|
|
|
{
|
|
|
|
return accept;
|
|
|
|
}
|
2023-12-12 23:48:07 +08:00
|
|
|
else if(accept==6)
|
2023-12-12 17:13:42 +08:00
|
|
|
{
|
2023-12-12 23:48:07 +08:00
|
|
|
printf("欢迎下次使用\n");
|
2023-12-12 17:13:42 +08:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
while(getchar() != '\n');//清楚缓存
|
|
|
|
printf("请输入正确的模式:");
|
|
|
|
goto accept_tab;
|
|
|
|
}
|
|
|
|
}
|
2023-12-12 23:48:07 +08:00
|
|
|
|
2023-12-12 17:13:42 +08:00
|
|
|
int main(void)
|
2023-12-11 23:23:15 +08:00
|
|
|
{
|
2023-12-12 23:48:07 +08:00
|
|
|
char name[10];//要接受用户输入的名字
|
|
|
|
int contact_number;//查找名字所在的结构体返回值
|
|
|
|
int homepage_pick=0;//主页选择变量
|
|
|
|
while(1)
|
|
|
|
{
|
|
|
|
homepage_pick=homepage();//接收主页选择
|
|
|
|
switch(homepage_pick)
|
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
{
|
|
|
|
view_contact();//输出所有联系人
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 2:
|
|
|
|
{
|
|
|
|
printf("\n请输入需要查找联系人的姓名:");
|
|
|
|
scanf("%s",name);
|
|
|
|
contact_number= find_numbering(name);//查找名字所在的结构体
|
|
|
|
see_contact(contact_number);//输出单个信息
|
|
|
|
break;
|
|
|
|
}
|
2023-12-13 01:29:31 +08:00
|
|
|
case 3:
|
|
|
|
{
|
|
|
|
printf("\n请输入需要修改联系人的姓名:");
|
|
|
|
scanf("%s",name);
|
|
|
|
contact_number= find_numbering(name);//查找名字所在的结构体
|
|
|
|
revise_contact(contact_number);//修改联系人
|
|
|
|
break;
|
|
|
|
}
|
2023-12-12 23:48:07 +08:00
|
|
|
case 4:
|
|
|
|
{
|
|
|
|
printf("\n请输入需要删除联系人的姓名:");
|
|
|
|
scanf("%s",name);
|
|
|
|
contact_number= find_numbering(name);//查找名字所在的结构体
|
|
|
|
remove_contact(contact_number);//删除联系人
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 5:
|
|
|
|
{
|
|
|
|
add_contact();//进行增加
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-12-12 13:10:23 +08:00
|
|
|
return 0;
|
2023-12-11 23:23:15 +08:00
|
|
|
}
|