#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX_NAME 11
#define MAX_SEX 3
#define MAX_BIRTHDAY 9
#define MAX_TEL 21
#define MAX_MOBILE 21
#define MAX_FAX 21
#define MAX_ADDRESS 101
#define MAX_POSTAL_CODE 7
typedef struct _person
{
char name[MAX_NAME];
char sex[MAX_SEX];
char birthday[MAX_BIRTHDAY];
char tel[MAX_TEL];
char mobile[MAX_MOBILE];
char fax[MAX_FAX];
char address[MAX_ADDRESS];
char postal_code[MAX_POSTAL_CODE];
}person;
typedef struct _addr_book
{
person per;
struct _addr_book *next;
}addr_book;
addr_book *first = NULL;
void add(); //添加联系人
void show(); //显示联系人
void search(); //查找联系人
void delete(); //删除联系人
void update(); //更新联系人
void save(); //保存联系人
void quit(); //推出
int print_menu(); //打印主菜单界面
addr_book *get_last(addr_book *from); //取链表最后一个值
void print_person(person *p); //显示联系人信息
void input_person(person *p); //提示输入联系人具体信息
char menu[] =
"+===============================================================+\n"
"| 通讯簿管理系统 |\n"
"+===============================================================+\n"
"| 1 添加联系人|\n"
"| 2 显示所有联系人|\n"
"| 3 查找联系人|\n"
"| 4 删除联系人|\n"
"| 5 更新联系人|\n"
"| 6 保存|\n"
"| 7 退出系统|\n"
"+===============================================================+\n";
int main()
{
int flg = 1;
while (flg)
{
switch (print_menu())
{
case 1:add();
break;
case 2:show();
break;
case 3:search();
break;
case 4:delete();
break;
case 5:update();
break;
case 6:save();
break;
case 7:quit();
break;
}
}
}
void add()
{
char input = 'N';
addr_book *last = NULL;
addr_book *new_addr = (addr_book*)malloc(sizeof(addr_book));
memset(new_addr, 0, sizeof(addr_book));
new_addr->next = NULL;
if (first == NULL)
{
first = new_addr;
}
else
{
last = get_last(first);
last->next = new_addr;
}
input_person(&(new_addr->per));
printf(">继续输入?(Y 继续,N 返回菜单)");
getchar();
input = getchar();
if (input == 'Y' || input == 'y')
{
add();
}
}
void show()
{
int i = 0;
addr_book *P = first;
while (P != NULL)
{
i++;
printf("********第%d个联系人*********************************\n", i);
print_person(&(P->per));
P = P->next;
if (i == 0)
{
printf("没有联系人!");
}
printf("按任意键返回菜单.....");
getchar();
getchar();
}
}
void search()
{
int count = 0;
char input = 'N';
char name[MAX_NAME] = { 0 };
addr_book *p = first;
printf(">请输入要查找的联系人的姓名(最大%d个字符):", MAX_NAME - 1);
scanf("%s", name);
while (p != NULL)
{
if (strcmp(p->per.name, name) == 0)
{
print_person(&(p->per));
count++;
}
p = p->next;
}
if (count == 0)
{
printf("没有找到姓名为%s的人.", name);
}
printf("继续查找吗?(Y 继续查找,N 返回菜单)");
getchar();
input = getchar();
if (input == 'Y' || input == 'y')
{
search();
}
}
void delete()
{
int count = 0;
char input = 'N';
char name[MAX_NAME] = { 0 };
addr_book *p = first;
addr_book *p1 = NULL;
printf(">请输入要删除的联系人姓名(最大%d个字符):", MAX_NAME - 1);
scanf("%s", name);
while (p != NULL)
{
if (strcmp(p->per.name, name) == 0)
{
print_person(&(p->per));
count++;
break;
}
p = p->next;
}
if (count == 0)
{
printf("没有姓名为%s的人.", name);
}
else
{
printf("确定要删除姓名为(%d)的联系人吗?(Y 确认 N 取消)", name);
getchar();
input = getchar();
if (input == 'Y' || input == 'y')
{
if (p == first)
{
first = p->next;
}
else
{
p1 = first;
while (p1 != NULL)
{
if (p1->next == p)
{
p1->next = p->next;
break;
}
p1 = p1->next;
}
}
free(p);
}
}
printf("继续删除其他联系人吗?(Y 继续删除,N 返回菜单)");
getchar;
input = getchar();
if (input == 'Y' || input == 'y')
{
delete();
}
}
void update()
{
int count = 0;
char input = 'N';
char name[MAX_NAME] = { 0 };
addr_book *p = first;
printf(">请输入要更新的联系人的姓名(最大%d个字符):", MAX_NAME - 1);
scanf("%s", name);
while (p != NULL)
{
if (strcmp(p->per.name, name) == 0)
{
print_person(&(p->per));
count++;
break;
}
p = p->next;
}
if (count == 0)
{
printf("没有找到姓名为%s的人.", name);
}
else
{
input_person(&(p->per));
}
printf("继续更新其他联系人吗?(Y 继续更新,N 返回菜单)");
getchar();
input = getchar();
if (input == 'Y' || input == 'y')
{
update();
}
}
void save()
{
FILE *fp;
char file[100];
addr_book *p = first;
printf("请输入文件名:");
scanf("%s", file);
fp = fopen(file, "w");
while (p != NULL)
{
fprintf(fp, "%s,%s,%s,%s,%s,%s,%s,%s\n",
p->per.name, p->per.sex, p->per.birthday, p->per.tel,
p->per.mobile, p->per.fax, p->per.address, p->per.postal_code);
p = p->next;
}
fclose(fp);
printf("保存成功!\n按任意键返回菜单..\n");
getchar();
getchar();
}
void quit()
{
addr_book *pdel = first;
addr_book *p = NULL;
if (pdel == NULL)
{
exit(0);
}
while (pdel->next != NULL)
{
p = pdel->next;
free(pdel);
pdel = p;
}
free(pdel);
exit(0);
}
int print_menu()
{
int selected = 0;
system("cls");
printf(menu);
printf(">请选择【1-7】:");
scanf("%d", &selected);
if (selected<1 || selected>7)
{
printf("错误的选择!(请输入1-7).按任意键继续....");
getchar();
getchar();
}
return selected;
}
void input_person(person* p)
{
printf(">请输入联系人信息:\n");
printf("请输入姓名(最大长度%d个字符):", MAX_NAME - 1);
scanf("%s", p->name);
printf("请输入姓别(最大长度%d个字符):", MAX_SEX - 1);
scanf("%s", p->sex);
printf("请输入出生日期(最大长度%d个字符):", MAX_BIRTHDAY - 1);
scanf("%s", p->birthday);
printf("请输入电话(最大长度%d个字符):", MAX_TEL - 1);
scanf("%s", p->tel);
printf("请输入手机(最大长度%d个字符):", MAX_MOBILE - 1);
scanf("%s", p->mobile);
printf("请输入传真(最大长度%d个字符):", MAX_FAX - 1);
scanf("%s", p->fax);
printf("请输入地址(最大长度%d个字符):", MAX_ADDRESS - 1);
scanf("%s", p->address);
printf("请输入邮编(最大长度%d个字符):", MAX_POSTAL_CODE - 1);
scanf("%s", p->postal_code);
}
void print_person(person *p)
{
printf("姓名:%s\t 性别:%s\t 生日:%s\n", p->name, p->sex, p->birthday);
printf("电话:%s\n", p->tel);
printf("手机:%s\n", p->mobile);
printf("传真:%s\n", p->fax);
printf("地址:%s\n", p->address);
printf("邮编:%s\n", p->postal_code);
printf("\n");
}
addr_book *get_last(addr_book *from)
{
addr_book *p = from;
while (p->next != NULL)
{
p = p->next;
}
return p;
}
转自:《c语言课程设计(第三版)梁旭 谷晓琳 黄明 焦璇 编著》