主要实现功能:
通讯录.h文件
#ifndef _ADDRESS_LIST#define _ADDRESS_LIST#include #include#include #include#define MAX 1024typedef struct ContactPerson{char id[10];char name[32];int age;char telephone[12];}Person;void welcome();void menu();int AddInfo();void display();void search();void search_ID();void search_name();void delete();void change();#endif
通讯录main.c文件
#include#include"address_list.h"Person *g_info[MAX]={0};//数组用来存储学生信息的地址int g_count=0;//代表当前人数int main(){ welcome(); int choice; sleep(2); while(1) { menu(); scanf("%d",&choice); switch(choice) { case 1: AddInfo(); break; case 2: display(); sleep(5); break; case 3: search(); sleep(5); break; case 4: delete(); break; case 5: change(); break; case 6: exit(0); break; default: break; } } return 0;}
通讯录.c文件
#include"address_list.h"extern Person *g_info[MAX];//数组用来存储学生信息的地址extern int g_count;void menu(){ system("clear"); printf("----------------------------------------------------------------------------nn"); printf("tt1、添加信息 2、显示信息nn"); printf("tt3、搜素信息 4、删除信息nn"); printf("tt5、修改信息 6、退出系统nn"); printf("-----------------------------------------------------------------------------nn");}void welcome(){ printf("**********************************************************************nn"); printf("**********************************************************************nn"); printf("**************************欢迎使用本通讯录系统*************************nn"); printf("*****************welcome using the address_list syste*****************nn"); printf("**********************************************************************nn"); printf("**********************************************************************nn"); }int AddInfo()//添加模块{ g_info[g_count]=(Person*)malloc(sizeof(Person)*1); if(NULL ==g_info) { printf("apply filed !n"); exit(1); } int flag=1; if(g_count>1024) { printf("对不起,人数已满n"); } else { while(flag) { printf("请输入ID 姓名 年龄 电话:n"); scanf("%s%s%d%s",g_info[g_count]->id,g_info[g_count]->name, &g_info[g_count]->age,g_info[g_count]->telephone); if(strlen(g_info[g_count]->telephone)!=11) { printf("电话号码格式输入错误,请重新输入"); flag=1; continue; } int i; for(i=0;itelephone,g_info[g_count]->telephone)==0) { printf("电话重复,请重新输入!n"); break; } } g_count++; free(g_info[g_count]); break; } } return 0;}void display()//显示模块{ if (g_count == 0) { printf("通讯录为空,请先添加!!!n"); return; } for(int m=0;mname,g_info[n+1]->name); if(ret>0) { Person *tamp; tamp=g_info[n]; g_info[n]=g_info[n+1]; g_info[n+1]=tamp; printf("okn"); } } } for(int j=0;jid,g_info[j]->name, g_info[j]->age,g_info[j]->telephone); printf("************************************************n"); }}void search(){ printf("请选择搜索方式:1.ID搜索 2.用户名搜索n"); int a; scanf("%dn",&a); switch (a) { case 1: search_ID(); break; case 2: search_name(); break; default: break; }}void search_ID(){ char ch[20]; char abc; int b; scanf("%s",ch); for(b=0;bid,ch)==0) { printf("ID: %s 姓名:%s 年龄:%d 电话: %s n",g_info[b]->id,g_info[b]->name, g_info[b]->age,g_info[b]->telephone); break; } else if (b==(g_count-1)) { printf("无n"); getchar(); while(!scanf("%c",&abc)) { sleep(1); } break; } } }void search_name(){ printf("请输入你要搜索的姓名n"); char nam[20]; char ab; int c; scanf("%s",nam); for(c=0;cname,nam)==0) { printf("ID: %s 姓名:%s 年龄:%d 电话: %s n",g_info[c]->id,g_info[c]->name, g_info[c]->age,g_info[c]->telephone); break; } else if (c==(g_count-1)) { printf("无n"); break; } }}void delete(){ char user[20];int c = 0;printf("删除用户!n");printf("请输入要删除的用户姓名:n");scanf("%s", user);for (c = 0;cname ) == 0){for(int j=c;jname); g_info[j] = g_info[j+1]; } g_count--;}}printf("删除成功!n");}void change(){ char name[20]; int c=0;printf("请输入要修改的用户姓名:n");scanf("%s", name); for (c = 0;cname ) == 0){ printf("请输入修改的: ID, 姓名, 年龄, 电话nn"); scanf("%s%s%d%s",g_info[c]->id,g_info[c]->name, &g_info[c]->age,g_info[c]->telephone); break;}}}