李刚是一爱折腾的人,当然爱折腾的人均有梦想,他想当中国的盖次呢。可不,现在个人好友信息多了,复杂了,他想制作一个个人通讯录的制作管理软件。 刚好这个学期学了数据结构课,所以他准备使用数据结构知识来实现了。并考虑使用双向链表作数据结构。并制定了初步要求:
(1)每个好友信息包含姓名、性别、住址、邮编、几岁、电话、QQ、微信帐号、生日等。
(2)作为一个完整的系统,应具有友好的界面和较强的容错能力。
1.首先构建双链表架构。
2.建立C++算法程序。
3.构建界面。
1.新增联系人。
2.删除联系人。
3.寻找联系人。
4.修改联系人。
5.联系人一览。
6.对于错误的操作进行文字提醒。
源代码:#include
#include
#include
#include
void init_dlink(void);
void read_file(void);
void write_file(void);
void insert_item(void);
void search_item(void);
void sort_item(void);
void delete_item(void);
void print_item(void);
void modify_item(void);
void anykey(void);
typedef struct node *link;
struct node {
char man_id[10];
char name[20];
char sex[10];
char address[50];
char postcode[10];
char age[10];
char phone[10];
char QQ[10];
char weixin[20];
char birthday[10];
link prior, next;
};
link ptr, head, tail, current;
bool fFlag;
int main(void)
{
char option1, option2;
system("cls");
init_dlink();
read_file();
while(1)
{
system("cls");
printf("**********************************\n");
printf(" 1.添加联系人\n");
printf(" 2.删除联系人\n");
printf(" 3.寻找联系人\n");
printf(" 4.联系人一览\n");
printf(" 5.联系人信息修改\n");
printf(" 6.退出程序\n");
printf("**********************************\n");
printf(" 请输入你的选择(1~5)");
option1 = getche();
switch(option1)
{
case '1':
fFlag = 1;
insert_item();
break;
case '2':
delete_item();
break;
case '3':
search_item();
break;
case '4':
print_item();
break;
case '5':
modify_item();
break;
case '6':
if (fFlag)
{
printf("\n");
printf("保存变化? (Y or N)");
option2 = getche();
if(option2 == 'Y')
{
write_file();
exit(0);
}
if(option2 = 'N')
exit(0);
}
else
{
write_file();
exit(0);
}
}
}
}
void init_dlink(void)
{
ptr = (link)malloc(sizeof *ptr);
strcpy(ptr->man_id, "0");
strcpy(ptr->name, "0");
strcpy(ptr->sex, "0");
strcpy(ptr->address, "0");
strcpy(ptr->postcode, "0");
strcpy(ptr->age, "0");
strcpy(ptr->phone, "0");
strcpy(ptr->QQ, "0");
strcpy(ptr->weixin, "0");
strcpy(ptr->birthday, "0");
ptr-> prior = ptr;
ptr->next = ptr;
head = ptr;
tail = ptr;
}
void read_file(void)
{
FILE *fptr;
if((fptr = fopen("dlist.dat", "r")) == NULL)
{
printf(" 保存文件不存在!\n");
printf(" 任意键建立第一个联系人...\n");
getch();
insert_item();
}
else
{
ptr = (link)malloc(sizeof *ptr);
while(fscanf(fptr, ptr->man_id, ptr->name, ptr->sex, ptr->address, ptr->postcode, ptr->age, ptr->phone, ptr->QQ, ptr->weixin, ptr->birthday) != EOF)
{
if(strcmp(ptr->man_id, " ") != 0)
{
sort_item();
ptr = (link)malloc(sizeof *ptr);
}
else
free(ptr);
}
fclose(fptr);
}
}
void write_file(void)
{
FILE *fptr;
fptr = fopen("dlist.dat", "w");
current = head->next;
while(current != head)
{
fprintf(fptr, "%s %s %s %s %s %s\n", current->man_id, current->name, current->sex, current->address, current->postcode, current->age, current->phone, current->QQ, current->weixin, current->birthday);
current = current->next;
}
fclose(fptr);
}
void insert_item(void)
{
system("cls");
ptr = (link)malloc(sizeof *ptr);
printf(" 序号:");
gets(ptr->man_id);
printf(" 姓名:");
gets(ptr->name);
printf(" 性别:");
gets(ptr->sex);
printf(" 地址:");
gets(ptr->address);
printf(" 邮编:");
gets(ptr->postcode);
printf(" 年龄:");
gets(ptr->age);
printf(" 电话:");
gets(ptr->phone);
printf(" QQ:");
gets(ptr->QQ);
printf(" 微信:");
gets(ptr->weixin);
printf(" 生日:");
gets(ptr->birthday);
sort_item();
}
void sort_item(void)
{
current = head->next;
while(current != head)
{
if(strcmp(ptr->man_id, current->man_id) < 0)
{
ptr->next = current;
ptr->prior = current->prior;
current->prior->next = ptr;
current->prior = ptr;
break;
}
current = current->next;
}
if(head->next == head || current == head)
{
ptr->next = head;
ptr->prior = head->prior;
head->prior->next = ptr;
head->prior = ptr;
tail = ptr;
}
}
void delete_item(void)
{
char del_id[10];
int count = 0;
link clear;
system("cls");
if(head->next == head)
printf(" 没有这个人!\n");
else
{
printf(" 序号:");
gets(del_id);
current = head->next;
while(current->next != head)
{
if(strcmp(del_id, current->man_id)==0)
{
count++;
clear = current;
current->prior->next = current->next;
current->next->prior = current->prior;
current = current->next;
free(clear);
}
current = current->next;
}
if(strcmp(del_id, current->man_id) == 0)
{
count++;
clear = current;
current->prior->next = head;
head->prior = current->prior;
tail = current->prior;
free(clear);
}
if(count > 0)
printf(" %s 删除这个人\n", del_id);
else
printf(" 没有这个人\n", del_id);
}
anykey();
}
void search_item(void)
{
char search_id[10];
system("cls");
if(head->next == head)
printf(" 没有这个人\n");
else
{
printf(" 查找序号:");
gets(search_id);
current = head->next;
while((current != head) && (strcmp(search_id, current->man_id)!=0))
current = current->next;
if (current != head)
{
printf("--------------------------------------\n");
printf(" 序号: %s\n", current->man_id);
printf(" 姓名: %s\n", current->name);
printf(" 性别: %s\n", current->sex);
printf(" 地址: %s\n", current->address);
printf(" 邮编: %s\n", current->postcode);
printf(" 年龄: %s\n", current->age);
printf(" 电话: %s\n", current->phone);
printf(" QQ: %s\n", current->QQ);
printf(" 微信: %s\n", current->weixin);
printf(" 生日: %s\n", current->birthday);
printf("--------------------------------------\n");
}
else
printf(" 没有这个人\n", search_id);
}
anykey();
}
void modify_item(void)
{
int count = 0;
char modify_id[10];
system("cls");
if(head->next == head)
printf(" 没有这个人的记录\n");
else
{
printf(" 修改联系人的序号:");
gets(modify_id);
current = head->next;
while(current != head)
{
if(strcmp(modify_id, current->man_id) == 0)
{
printf("******************************\n");
printf(" 序号: %s\n", current->man_id);
printf(" 姓名: %s\n", current->name);
printf(" 性别: %s\n", current->sex);
printf(" 地址: %s\n", current->address);
printf(" 邮编: %s\n", current->postcode);
printf(" 年龄: %s\n", current->age);
printf(" 电话: %s\n", current->phone);
printf(" QQ: %s\n", current->QQ);
printf(" 微信: %s\n", current->weixin);
printf(" 生日: %s\n", current->birthday);
printf(" 请输入新地址:");
gets(current->address);
printf(" 请输入新邮编:");
gets(current->postcode);
printf(" 请输入新年龄:");
gets(current->age);
printf(" 请输入新电话:");
gets(current->phone);
printf(" 请输入新QQ:");
gets(current->QQ);
printf(" 请输入新微信:");
gets(current->weixin);
printf(" 请输入新生日:");
gets(current->birthday);
count++;
}
current = current->next;
}
if(count > 0)
printf(" %d 联系人信息的修改\n", count);
else
printf(" 没有这个人\n", modify_id);
}
anykey();
}
void print_item(void)
{
int count = 0;
system("cls");
if(head->next == head)
printf(" 没有联系人信息\n");
else
{
printf(" 序号 姓名 性别 地址 邮编 年龄 电话 QQ 微信 生日\n");
printf("-------------------------------------------------------------------------------\n");
current = head->next;
while(current != head)
{
printf(" %-7s %-12s %-6s %-12s %-12s %-s\n", current->man_id, current->name, current->sex, current->address, current->postcode, current->age, current->phone, current->QQ, current->weixin, current->birthday);
count++;
current = current->next;
if(count % 20 == 0)
getch();
}
printf("-------------------------------------------------------------------------------\n");
printf(" 信息没有发现\n", count);
}
anykey();
}
void anykey(void)
{
printf(" 任意键继续...");
getch();
}