//通讯录系统
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <stdlib.h>
#define name_LENGTH 16
#define birthplace_LENGTH 64
#define numer_LENGTH 16
#define Email_LENGTH 256
//定义通讯录结构体的数据结构
struct mail
{
char name[name_LENGTH]; //姓名
char birthplace[birthplace_LENGTH]; //籍贯
char numer1[numer_LENGTH]; //号码1
char numer2[numer_LENGTH]; //号码2
char Email[Email_LENGTH]; //电子邮箱
};
//定义每条记录或节点的数据结构
struct node
{
struct mail data; //数据域
struct node* next; //指针域
};
//显示功能菜单
void menu()
{
printf("------------菜单-------------\n");
printf(" 1 添加联系人及其信息\n");
printf(" 2 通讯录记录的联系人个数\n");
printf(" 3 浏览通讯录信息\n");
printf(" 4 查询联系人信息\n");
printf(" 5 删除联系人及其信息\n");
printf(" 6 修改联系人及其信息\n");
printf(" 7 退出通讯录系统\n");
printf(" 8 清屏\n");
printf(" 9 保存到文件\n");
printf("------------------------------\n");
}
void bc_f(struct node* head)
{
FILE* fp;
if ((fp = fopen("通讯录信息", "wb")) == NULL)
{
printf("内存不足 保存失败。\n");
return;
}
while (head->next != NULL)
{
fwrite(head, sizeof(struct node), sizeof(char), fp);
head = head->next;
}
fwrite(head, sizeof(struct node), sizeof(char), fp);
fclose(fp);
printf("保存完成。\n");
return;
}
void dq_f(struct node* head)
{
FILE* fp;
if ((fp = fopen("通讯录信息", "rb")) == NULL)
{
printf("文件为空\n");
if ((fp = fopen("通讯录信息", "rb")) == NULL) {
printf("重新创建数据文件失败");
getchar();
return;
}
printf("创建成功。\n");
return;
}
fread(head, sizeof(struct node), sizeof(char), fp);
while (head->next != NULL)
{
struct node* node;
node = (struct node*)malloc(sizeof(struct node));
fread(head, sizeof(struct node), sizeof(char), fp);
head->next = node;
head = node;
}
printf("读取成功。\n");
fclose(fp);
return;
}
//显示某一联系人信息
void show_f(struct node* q)
{
printf("联系人名字:%s\n", q->data.name);
printf("联系人籍贯:%s\n", q->data.birthplace);
printf("联系人电话1:%s\n", q->data.numer1);
printf("联系人电话2:%s\n", q->data.numer2);
printf("联系人电子邮箱:%s\n\n", q->data.Email);
}
//浏览通讯录列表
void display_f(struct node* head)
{
struct node* p, * q, * min;
char* temp1, * temp2, * temp3, * temp4, * temp5;
p = head->next;
if (head->next == NULL)
{
printf("当前列表为空\n");
return;
}
while (p->next != NULL)
{
min = p;
q = p->next;
while (q != NULL)
{
if (min->data.name[0] > q->data.name[0])
{
min = q;
}
q = q->next;
}
if (min != p)
{
strcpy(temp1, &min->data.name[0]);
strcpy(&min->data.name[0], &p->data.name[0]);
strcpy(&p->data.name[0], temp1);
strcpy(temp2, &min->data.birthplace[0]);
strcpy(&min->data.birthplace[0], &p->data.birthplace[0]);
strcpy(&p->data.birthplace[0], temp2);
strcpy(temp3, &min->data.numer1[0]);
strcpy(&min->data.numer1[0], &p->data.numer1[0]);
strcpy(&p->data.numer1[0], temp3);
strcpy(temp4, &min->data.numer2[0]);
strcpy(&min->data.numer2[0], &p->data.numer2[0]);
strcpy(&p->data.numer2[0], temp4);
strcpy(temp5, &min->data.Email[0]);
strcpy(&min->data.Email[0], &p->data.Email[0]);
strcpy(&p->data.Email[0], temp5);
}
p = p->next;
}
printf("通讯录列表(按首字母排序)如下:\n");
struct node* d_p;
d_p = head->next;
while (d_p != NULL)
{
show_f(d_p);
d_p = d_p->next;
}
}
//联系人列表人数
int count_f(struct node* head)
{
struct node* p;
int count = 0;
p = head->next;
while (p != NULL)
{
count++;
p = p->next;
}
return count;
}
//查询某一联系人信息
void query_f(struct node* head)
{
struct node* p;
p = head->next;
if (head->next == NULL)
{
printf("当前列表为空\n");
return;
}
else
{
char name_lookup[name_LENGTH]; //要查询的联系人的姓名
printf("请输入你要查询的联系人姓名:\n");
scanf("%s", &name_lookup);
while (p != NULL)
{
if (strcmp(p->data.name, name_lookup) == 0)
{
show_f(p);
return;
}
else
p = p->next;
}
}
printf("当前列表无该联系人\n");
return;
}
void input_f(struct node* p, const
char commad[])
{
printf("请输入要%s的联系人名字:\n", commad);
scanf("%s", &p->data.name);
printf("请输入该联系人要%s籍贯:\n", commad);
scanf("%s", &p->data.birthplace);
printf("请输入该联系人要%s的电话号码1:\n", commad);
scanf("%s", &p->data.numer1);
printf("请输入该联系人要%s的电话号码2:\n", commad);
scanf("%s", &p->data.numer2);
printf("请输入该联系人要%s的电子邮箱:\n", commad);
scanf("%s", &p->data.Email);
}
//删除联系人
void delete_f(struct node* head)
{
char name_delete[name_LENGTH];
printf("请输入要删除的联系人的姓名:\n");
scanf("%s", name_delete);
struct node* p, * q;
//保证指针一前一后
p = head->next;
q = head;
if (head->next == NULL)
{
printf("当前列表为空\n");
return;
}
else
{
while (head->next != NULL)
{
if (strcmp(&(p->data.name[0]), name_delete) == 0)
{
q->next = p->next;
free(p);
printf("删除成功\n");
return;
}
else
q = p;
p = p->next;
}
}
printf("没有找到该联系人,删除失败");
return;
}
//清空列表
void empty_f(struct node* head)
{
struct node* p;
p = head->next;
if (head->next == NULL)
return;
else
{
while (p != NULL)
{
free(p);
p = p->next;
}
printf("列表信息已全部删除");
return;
}
}
//添加联系人及其信息
void add_f(struct node* head)
{
struct node* p, * q;
q = head;
p = head->next;
struct node* node;
node = (struct node*)malloc(sizeof(struct node));
input_f(node, "添加");
node->next = NULL;
if (head->next == NULL)
{ //链表为空
head->next = node;
}
else
{
while (p != NULL)
{
if (p->data.name[0] > node->data.name[0])
{ //node节点应该插在p,q之间
node->next = q->next;
q->next = node;
return;
}
else
{
q = p;
p = p->next;
}
}
}
q->next = node;
return;
}
//修改联系人信息
void modify_f(struct node* head) {
struct node* node;
struct node* p, * q;
q = head;
p = head->next;
node = (struct node*)malloc(sizeof(struct node));
node->next = NULL;
if (head->next == NULL)
{
printf("该列表当前为空\n");
return;
}
else
{
input_f(node, "修改");
node->next = NULL;
while (p != NULL)
{
if (strcmp(node->data.name, p->data.name) == 0)
{
q->next = p->next;
free(p);
//node插在q之后
printf("1");
node->next = q->next;
q->next = node;
printf("修改成功\n");
return;
}
else
{
p = q;
p = p->next;
}
}
printf("当前列表没有该联系人");
return;
}
}
int main()
{
//创建头节点
struct node* head;
head = (struct node*)malloc(sizeof(struct node));
head->next = NULL;
int select;
while (1 > 0)
{
menu();
printf("\n请输入你的选择(1~9):");
scanf("%d", &select);
switch (select)
{
case 1:
add_f(head);
break;
case 2:
printf("通讯录记录的人数为:%d\n", count_f(head));
break;
case 3:
dq_f( head);
break;
case 4:
query_f(head);
break;
case 5:
delete_f(head);
break;
case 6:
modify_f(head);
break;
case 7:
empty_f(head);
printf("期待你下次使用");
return 0;
case 8:
system("cls");
break;
case 9:
bc_f(head);
break;
default:
printf("输入不正确\n");
break;
}
}
return 0;
}
另外我想把上面的代码放进这个框架里面,显得简洁易懂一点
#include<stdio.h>
#include<stdlib.h>
#include<Windows.h>
#include<iostream>
void menu(); //显示菜单
void read(); //浏览通讯录
void write(); //添加联系人
void search(); //查询联系人
void change(); //修改联系人信息
void cut(); //删除联系人
void end(); //退出程序
void sort(); //按名字首字母排序
int back = 0;
int main()
{
do
{
system("cls");
int gongneng = 0;//功能序号
menu();
printf("请输入菜单中各功能对应的阿拉伯数字使用该功能:");
scanf("%d", &gongneng);
switch (gongneng)
{
case 1:
{
read();
break;
}
case 2:write(); break;
case 3:search(); break;
case 4:change(); break;
case 5:cut(); break;
case 6:end(); break;
default:printf("\n请输入正确的序号\n");
break;
}
} while (back == 0);
}
void menu()//显示菜单
{
printf("@@@@@@@@@@@@@@@@@@@@@@菜单@@@@@@@@@@@@@@@@@@@@@@\n");
printf("@@ 1.浏览通讯录 2.添加联系人 @@\n");
printf("@@ 3.查询联系人信息 4.修改联系人信息 @@\n");
printf("@@ 5.删除联系人 6.退出程序 @@\n");
printf("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n");
}
void read()
{
system("cls");
back = 1;
printf("输入0返回主菜单");
scanf("%d", &back);
}
void write()
{
printf("write");
}
void search()
{
}
void cut()
{
}
void change()
{
}
void end()
{
back = 1;
return;
}
void sort()
{
}
这个源码我有 记得晚上管我要
//通讯录系统
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <stdlib.h>
#define name_LENGTH 16
#define birthplace_LENGTH 64
#define numer_LENGTH 16
#define Email_LENGTH 256
//定义通讯录结构体的数据结构
struct mail
{
char name[name_LENGTH]; //姓名
char birthplace[birthplace_LENGTH]; //籍贯
char numer1[numer_LENGTH]; //号码1
char numer2[numer_LENGTH]; //号码2
char Email[Email_LENGTH]; //电子邮箱
};
//定义每条记录或节点的数据结构
struct node
{
struct mail data; //数据域
struct node* next; //指针域
};
//显示功能菜单
void menu()
{
printf("------------菜单-------------\n");
printf(" 1 添加联系人及其信息\n");
printf(" 2 通讯录记录的联系人个数\n");
printf(" 3 浏览通讯录信息\n");
printf(" 4 查询联系人信息\n");
printf(" 5 删除联系人及其信息\n");
printf(" 6 修改联系人及其信息\n");
printf(" 7 退出通讯录系统\n");
printf(" 8 清屏\n");
printf(" 9 保存到文件\n");
printf("------------------------------\n");
}
//显示某一联系人信息
void show_f(struct node* q)
{
printf("联系人名字:%s\n", q->data.name);
printf("联系人籍贯:%s\n", q->data.birthplace);
printf("联系人电话1:%s\n", q->data.numer1);
printf("联系人电话2:%s\n", q->data.numer2);
printf("联系人电子邮箱:%s\n\n", q->data.Email);
}
void bc_f(struct node* head)
{
FILE* fp;
if ((fp = fopen("通讯录信息", "wb")) == NULL)
{
printf("内存不足 保存失败。\n");
return;
}
while (head->next != NULL)
{
fwrite(&head->data, sizeof(struct mail), 1, fp);
head = head->next;
}
fclose(fp);
printf("保存完成。\n");
return;
}
void load_f(struct node* head)
{
FILE* fp;
if ((fp = fopen("通讯录信息", "rb")) == NULL)
{
printf("文件为空\n");
if ((fp = fopen("通讯录信息", "rb")) == NULL) {
printf("重新创建数据文件失败");
getchar();
return;
}
printf("创建成功。\n");
return;
}
struct node* pNode = head;
while (true)
{
struct node* node;
node = (struct node*)malloc(sizeof(struct node));
int n = fread(&node->data, sizeof(struct mail), 1, fp);
if (n > 0)
{
pNode->next = node;
pNode = node;
}
else
{
pNode->next = NULL;
break;
}
}
fclose(fp);
return;
}
void dq_f(struct node* head)
{
struct node* node = head->next;
while (node != NULL)
{
show_f(node);
node = node->next;
}
printf("读取成功。\n");
return;
}
//浏览通讯录列表
void display_f(struct node* head)
{
struct node* p, * q, * min;
char temp1[256], temp2[256], temp3[256], temp4[256], temp5[256];
p = head->next;
if (head->next == NULL)
{
printf("当前列表为空\n");
return;
}
while (p->next != NULL)
{
min = p;
q = p->next;
while (q != NULL)
{
if (min->data.name[0] > q->data.name[0])
{
min = q;
}
q = q->next;
}
if (min != p)
{
strcpy(temp1, &min->data.name[0]);
strcpy(&min->data.name[0], &p->data.name[0]);
strcpy(&p->data.name[0], temp1);
strcpy(temp2, &min->data.birthplace[0]);
strcpy(&min->data.birthplace[0], &p->data.birthplace[0]);
strcpy(&p->data.birthplace[0], temp2);
strcpy(temp3, &min->data.numer1[0]);
strcpy(&min->data.numer1[0], &p->data.numer1[0]);
strcpy(&p->data.numer1[0], temp3);
strcpy(temp4, &min->data.numer2[0]);
strcpy(&min->data.numer2[0], &p->data.numer2[0]);
strcpy(&p->data.numer2[0], temp4);
strcpy(temp5, &min->data.Email[0]);
strcpy(&min->data.Email[0], &p->data.Email[0]);
strcpy(&p->data.Email[0], temp5);
}
p = p->next;
}
printf("通讯录列表(按首字母排序)如下:\n");
struct node* d_p;
d_p = head->next;
while (d_p != NULL)
{
show_f(d_p);
d_p = d_p->next;
}
}
//联系人列表人数
int count_f(struct node* head)
{
struct node* p;
int count = 0;
p = head->next;
while (p != NULL)
{
count++;
p = p->next;
}
return count;
}
//查询某一联系人信息
void query_f(struct node* head)
{
struct node* p;
p = head->next;
if (head->next == NULL)
{
printf("当前列表为空\n");
return;
}
else
{
char name_lookup[name_LENGTH]; //要查询的联系人的姓名
printf("请输入你要查询的联系人姓名:\n");
scanf("%s", &name_lookup);
while (p != NULL)
{
if (strcmp(p->data.name, name_lookup) == 0)
{
show_f(p);
return;
}
else
p = p->next;
}
}
printf("当前列表无该联系人\n");
return;
}
void input_f(struct node* p, const
char commad[])
{
printf("请输入要%s的联系人名字:\n", commad);
scanf("%s", &p->data.name);
printf("请输入该联系人要%s籍贯:\n", commad);
scanf("%s", &p->data.birthplace);
printf("请输入该联系人要%s的电话号码1:\n", commad);
scanf("%s", &p->data.numer1);
printf("请输入该联系人要%s的电话号码2:\n", commad);
scanf("%s", &p->data.numer2);
printf("请输入该联系人要%s的电子邮箱:\n", commad);
scanf("%s", &p->data.Email);
}
//删除联系人
void delete_f(struct node* head)
{
char name_delete[name_LENGTH];
printf("请输入要删除的联系人的姓名:\n");
scanf("%s", name_delete);
struct node* p, * q;
//保证指针一前一后
p = head->next;
q = head;
if (head->next == NULL)
{
printf("当前列表为空\n");
return;
}
else
{
while (head->next != NULL)
{
if (strcmp(&(p->data.name[0]), name_delete) == 0)
{
q->next = p->next;
free(p);
printf("删除成功\n");
return;
}
else
q = p;
p = p->next;
}
}
printf("没有找到该联系人,删除失败");
return;
}
//清空列表
void empty_f(struct node* head)
{
struct node* p;
p = head->next;
if (head->next == NULL)
return;
else
{
while (p != NULL)
{
struct node* pNode = p;
p = p->next;
free(pNode);
}
printf("列表信息已全部删除");
return;
}
}
//添加联系人及其信息
void add_f(struct node* head)
{
struct node* p, * q;
q = head;
p = head->next;
if (head->data.name[0] == 0)
{
input_f(head, "添加");
return;
}
struct node* node;
node = (struct node*)malloc(sizeof(struct node));
input_f(node, "添加");
node->next = NULL;
if (head->next == NULL)
{ //链表为空
head->next = node;
}
else
{
while (p != NULL)
{
if (p->data.name[0] > node->data.name[0])
{ //node节点应该插在p,q之间
node->next = q->next;
q->next = node;
return;
}
else
{
q = p;
p = p->next;
}
}
}
q->next = node;
return;
}
//修改联系人信息
void modify_f(struct node* head) {
struct node* node;
struct node* p, * q;
q = head;
p = head->next;
node = (struct node*)malloc(sizeof(struct node));
node->next = NULL;
if (head->next == NULL)
{
printf("该列表当前为空\n");
return;
}
else
{
input_f(node, "修改");
node->next = NULL;
while (p != NULL)
{
if (strcmp(node->data.name, p->data.name) == 0)
{
q->next = p->next;
free(p);
//node插在q之后
printf("1");
node->next = q->next;
q->next = node;
printf("修改成功\n");
return;
}
else
{
p = q;
p = p->next;
}
}
printf("当前列表没有该联系人");
return;
}
}
int main()
{
//创建头节点
struct node* head;
head = (struct node*)malloc(sizeof(struct node));
head->data.name[0] = 0;
head->next = NULL;
load_f(head);
int select;
while (1 > 0)
{
fflush(stdin);
menu();
printf("\n请输入你的选择(1~9):");
scanf("%d", &select);
switch (select)
{
case 1:
add_f(head);
break;
case 2:
printf("通讯录记录的人数为:%d\n", count_f(head));
break;
case 3:
dq_f(head);
break;
case 4:
query_f(head);
break;
case 5:
delete_f(head);
break;
case 6:
modify_f(head);
break;
case 7:
empty_f(head);
printf("期待你下次使用");
return 0;
case 8:
system("cls");
break;
case 9:
bc_f(head);
break;
default:
printf("输入不正确\n");
break;
}
}
return 0;
}