头文件
# ifndef __LINKMAN_H__
# define __LINKMAN_H__ # include <stdio.h>
# include <string.h>
# pragma warning ( disable: 4996 )
typedef struct LINKMAN
{ char name[ 20 ] ; char sex[ 10 ] ; int age; int tel[ 12 ] ; char addr[ 50 ] ;
} LINKMAN; typedef struct Statis
{ LINKMAN num[ 1000 ] ; int count;
} Statis; void inint_linkman ( Statis* p) ;
void Add_linkman ( Statis* p) ;
void Dele_linkman ( Statis* p) ;
void Find_linkman ( Statis* p) ;
void Revise_linkman ( Statis* p) ;
void Display_linkman ( Statis* p) ;
void Empty_linkman ( Statis* p) ;
void sort_linkman ( Statis* p) ;
# endif
实现文件
# include "test.h"
void inint_linkman ( Statis* p)
{ int count = sizeof ( p-> num) ; p-> count = 0 ; memset ( p-> num, 0 , count) ;
}
int Find ( Statis* p, char * pname)
{ int i = 0 ; for ( i = 0 ; i < ( p-> count) ; i++ ) { if ( strcmp ( p-> num[ i] . name, pname) == 0 ) return i; } return - 1 ;
} void menu1 ( )
{ printf ( "*********************\n" ) ; printf ( "****1.姓名*2.性别****\n" ) ; printf ( "****3.年龄*4.电话****\n" ) ; printf ( "****5.地址*6.返回****\n" ) ; printf ( "*********************\n" ) ;
} void Add_linkman ( Statis* p)
{ printf ( "请输入你要添加的联系人姓名:\n" ) ; scanf ( "%s" , p-> num[ p-> count] . name) ; printf ( "请输入你要添加的联系人性别:\n" ) ; scanf ( "%s" , p-> num[ p-> count] . sex) ; printf ( "请输入你要添加的联系人年龄:\n" ) ; scanf ( "%d" , & ( p-> num[ p-> count] . age) ) ; printf ( "请输入你要添加的联系人电话:\n" ) ; scanf ( "%s" , p-> num[ p-> count] . tel) ; printf ( "请输入你要添加的联系人地址:\n" ) ; scanf ( "%s" , p-> num[ p-> count] . addr) ; if ( ( p-> count) > 1000 ) { printf ( "联系人上限\n" ) ; } else { printf ( "添加成功\n" ) ; p-> count++ ; }
} void Dele_linkman ( Statis* p)
{ char name[ 20 ] = { 0 } ; int result = 0 ; int n = 0 ; int i = 0 ; printf ( "请输入要删除人的姓名:\n" ) ; scanf ( "%s" , name) ; result = Find ( p, name) ; if ( result != - 1 ) { printf ( "你是否要删除该联系人?\n" ) ; printf ( "删除请按1,不删除请按0\n" ) ; scanf ( "%d" , & n) ; if ( n == 1 ) { for ( i = 0 ; i < ( p-> count) - 1 ; i++ ) { p-> num[ i] = p-> num[ i + 1 ] ; } p-> count-- ; printf ( "删除成功\n" ) ; } else { printf ( "删除失败\n" ) ; } } else { printf ( "你要删除的联系人不存在\n" ) ; }
} void Find_linkman ( Statis* p)
{ char name[ 20 ] = { 0 } ; int result = 0 ; printf ( "请输入要查找联系人的姓名:\n" ) ; scanf ( "%s" , name) ; result = Find ( p, name) ; if ( result != - 1 ) { printf ( "姓名:%s\n" , p-> num[ result] . name) ; printf ( "性别:%s\n" , p-> num[ result] . sex) ; printf ( "年龄:%d\n" , p-> num[ result] . age) ; printf ( "电话:%s\n" , p-> num[ result] . tel) ; printf ( "地址:%s\n" , p-> num[ result] . addr) ; } else { printf ( "你要查找的联系人不存在\n" ) ; }
} void Revise_linkman ( Statis* p)
{ char name[ 20 ] = { 0 } ; int result = 0 ; printf ( "请输入你要修改联系人的姓名:\n" ) ; scanf ( "%s" , name) ; result = Find ( p, name) ; if ( result != - 1 ) { printf ( "姓名:%s\n" , p-> num[ result] . name) ; printf ( "性别:%s\n" , p-> num[ result] . sex) ; printf ( "年龄:%d\n" , p-> num[ result] . age) ; printf ( "电话:%s\n" , p-> num[ result] . tel) ; printf ( "地址:%s\n" , p-> num[ result] . addr) ; int i = 0 ; do { menu1 ( ) ; printf ( "输入你要修改的选项:\n" ) ; scanf ( "%d" , & i) ; switch ( i) { case 1 : printf ( "请把姓名修改成:" ) ; scanf ( "%s" , p-> num[ result] . name) ; break ; case 2 : printf ( "请把性别修改成:" ) ; scanf ( "%s" , p-> num[ result] . sex) ; break ; case 3 : printf ( "请把年龄修改成:" ) ; scanf ( "%d" , & ( p-> num[ result] . age) ) ; break ; case 4 : printf ( "请把电话修改成:" ) ; scanf ( "%s" , p-> num[ result] . tel) ; break ; case 5 : printf ( "请把地址修改成:" ) ; scanf ( "%s" , p-> num[ result] . addr) ; break ; case 0 : break ; default : printf ( "输入错误" ) ; break ; } } while ( i) ; } else { printf ( "你要修改的联系人不存在\n" ) ; }
} void Display_linkman ( Statis* p)
{ int i = 0 ; printf ( "输出所有人的信息:\n" ) ; printf ( "%10s%7s%6s%8s%10s\n" , "名字" , "性别" , "年龄" , "电话" , "住址" ) ; for ( i = 0 ; i < ( p-> count) ; i++ ) { printf ( "%11s" , p-> num[ i] . name) ; printf ( "%5s" , p-> num[ i] . sex) ; printf ( "%5d" , p-> num[ i] . age) ; printf ( "%10s" , p-> num[ i] . tel) ; printf ( "%12s" , p-> num[ i] . addr) ; printf ( "\n" ) ; }
} void Empty_linkman ( Statis* p)
{ p-> count = 0 ;
} void sort_linkman ( Statis* p)
{ int i = 0 ; int j = 0 ; for ( i = 0 ; i < p-> count - 1 ; i++ ) for ( j = 0 ; j < p-> count - 1 - i; j++ ) { if ( strcmp ( p-> num[ j] . name, p-> num[ j + 1 ] . name) > 0 ) { LINKMAN tmp; tmp = p-> num[ j] ; p-> num[ j] = p-> num[ j + 1 ] ; p-> num[ j + 1 ] = tmp; } }
}
测试文件
# include "test.h"
Statis sta;
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 ( "************按0退出程序**********\n" ) ; printf ( "***********************************\n" ) ; } void test ( )
{ int input = 0 ; do { menu ( ) ; printf ( "请输入你要进行的选项:\n" ) ; scanf ( "%d" , & input) ; switch ( input) { case 1 : Add_linkman ( & sta) ; break ; case 2 : Dele_linkman ( & sta) ; break ; case 3 : Find_linkman ( & sta) ; break ; case 4 : Revise_linkman ( & sta) ; break ; case 5 : Display_linkman ( & sta) ; break ; case 6 : Empty_linkman ( & sta) ; break ; case 7 : sort_linkman ( & sta) ; break ; case 0 : exit ( 1 ) ; break ; default : printf ( "输入错误,请重新输入\n" ) ; break ; } } while ( input) ;
} int main ( )
{ inint_linkman ( & sta) ; test ( ) ; return 0 ;
}