#include<stdio.h> #include<stdlib.h> #include<string.h> #define MAX 1001 struct LXR { char name[20]; int age; char sex[5]; char tel[12]; char add[30]; }; typedef struct { struct LXR b[MAX]; int p_size; }DHB, *PDHB; int flag(PDHB dhb, char *name); void add(PDHB dhb); void delete(PDHB dhb); void search(PDHB dhb); void modify(PDHB dhb); void show(PDHB dhb); void empty(PDHB dhb); int flag(PDHB dhb, char *name) { int i = 0; for (i = 0; i < dhb->p_size; i++) { if (strcmp(dhb->b[i].name, name)==0)/*结构体指针:C++语言规定,通过非指针变量访问类或者结构体 的成员都可以使用.来实现,而通过指针变量访问类或者结构 体的成员可以使用两种方法来实现:1种是:“(*变量名). ”(注意:括号不能省),另1种是“变量名-> ”。 */ { return i; } } return -1; } void add(PDHB dhb){ if (dhb->p_size < MAX) { printf("\nname:"); scanf("%s", dhb->b[dhb->p_size].name); printf("\nage:"); scanf("%s", &dhb->b[dhb->p_size].age); printf("\nsex:"); scanf("%s", dhb->b[dhb->p_size].sex); printf("\ntel:"); scanf("%s", dhb->b[dhb->p_size].tel); printf("\nadd:"); scanf("%s", dhb->b[dhb->p_size].add); dhb->p_size++; printf("\n已成功添加\n"); } else printf("已无空间添加,若要添加,请删除后再添加\n"); } void delete(PDHB dhb){ char name[20] = { 0 }; int i = 0; int ret; printf("请输入所要删除人信息的姓名:"); scanf("%s", name); ret= flag(dhb, name); if (ret >= 0) { for (i = ret; i < dhb->p_size; i++) dhb->b[i] = dhb->b[i++]; dhb->p_size--; printf("\n已成功删除\n"); } else printf("没查找到该联系人,无法进行删除,请确认是否输入正确\n"); } void search(PDHB dhb){ char name[20] =""; int i = 0; int ret=0; printf("请输入所要查找人信息的姓名:"); scanf("%s", name); ret = flag(dhb, name); if (ret >= 0) { printf("name:%s\nage:%d\nsex:%s\ntel:%s\nadd:%s\n", dhb->b[ret].name, dhb->b[ret].age, dhb->b[ret].sex, dhb->b[ret].tel , dhb->b[ret].add); } else printf("没查找到该联系人,请确认是否输入正确\n"); } void modify(PDHB dhb){ char name[20] = { 0 }; int i = 0; int ret; printf("请输入所要修改人信息的姓名:"); scanf("%s", name); ret = flag(dhb, name); if (ret >= 0) { printf("\nname:"); scanf("%s", dhb->b[ret].name); printf("\nage:"); scanf("%s", &dhb->b[ret].age); printf("\nsex:"); scanf("%s", dhb->b[ret].sex); printf("\ntel:"); scanf("%s", dhb->b[ret].tel); printf("\nadd:"); scanf("%s", dhb->b[ret].add); printf("\n已成功修改\n"); } else printf("没查找到该联系人,无法进行修改,请确认是否输入正确\n"); } void show(PDHB dhb){ int ret = 0; for (ret= 0; ret < dhb->p_size; ret++) { printf("name:%s\nage:%d\nsex:%s\ntel:%s\nadd:%s\n", (*dhb).b[ret].name, dhb->b[ret].age, dhb->b[ret].sex, dhb->b[ret].tel , dhb->b[ret].add); } } void empty(PDHB dhb){ dhb->p_size = 0; printf("已清空"); } void print() { printf("*************请选择功能************\n"); printf("****************0:退出************\n"); printf("****************1:添加*************\n"); printf("****************2:删除*************\n"); printf("****************3:查找*************\n"); printf("****************4:修改*************\n"); printf("****************5:显示*************\n"); printf("****************6:清空*************\n"); printf("****************请输入*************\n"); } enum MENU { RET, ADD, DELETE, SEARCH, MODIFY, SHOW, EMPTY, }; int main() { int input = 0;DHB dhb = { 0 }; dhb.p_size = 0; while (1) { print(); scanf("%d", &input); switch (input) { case RET: exit(0); case ADD: add(&dhb);//注意&,要不只是临时拷贝 break; case DELETE: delete(&dhb); break; case SEARCH: search(&dhb); break; case MODIFY: modify(&dhb); break; case SHOW: show(&dhb); break; case EMPTY: empty(&dhb); break; default: printf("输入错误"); break; } } system("pause"); return 0; }
本文出自 “小止” 博客,请务必保留此出处http://10541556.blog.51cto.com/10531556/1696535
原文地址:http://10541556.blog.51cto.com/10531556/1696535