//【C语言】通讯录(一个文件实现)
#include <stdio.h> #include <string.h> #define NAME_MAX 20 #define SEX_MAX 5 #define TEL_MAX 11 #define ADDR_MAX 20 #define PERSON_MAX 1000 typedef struct person { char name[NAME_MAX]; char sex[SEX_MAX]; int age; char tel[TEL_MAX]; char addr[ADDR_MAX]; }*pPerson; typedef struct contact { struct person TelBook[PERSON_MAX]; int count; }*pCon; void print() { printf(" ************************************************************************\n"); printf(" || ||\n"); printf(" || 1. 添加联系人 2. 删除联系人 3.查找联系人 ||\n"); printf(" || 4. 修改联系人 5. 显示联系人 6.清空联系人 ||\n"); printf(" || ||\n"); printf(" ************************************************************************\n"); } int find(pCon pcon,char *name) { int i=0; for(i=0;i<pcon->count;i++) { if(strcmp((pcon->TelBook[i]).name,name)==0) return i; } return -1; } void add_peo(pCon pcon) { if((pcon->count)==PERSON_MAX) printf("通讯录已满,请删除后再添加。\n"); printf("姓名:"); scanf("%s",(pcon->TelBook[pcon->count]).name); printf("性别:"); scanf("%s",(pcon->TelBook[pcon->count]).sex); printf("年龄:"); scanf("%d",&(pcon->TelBook[pcon->count]).age); printf("电话:"); scanf("%s",(pcon->TelBook[pcon->count]).tel); printf("家庭住址:"); scanf("%s",(pcon->TelBook[pcon->count]).addr); pcon->count++; } void del_peo(pCon pcon) { int ret=0; char peoplename[NAME_MAX]; printf("请输入您要删除的联系人:"); scanf("%s",peoplename); ret=find(pcon,peoplename); if(ret==-1) { printf("联系人不存在。\n"); } else { int j; for(j=ret;j<pcon->count-1;j++) { pcon->TelBook[j]=pcon->TelBook[j+1]; } printf("删除联系人成功。\n"); } pcon->count--; } void search_peo(pCon pcon) { int ret=0; char peoplename[NAME_MAX]; printf("请输入您要查找的联系人:"); scanf("%s",peoplename); ret=find(pcon,peoplename); if(ret==-1) { printf("联系人不存在。\n"); } else { printf(" %s\t",(pcon->TelBook[ret]).name); printf(" %s\t",(pcon->TelBook[ret]).sex); printf(" %d\t",(pcon->TelBook[ret]).age); printf(" %s\t",(pcon->TelBook[ret]).tel); printf(" %s\n",(pcon->TelBook[ret]).addr); } } void modil_peo(pCon pcon) { int ret=0; char peoplename[NAME_MAX]; printf("请输入您要修改的联系人:"); scanf("%s",peoplename); ret=find(pcon,peoplename); if(ret==-1) { printf("联系人不存在。"); } else { printf("姓名:"); scanf("%s",(pcon->TelBook[ret]).name); printf("性别:"); scanf("%s",(pcon->TelBook[ret]).sex); printf("年龄:"); scanf("%d",&(pcon->TelBook[ret]).age); printf("电话:"); scanf("%s",(pcon->TelBook[ret]).tel); printf("家庭住址:"); scanf("%s",(pcon->TelBook[ret]).addr); } } void show_peo(pCon pcon) { int i; for(i=0;i<pcon->count;i++) { printf(" %d.%s\t",i+1,(pcon->TelBook[i]).name); printf(" %s\t",(pcon->TelBook[i]).sex); printf(" %d\t",(pcon->TelBook[i]).age); printf(" %s\t",(pcon->TelBook[i]).tel); printf(" %s\n",(pcon->TelBook[i]).addr); } } void clear_peo(pCon pcon) { pcon->count=0; printf("通讯录已清空。\n"); } int main () { struct contact my_contact; int input; my_contact.count=0; while(1) { print(); printf("请选择操作:"); scanf("%d",&input); switch(input) { case 1: add_peo(&my_contact); break; case 2: del_peo(&my_contact); break; case 3: search_peo(&my_contact); break; case 4: modil_peo(&my_contact); break; case 5: show_peo(&my_contact); break; case 6: clear_peo(&my_contact); break; default: printf("操作错误\n"); break; } } return 0; }
时间: 2024-12-29 01:54:09