1.功能:
创建新表
增加联系人
多参数排序
多参数查找
保存到文件中
从文件中 打开
删除联系人
修改联系人
退出
2.注意事项:
需要在.c所在文件夹下建一个名称为通讯录的txt文件才能正常使用。
3.环境:
windows vc++6.0
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/* 标准库函数// */
///定义
#define NULL 0
#define LEN sizeof(struct txlproject)
/* 计算字节// */
int n;
struct txlproject
{
char name[30];
/*名字 */
char sex[30];
/* 性别 */
char handset[30];
/* 手机 */
char email[30];
/* 电子邮件 */
char address[30];
/* 通讯地址 */
struct txlproject *next;
}
;
struct txlproject *shifang( struct txlproject *head );
/* 释放内存函数声明 */
///创建链表/
/* 创建函数,不带头结点的链表 */
struct txlproject *creat( void )
{
struct txlproject *head, *p1, *p2;
char name[20];
n = 0;
p1 = (struct txlproject *) malloc( LEN );
p2 = p1;
/* 强制内存转换 */
printf( "请输入通讯录的内容!\n姓名输入为0时表示创建完毕!\n" );
printf( "请输入姓名:" );
gets( name );
if ( strcmp( name, "0" ) != 0 )
{
strcpy( p1->name, name );
printf( "请输入性别:" );
gets( p1->sex );
printf( "请输入手机:" );
gets( p1->handset );
printf( "请输入电子邮件:" );
gets( p1->email );
printf( "请输入通讯地址:" );
gets( p1->address );
head = NULL;
while ( 1 )
{
n = n + 1;
/* 记录通讯录人数个数 */
if ( n == 1 )
head = p1; else
p2->next = p1;
p2 = p1;
printf( "请输入姓名:" );
gets( name );
if ( strcmp( name, "0" ) == 0 )
{
break;
} else
{
p1 = (struct txlproject *) malloc( LEN );
strcpy( p1->name, name );
printf( "请输入性别:" );
gets( p1->sex );
printf( "请输入手机:" );
gets( p1->handset );
printf( "请输入电子邮件:" );
gets( p1->email );
printf( "请输入通讯地址:" );
gets( p1->address );
}
}
p2->next = NULL;
return(head);
} else
return(0);
}
/* 输出函数 */
void print( struct txlproject *head )
{
struct txlproject *p;
if ( head != NULL )
{
p = head;
// printf( "本通讯录现在共有%d人:\n", n );
printf( "---姓名-------性别--------手机-------------------Email-------------通讯地址\n" );
printf( "==========================================================================================\n" );
do
{
printf( "%7s", p->name );
printf( " " );
printf( "%1s", p->sex );
printf( " " );
printf( "%7s", p->handset );
printf( " " );
printf( "%11s", p->email );
//printf( " " );
printf( "%15s", p->address );
printf( " \n" );
p = p->next;
}
while ( p != NULL );
printf( "==========================================================================================\n" );
getchar();
system("cls");
} else
{
printf( "通讯录为空,无法输出!\n" );
getchar();
system("cls");
}
}
/* 增加函数 */
struct txlproject *insert( struct txlproject *head )
{
struct txlproject *p0, *p1, *p2;
char name[20];
p1 = head;
printf( "请输入增加的内容:\n" );
printf( "请输入姓名:" );
gets( name );
if ( strcmp( name, "0" ) == 0 )
{
printf( "姓名不能为0,增加失败!\n" );
return(head);
} else
{
p0 = (struct txlproject *) malloc( LEN );
strcpy( p0->name, name );
printf( "请输入性别:" );
gets( p0->sex );
printf( "请输入手机:" );
gets( p0->handset );
printf( "请输入电子邮件:" );
gets( p0->email );
printf( "请输入通讯地址:" );
gets( p0->address );
n = n + 1;
if ( head == NULL )
{
head = p0;
p0->next = NULL;
return(head);
} else
{
while ( strcmp( p0->name, p1->name ) > 0 && (p1->next != NULL) )
{
p2 = p1;
p1 = p1->next;
}
if ( strcmp( p0->name, p1->name ) < 0 || strcmp( p0->name, p1->name ) == 0 )
{
if ( head == p1 )
{
head = p0;
} else
{
p2->next = p0;
}
p0->next = p1;
} else
{
p1->next = p0;
p0->next = NULL;
}
return(head);
}
}
}
struct txlproject *changeadd( struct txlproject *head ) //修改时新写入数据的函数
{
struct txlproject *p0, *p1, *p2;
char name[20];
p1 = head;
printf( "请输入姓名:" );
gets( name );
if ( strcmp( name, "0" ) == 0 )
{
printf( "姓名不能为0,增加失败!\n" );
return(head);
} else
{
p0 = (struct txlproject *) malloc( LEN );
strcpy( p0->name, name );
printf( "请输入性别:" );
gets( p0->sex );
printf( "请输入手机:" );
gets( p0->handset );
printf( "请输入电子邮件:" );
gets( p0->email );
printf( "请输入通讯地址:" );
gets( p0->address );
n = n + 1;
if ( head == NULL )
{
head = p0;
p0->next = NULL;
return(head);
} else
{
while ( strcmp( p0->name, p1->name ) > 0 && (p1->next != NULL) )
{
p2 = p1;
p1 = p1->next;
}
if ( strcmp( p0->name, p1->name ) < 0 || strcmp( p0->name, p1->name ) == 0 )
{
if ( head == p1 )
{
head = p0;
} else
{
p2->next = p0;
}
p0->next = p1;
} else
{
p1->next = p0;
p0->next = NULL;
}
return(head);
}
}
}
struct txlproject *delete( struct txlproject *head ) //删除联系人函数
{
struct txlproject *p, *q;
char name[30];
if ( head == NULL )
{
printf( "通讯录为空,无法显示!\n" );
getchar();
system("cls");
return(head);
}
p = head;
printf( "请输入需要删除的人的姓名:" );
gets( name );
if ( strcmp( head->name, name ) == 0 )
{
head = head->next;
free( p );
printf( "删除操作成功!\n" );
return(head);
} else
{
q = head, p = head->next;
while ( p != NULL )
{
if ( strcmp( p->name, name ) == 0 )
{
q->next = p->next;
free( p );
printf( "删除操作成功!\n" );
return(head);
}
p = p->next;
q = q->next;
}
}
}
struct txlproject *change( struct txlproject *head ) //修改联系人函数
{
struct txlproject *p, *q;
char name[30];
if ( head == NULL )
{
printf( "通讯录为空,无法显示!\n" );
getchar();
system("cls");
return(head);
}
p = head;
printf( "请输入需要修改的人的姓名:" );
gets( name );
if ( strcmp( head->name, name ) == 0 )
{
head = head->next;
free( p );
// printf( "修改操作成功!\n" );
return(head);
} else
{
q = head, p = head->next;
while ( p != NULL )
{
if ( strcmp( p->name, name ) == 0 )
{
q->next = p->next;
free( p );
// printf( "修改操作成功!\n" );
return(head);
}
p = p->next;
q = q->next;
}
}
}
/* 显示函数 */
struct txlproject *display( struct txlproject *head )
{
struct txlproject *p1, *p2;
char name[30];
int m;
if ( head == NULL )
{
printf( "通讯录为空,无法显示!\n" );
getchar();
system("cls");
return(head);
}
p1 = head;
m = 0;
printf( "请输入需要显示人的姓名:" );
gets( name );
while ( p1 != NULL )
{
while ( (strcmp( p1->name, name ) ) != 0 && p1->next != NULL )
{
p2 = p1;
p1 = p1->next;
}
if ( strcmp( p1->name, name ) == 0 )
{
m++;
printf( "%s的通讯内容如下:\n", name );
printf( "---姓名--------性别--------手机-------Email------通讯地址\n" );
printf( "==================================\n" );
printf( "== %s", p1->name );
printf( " " );
printf( "%s", p1->sex );
printf( " " );
printf( "%s", p1->handset );
printf( " " );
printf( "%s", p1->email );
printf( " " );
printf( "%s", p1->address );
printf( " \n" );
printf( "==================================\n" );
}
p1 = p1->next;
}
if ( m == 0 )
{
printf( "此人未在本通讯录中!\n" );
getchar();
system("cls");
}
return(head);
}
///排序函数
/* 姓名排序函数 */
struct txlproject *paixu_name( struct txlproject *head )
{
struct txlproject *p1, *p2;
int i, j;
struct txlproject1
{
char name[30];
char sex[30];
char handset[30];
char email[30];
char address[30];
}
;
struct txlproject1 px[200];
struct txlproject1 temp;
if ( head == NULL )
{
printf( "通讯录为空,无法排序!\n" );
getchar();
system("cls");
return(head);
}
p1 = head;
for ( i = 0; i < n, p1 != NULL; i++ )
{
strcpy( px[i].name, p1->name );
strcpy( px[i].sex, p1->sex );
strcpy( px[i].handset, p1->handset );
strcpy( px[i].email, p1->email );
strcpy( px[i].address, p1->address );
p2 = p1;
p1 = p1->next;
}
head = shifang( head );
for ( j = 0; j < n - 1; j++ )
{
for ( i = j + 1; i < n; i++ )
{
if ( strcmp( px[i].name, px[j].name ) < 0 )
{
temp = px[i];
px[i] = px[j];
px[j] = temp;
}
}
}
p1 = (struct txlproject *) malloc( LEN );
p2 = p1;
strcpy( p1->name, px[0].name );
strcpy( p1->sex, px[0].sex );
strcpy( p1->handset, px[0].handset );
strcpy( p1->email, px[0].email );
strcpy( p1->address, px[0].address );
head = p1;
for ( i = 1; i < n; i++ )
{
p1 = (struct txlproject *) malloc( LEN );
strcpy( p1->name, px[i].name );
strcpy( p1->sex, px[i].sex );
strcpy( p1->handset, px[i].handset );
strcpy( p1->email, px[i].email );
strcpy( p1->address, px[i].address );
p2->next = p1;
p2 = p1;
}
p2->next = NULL;
printf( "按姓名排序后为:\n" );
print( head );
return(head);
}
struct txlproject *paixu_phone( struct txlproject *head ) //按电话号码排序
{
struct txlproject *p1, *p2;
int i, j;
struct txlproject1
{
char name[30];
char sex[30];
char handset[30];
char email[30];
char address[30];
}
;
struct txlproject1 px[200];
struct txlproject1 temp;
if ( head == NULL )
{
printf( "通讯录为空,无法排序!\n" );
getchar();
system("cls");
return(head);
}
p1 = head;
for ( i = 0; i < n, p1 != NULL; i++ )
{
strcpy( px[i].name, p1->name );
strcpy( px[i].sex, p1->sex );
strcpy( px[i].handset, p1->handset );
strcpy( px[i].email, p1->email );
strcpy( px[i].address, p1->address );
p2 = p1;
p1 = p1->next;
}
head = shifang( head );
for ( j = 0; j < n - 1; j++ )
{
for ( i = j + 1; i < n; i++ )
{
if ( strcmp( px[i].handset, px[j].handset ) < 0 )
{
temp = px[i];
px[i] = px[j];
px[j] = temp;
}
}
}
p1 = (struct txlproject *) malloc( LEN );
p2 = p1;
strcpy( p1->name, px[0].name );
strcpy( p1->sex, px[0].sex );
strcpy( p1->handset, px[0].handset );
strcpy( p1->email, px[0].email );
strcpy( p1->address, px[0].address );
head = p1;
for ( i = 1; i < n; i++ )
{
p1 = (struct txlproject *) malloc( LEN );
strcpy( p1->name, px[i].name );
strcpy( p1->sex, px[i].sex );
strcpy( p1->handset, px[i].handset );
strcpy( p1->email, px[i].email );
strcpy( p1->address, px[i].address );
p2->next = p1;
p2 = p1;
}
p2->next = NULL;
printf( "按姓名排序后为:\n" );
print( head );
return(head);
}
///查找函数/
/* 姓名查找函数 */
struct txlproject *search_name( struct txlproject *head )
{
struct txlproject *p1, *p2;
int m;
char name[30];
if ( head == NULL )
{
printf( "通讯录为空,无法分类查找!\n" );
getchar();
system("cls");
return(head);
}
p1 = head;
printf( "********************\n" );
printf( "** 请输入需要查找的姓名 **\n" );
printf( "********************\n" );
m = 0;
gets( name );
while ( p1 != NULL )
{
while ( strcmp( p1->name, name ) != 0 && p1->next != NULL )
{
p2 = p1;
p1 = p1->next;
}
if ( strcmp( p1->name, name ) == 0 )
{
m++;
system("cls");
printf( "你查找的内容是:\n" );
printf( "---姓名-------性别--------手机-------------------Email-------------通讯地址\n" );
printf( "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n" );
printf( " %7s %1s %7s %11s%18s\n", p1->name, p1->sex, p1->handset, p1->email, p1->address );
printf( "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n" );
getchar();
system("cls");
}
p1 = p1->next;
if ( m == 0 )
{
printf( "此人未在本通讯录中!\n" );
getchar();
system("cls");
}
break;
}
return(head);
}
/* 电话查找函数 */
struct txlproject *search_phone( struct txlproject *head )
{
struct txlproject *p1, *p2;
int m;
char phone[30];
if ( head == NULL )
{
printf( "通讯录为空,无法分类查找!\n" );
getchar();
system("cls");
return(head);
}
p1 = head;
printf( "********************\n" );
printf( "** 请输入需要查找的号码 **\n" );
printf( "********************\n" );
m = 0;
gets( phone );
while ( p1 != NULL )
{
while ( strcmp( p1->handset, phone ) != 0 && p1->next != NULL )
{
p2 = p1;
p1 = p1->next;
}
if ( strcmp( p1->handset, phone ) == 0 )
{
m++;
system("cls");
printf( "你查找的内容是:\n" );
printf( "---姓名-------性别--------手机-------------------Email-------------通讯地址\n" );
printf( "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n" );
printf( " %7s %1s %7s %11s%18s\n", p1->name, p1->sex, p1->handset, p1->email, p1->address );
printf( "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n" );
getchar();
system("cls");
}
p1 = p1->next;
if ( m == 0 )
{
printf( "此人未在本通讯录中!\n" );
getchar();
system("cls");
}
break;
}
return(head);
}
/* 地址查找函数 */
struct txlproject *search_address( struct txlproject *head )
{
struct txlproject *p1, *p2;
int m;
char address[30];
if ( head == NULL )
{
printf( "通讯录为空,无法分类查找!\n" );
getchar();
system("cls");
return(head);
}
p1 = head;
printf( "********************\n" );
printf( "** 请输入需要查找的地址 **\n" );
printf( "********************\n" );
m = 0;
gets( address );
while ( p1 != NULL )
{
while ( strcmp( p1->address, address ) != 0 && p1->next != NULL )
{
p2 = p1;
p1 = p1->next;
}
if ( strcmp( p1->address, address) == 0 )
{
m++;
system("cls");
printf( "你查找的内容是:\n" );
printf( "---姓名-------性别--------手机-------------------Email-------------通讯地址\n" );
printf( "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n" );
printf( " %7s %1s %7s %11s%18s\n", p1->name, p1->sex, p1->handset, p1->email, p1->address );
printf( "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n" );
getchar();
system("cls");
}
p1 = p1->next;
if ( m == 0 )
{
printf( "此人未在本通讯录中!\n" );
getchar();
system("cls");
}
break;
}
return(head);
}
/* 邮箱查找函数 */
struct txlproject *search_email( struct txlproject *head )
{
struct txlproject *p1, *p2;
int m;
char email[30];
if ( head == NULL )
{
printf( "通讯录为空,无法分类查找!\n" );
getchar();
system("cls");
return(head);
}
p1 = head;
printf( "********************\n" );
printf( "** 请输入需要查找的邮箱 **\n" );
printf( "********************\n" );
m = 0;
gets( email );
while ( p1 != NULL )
{
while ( strcmp( p1->email, email ) != 0 && p1->next != NULL )
{
p2 = p1;
p1 = p1->next;
}
if ( strcmp( p1->email, email) == 0 )
{
m++;
system("cls");
printf( "你查找的内容是:\n" );
printf( "---姓名-------性别--------手机-------------------Email-------------通讯地址\n" );
printf( "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n" );
printf( " %7s %1s %7s %11s%18s\n", p1->name, p1->sex, p1->handset, p1->email, p1->address );
printf( "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n" );
getchar();
system("cls");
}
p1 = p1->next;
if ( m == 0 )
{
printf( "此人未在本通讯录中!\n" );
getchar();
system("cls");
}
break;
}
return(head);
}
/* 释放内存函数 */
struct txlproject *shifang( struct txlproject *head )
{
struct txlproject *p1;
while ( head != NULL )
{
p1 = head;
head = head->next;
free( p1 );
}
return(head);
}
/* 文件写入函数 */
void save( struct txlproject *head )
{
FILE *fp;
struct txlproject *p1;
if ( head == NULL )
{
printf( "通讯录为空,无法存储!\n" );
return;
}
fp = fopen( "通讯录.txt", "w" );
if ( fp == NULL )
{
printf( "cannot open file\n" );
return;
}
p1 = head;
fprintf( fp, "姓名 性别 手机 Email 通讯地址\n" );
for (; p1 != NULL; )
{
fprintf( fp, "%s %s %s %s %s\n", p1->name, p1->sex, p1->handset, p1->email, p1->address );
p1 = p1->next;
}
printf( "保存完毕!\n" );
fclose( fp );
}
/* 文件读出函数 */
struct txlproject *load( struct txlproject *head )
{
FILE *fp;
struct txlproject *p1, *p2;
fp = fopen( "通讯录.txt", "r" );
if ( fp == NULL )
{
printf( "通讯录不存在,无法输出!\n" );
getchar();
system("cls");
return(head);
} else
{
head = shifang( head );
}
p1 = (struct txlproject *) malloc( LEN );
fscanf( fp, "%s%s%s%s%s", &p1->name, &p1->sex, &p1->handset, &p1->email, &p1->address );
if ( feof( fp ) != 0 )
{
printf( "文件为空,无法打开!\n" );
getchar();
system("cls");
return(head);
} else
{
rewind( fp );
p2 = p1;
head = p1;
n = 0;
while ( feof( fp ) == 0 )
{
fscanf( fp, "%s%s%s%s%s", &p1->name, &p1->sex, &p1->handset, &p1->email, &p1->address );
if ( feof( fp ) != 0 )
break;
p2->next = p1;
p2 = p1;
p1 = (struct txlproject *) malloc( LEN );
n = n + 1;
}
p2->next = NULL;
p1 = head;
head = head->next;
n = n - 1;
free( p1 );
print( head );
// printf( "打开完毕!\n" );
return(head);
}
fclose( fp );
}
struct txlproject *autoload( struct txlproject *head ) //自动读取文件函数
{
FILE *fp;
struct txlproject *p1, *p2;
fp = fopen( "通讯录.txt", "r" );
if ( fp == NULL )
{
printf( "通讯录不存在,无法输出!\n" );
getchar();
system("cls");
return(head);
} else
{
head = shifang( head );
}
p1 = (struct txlproject *) malloc( LEN );
fscanf( fp, "%s%s%s%s%s", &p1->name, &p1->sex, &p1->handset, &p1->email, &p1->address );
if ( feof( fp ) != 0 )
{
printf( "文件为空,无法打开!\n" );
getchar();
system("cls");
return(head);
} else
{
rewind( fp );
p2 = p1;
head = p1;
n = 0;
while ( feof( fp ) == 0 )
{
fscanf( fp, "%s%s%s%s%s", &p1->name, &p1->sex, &p1->handset, &p1->email, &p1->address );
if ( feof( fp ) != 0 )
break;
p2->next = p1;
p2 = p1;
p1 = (struct txlproject *) malloc( LEN );
n = n + 1;
}
p2->next = NULL;
p1 = head;
head = head->next;
n = n - 1;
free( p1 );
return(head);
}
fclose( fp );
}
/* 主函数 */
void main()
{
struct txlproject *head = NULL;
char num[10];
while ( 1 )
{
head=autoload( head );
printf("################# 通讯录 v1.1 ###################\n");
printf("# #\n");
printf("#####################################################################\n");
printf("# #\n");
printf("# 1----------------新建通讯录------------------ #\n");
printf("# #\n");
printf("# 2----------------联系人排序------------------ #\n");
printf("# #\n");
printf("# 3----------------联系人查找------------------ #\n");
printf("# #\n");
printf("# 4----------------录入联系人------------------ #\n");
printf("# #\n");
printf("# 5----------------查看通讯录------------------ #\n");
printf("# #\n");
printf("# 6----------------删除联系人------------------ #\n");
printf("# #\n");
printf("# 7----------------修改联系人------------------ #\n");
printf("# #\n");
printf("# 8----------------退出通讯录------------------ #\n");
printf("# #\n");
printf("#####################################################################\n");
printf("# 李文轩 201808 #\n");
printf("#####################################################################\n");
gets( num );
switch ( *num )
{
case '1':
{
if ( head == NULL )
{
system("cls");
head = creat();
save( head );
/* 创建 */
system("cls");
print( head );
} else
{
head = shifang( head );
head = creat();
/* 重新创建 */
print( head );
}
save( head );
}
break;
///排序二级界面//
case '2':
{
int Check_sortkey; //决定进入什么功能
system("color 03");
system("cls");
printf("########### 通讯录 v1.0 ################\n");
printf("############################################################\n");
printf("# #\n");
printf("# 1------按姓名排序 #\n");
printf("# #\n");
printf("# 2------按电话号码排序 #\n");
printf("# #\n");
printf("############################################################\n");
printf("# 李文轩 201807 #\n");
printf("############################################################\n");
scanf("%d",&Check_sortkey);
switch(Check_sortkey)
{
case 1:
system("cls");
head=paixu_name(head);
printf("输入任意键结束");
break;
case 2:
system("cls");
head=paixu_phone(head);
printf("输入任意键结束");
break;
}
system("cls");
head = paixu_name( head );
save( head );
// getchar();
/* 排序 */
}
break;
查找二级界面/
case '3':
{
char num[10];
system("color 03");
system("cls");
printf("########### 通讯录 v1.0 ################\n");
printf("############################################################\n");
printf("# #\n");
printf("# 1------按姓名查询 #\n");
printf("# #\n");
printf("# 2------按电话号码查询 #\n");
printf("# #\n");
printf("# 3------按地址查询 #\n");
printf("# #\n");
printf("# 4------按邮箱查询 #\n");
printf("# #\n");
printf("############################################################\n");
printf("# 李文轩 201808 #\n");
printf("############################################################\n");
gets( num );
switch(*num)
{
case '1':
system("cls");
head = search_name( head );
printf("输入任意键结束");
break;
case '2':
system("cls");
head=search_phone(head);
printf("输入任意键结束");
break;
case '3':
system("cls");
head=search_address(head);
printf("输入任意键结束");
break;
case '4':
system("cls");
head=search_email(head);
printf("输入任意键结束");
break;
}
system("cls");
}
break;
case '4':
{
system("cls");
head = insert( head );
/* 增添信息 */
save( head );
print( head );
}
break;
case '5':
{
system("cls");
head = load( head );
/* 文件输出 */
}
break;
case '6':
{
system("cls");
head = delete( head );
/*删除 */
save( head );
print( head );
}
break;
case '7':
system("cls");
head = change( head );
head = changeadd( head );
/*修改 */
save( head );
print( head );
break;
case '8':
head = shifang( head );
break;
default:
system("cls");
printf( "操作错误,此项不存在!\n" );
getchar();
system("cls");
break;
}
if ( strcmp( num, "8" ) == 0 )
break;
}
}