实现一个通讯录;
通讯录可以用来存储1000个人的信息,每个人的信息包括:
姓名、性别、年龄、电话、住址
提供方法:
1. 添加联系人信息
2. 删除指定联系人信息
3. 查找指定联系人信息
4. 修改指定联系人信息
5. 显示所有联系人信息
6. 清空所有联系人
main.c
#include "contact.h" //查找联系人,返回下标; int find(pCon pcon,char *name) { int i=0; while(i < pcon->num) { if(strcmp(name,pcon->telbook[i].name) == 0) { return i; } i++; } return -1; } /****************添加联系人****************/ void add_peo(pCon pcon) { if(pcon->num==NUM_MAX) { printf("电话本已满,无法存储!"); return ; } printf("姓名:"); scanf("%s",pcon->telbook[pcon->num].name); printf("性别:"); scanf("%s",pcon->telbook[pcon->num].sex); printf("年龄:"); scanf("%d",&(pcon->telbook[pcon->num].age)); printf("电话:"); scanf("%s",pcon->telbook[pcon->num].tel); printf("住址:"); scanf("%s",pcon->telbook[pcon->num].addr); pcon->num++; printf("添加成功,请按任意键继续..."); getch(); } /****************删除联系人****************/ void del_peo(pCon pcon) { char delname[NAME_MAX]; int ret = 0; printf("请输入要删除的联系人姓名:"); scanf("%s",delname); ret = find(pcon,delname); if(0 == ret) { int j; for(j=ret;j<pcon->num-1;j++) { pcon->telbook[j]=pcon->telbook[j+1]; } pcon->num--; printf("删除成功!\n"); } else { printf("未找到此联系人!\n"); } printf("请按任意键继续..."); getch(); } /****************查找联系人****************/ void serach_peo(pCon pcon) { char sername[NAME_MAX]; int ret = 0; printf("请输入要查找的联系人姓名:"); scanf("%s",sername); ret = find(pcon,sername); if(0 == ret) { printf("%s\t%s\t%d\t%s\t%s\n",pcon->telbook[ret].name,pcon->telbook[ret].sex,pcon->telbook[ret].age,pcon->telbook[ret].tel,pcon->telbook[ret].addr); printf("查找完成!"); } else { printf("未找到此联系人!\n"); } printf("请按任意键继续..."); getch(); } /****************修改联系人****************/ void modify_peo(pCon pcon) { char modname[NAME_MAX]; int ret = 0; printf("请输入要修改的联系人姓名:"); scanf("%s",modname); ret = find(pcon,modname); if(0 == ret) { 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); printf("修改成功!"); } else { printf("无此联系人信息,无法修改。\n"); } printf("请按任意键继续..."); getch(); } /****************显示联系人****************/ void show_peo(pCon pcon) { int i; if(pcon->num == 0) { printf("电话本为空!"); } else { for(i=0;i<pcon->num;i++) { printf("%s\t%s\t%d\t%s\t%s\n",pcon->telbook[i].name,pcon->telbook[i].sex,pcon->telbook[i].age,pcon->telbook[i].tel,pcon->telbook[i].addr); } } printf("请按任意键继续..."); getch(); } /****************清空联系人****************/ void clear_peo(pCon pcon) { pcon->num = 0; printf("清空完成,请按任意键继续..."); getch(); }
test.c
#include "contact.h" char show_menu() { printf("***************************************\n"); printf("* 菜单 *\n"); printf("* 1.添加联系人 2.删除联系人 *\n"); printf("* 3.查找联系人 4.修改联系人 *\n"); printf("* 5.显示联系人 6.清空联系人 *\n"); printf("* 0.退出 *\n"); printf("***************************************\n"); return 0; } int main() { struct contact my_contact; int input; my_contact.num=0; do { system("cls"); show_menu(); printf("请选择你所需的操作:"); x: scanf("%d",&input); if(input>=0 && input <=6) { switch(input) { case 0: return 0; case 1: { add_peo(&my_contact); break; } case 2: { del_peo(&my_contact); break; } case 3: { serach_peo(&my_contact); break; } case 4: { modify_peo(&my_contact); break; } case 5: { show_peo(&my_contact); break; } case 6: { clear_peo(&my_contact); break; } default: break; } } else { printf("输入有误,请重新输入:"); goto x; } }while(input>=0 && input<=6); return 0; }
contact.h
#ifndef __CONTACT_H__ #define __CONTACT_H__ #include <stdio.h> #include <string.h> #include <stdlib.h> #include <conio.h> #define NAME_MAX 20 #define SEX_MAX 5 #define TEL_MAX 12 #define ADDR_MAX 20 #define NUM_MAX 1000 typedef struct people { char name[NAME_MAX]; char sex[SEX_MAX]; int age; char tel[TEL_MAX]; char addr[ADDR_MAX]; }pPeople; typedef struct contact { int num; struct people telbook[NUM_MAX]; }*pCon; void add_peo(pCon pcon); void del_peo(pCon pcon); void serach_peo(pCon pcon); void modify_peo(pCon pcon); void show_peo(pCon pcon); void clear_peo(pCon pcon); #endif
程序运行结果截图:
时间: 2024-11-10 18:07:57