#ifndef _CONTACTS_H_ #define _CONTACTS_H_ #define NAME_LENGTH 20 #define SEX_LENGTH 10 #define TELE_LENGTH 11 #define ADDR_LENGTH 30 #define MAX 1000 struct PeopleInfo { char name[NAME_LENGTH]; int age; char sex[SEX_LENGTH]; char tele[TELE_LENGTH]; char addr[ADDR_LENGTH]; }; struct Contacts { struct PeopleInfo person[MAX]; int count; } typedef struct Contacts *pcontacts; int add_contacts(pcontacts pcon); int dele_contacts(pcontacts pcon); int clear_contacts(pcontacts pcon); int find_contacts(pcontacts pcon); int modify_contacts(pcontacts pcon); void show_contacts(pcontacts pcon); #endif _CONTACTS_H_
contacts.c
/*实现一个通讯录; 通讯录可以用来存储1000个人的信息,每个人的信息包括: 姓名、性别、年龄、电话、住址 提供方法: 1. 添加联系人信息 2. 删除指定联系人信息 3. 查找指定联系人信息 4. 修改指定联系人信息 5. 显示所有联系人信息 6. 清空所有联系人*/ #include"contacts.h" #include<string.h> int find_entry(pcontacts pcon) { int i=0; char name[NAME_LENGTH]; printf("input name:"); scanf("%s",name); for(i=0;i<pcon->count;i++) { if(strcmp(person[i].name,name)==0) { return i; } } return -1; } int add_contacts(pcontacts pcon) { if(pcon->count==max) { printf("full!"); } printf("input name:"); scanf("%s",pcon->person[pcon->count].name); printf("input age:"); scanf("%d",&pcon->person[pcon->count].age); printf("input sex:"); scanf("%s",pcon->person[pcon->count].sex); printf("input tele:"); scanf("%s",pcon->person[pcon->count].tele); printf("input addr:"); scanf("%s",pcon->person[pcon->count].addr); pcon->count++; return 1; } int dele_contacts(pcontacts pcon) { int i=0; int ret=find_entry(pcon); if(ret!=-1) { for(i=ret;i<pcon->count;i++) { pcon->person[i]=pcon->person[i+1]; } pcon->count--; return 1; } else { printf("not exist!\n"); return -1; } } int clear_contacts(pcontacts pcon) { pcon->count=0; return 1; } int find_contacts(pcontacts pcon) { int ret = find_entry(pcon); if(ret != -1) { printf("name:%s",pcon->person[ret].name); return 1; } else { printf("not exist!\n"); return -1; } } int modify_contacts(pcontacts pcon) { int ret = find_entry(pcon); if(ret != -1) { printf("input name:"); scanf("%s",pcon->person[ret].name); printf("input sex:"); scanf("%s",pcon->person[ret].sex); printf("input age:"); scanf("%s",pcon->person[ret].age); printf("input tele:"); scanf("%s",pcon->person[ret].tele); printf("input addr:"); scanf("%s",pcon->person[ret].addr); return 1; } else { printf("not exist!\n"); return -1; } } void show_contacts(pcontacts pcon) { int i=0; printf("\tname\tage\tsex\ttele\taddr"); for(i=0;i<pcon->count;i++) { printf("%10s\t",pcon->person[i].name); printf("%5s\t",pcon->person[i].sex); printf("%5d\t",pcon->person[i].age); printf("%12s\t",pcon->person[i].tele); printf("%20s\t",pcon->person[i].addr); } printf("\n"); }
主函数
#include<stdio.h> #include<string.h> #include"contacts.h" int main() { int n = 1; struct Contacts people; people.count = 0; printf("input number:"); scanf("%d",&n); switch(n) { case 1:add_contacts(&people); break; case 2:dele_contacts(&people); break; case 3:clear_contacts(&people); break; case 4:find_contacts(&people); break; case 5:modify_contacts(&people); break; case 6:show_contacts(&people); break; default: printf("not exist\n"); break; } return 0; }