赞
踩
- #define _CRT_SECURE_NO_WARNINGS 1
- #define MAX 100
- #define MAX_NAME 20
- #define MAX_SEX 5
- #define MAX_TELE 15
- #define MAX_ADDR 30
- #include<stdio.h>
- #include<string.h>
- enum Option
- {
- EXIT,
- ADD,
- DEL,
- SEARCH,
- MODIFY,
- SHOW
- };
- struct PeoInfo
- {
- char name[MAX_NAME];
- int age;
- char sex[MAX_SEX];
- char tele[MAX_TELE];
- char addr[MAX_ADDR];
- };
- //通讯录声明
- struct Contact
- {
- struct PeoInfo data[MAX];//存放一个信息
- int size;//记录当前已有的元素个数
- };
- //函数声明
- //初始化通讯录
- void InitContact(struct Contact* ps);
- //添加联系人
- void AddContact(struct Contact* ps);
- //展示通讯录
- void ShowContact(const struct Contact* ps);
- //删除联系人
- void DelContact(struct Contact* ps);
- //查找联系人
- void SearchContact(const struct Contact* ps);
- //修改联系人信息
- void ModdifyContact(struct Contact* ps);
- #include "contact.h"
- void InitContact(struct Contact* ps)
- {
- memset(ps->data, 0, sizeof(ps->data));
- ps->size = 0;
- }
- void AddContact(struct Contact* ps)
- {
- if (ps->size == MAX)
- {
- printf("通讯录已满,无法增加\n");
- }
- else
- {
- printf("请输入名字:");
- scanf("%s", ps->data[ps->size].name);
- printf("请输入年龄:");
- scanf("%d", &(ps->data[ps->size].age));
- printf("请输入性别:");
- scanf("%s", ps->data[ps->size].sex);
- printf("请输入电话:");
- scanf("%s", ps->data[ps->size].tele);
- printf("请输入地址:");
- scanf("%s", ps->data[ps->size].addr);
- ps->size++;
- printf("添加成功\n");
- }
- }
- void ShowContact(const struct Contact* ps)
- {
- if (ps->size == 0)
- {
- printf("通讯录为空\n");
- }
- else
- {
- int i = 0;
- printf("%-10s\t%-4s\t%-5s\t%-12s\t%-20s\n", "名字", "年龄", "性别", "电话", "地址");
- for (i = 0; i < ps->size; i++)
- {
- printf("%-10s\t%-4d\t%-5s\t%-12s\t%-20s\n",
- ps->data[i].name,
- ps->data[i].age,
- ps->data[i].sex,
- ps->data[i].tele,
- ps->data[i].addr);
- }
- }
- }
- static int FindByName(const struct Contact* ps, char name[MAX_NAME])
- {
- int i = 0;
- for (i = 0; i < ps->size; i++)
- {
- if (strcmp(ps->data[i].name, name) == 0)
- return i;
- }
- return -1;
- }
- void DelContact(struct Contact* ps)
- {
- char name[MAX_NAME];
- printf("请输入删除人的名字:");
- scanf("%s", name);
- //查找要删除的人在什么位置
- int pos = FindByName(ps, name);//找到返回下标,否则返回-1
- if (pos == -1)
- printf("联系人不存在\n");
- else
- {
- int j = 0;
- for (j = pos; j < ps->size - 1; j++)
- ps->data[j] = ps->data[j + 1];
- ps->size--;
- printf("删除成功\n");
- }
- }
- void SearchContact(const struct Contact* ps)
- {
- char name[MAX_NAME];
- printf("请输入查找人的名字:");
- scanf("%s", name);
- int pos = FindByName(ps, name);
- if (pos == -1)
- printf("联系人不存在\n");
- else
- {
- printf("%-10s\t%-4s\t%-5s\t%-12s\t%-20s\n", "名字", "年龄", "性别", "电话", "地址");
- printf("%-10s\t%-4d\t%-5s\t%-12s\t%-20s\n",
- ps->data[pos].name,
- ps->data[pos].age,
- ps->data[pos].sex,
- ps->data[pos].tele,
- ps->data[pos].addr);
- }
- }
- void ModdifyContact(struct Contact* ps)
- {
- char name[MAX_NAME];
- printf("请输入要修改联系人名字:");
- scanf("%s", name);
- int pos = FindByName(ps, name);
- if (pos == -1)
- printf("要修改的联系人不存在\n");
- else
- {
- printf("请输入年龄:");
- scanf("%d", &(ps->data[pos].age));
- printf("请输入性别:");
- scanf("%s", ps->data[pos].sex);
- printf("请输入电话:");
- scanf("%s", ps->data[pos].tele);
- printf("请输入地址:");
- scanf("%s", ps->data[pos].addr);
- printf("信息已重新修改完成\n");
- }
- }
- #include "contact.h"
- void menu()
- {
- printf("** 1.添加 2.删除 **\n");
- printf("** 3.查找 4.修改 **\n");
- printf("** 5.显示 0.退出 **\n");
- }
- int main()
- {
- int input = 0;
- struct Contact con;
- //初始化通讯录
- InitContact(&con);
- do
- {
- menu();
- printf("请选择:");
- scanf("%d", &input);
- switch (input)
- {
- case ADD:
- AddContact(&con);
- break;
- case DEL:
- DelContact(&con);
- break;
- case SEARCH:
- SearchContact(&con);
- break;
- case MODIFY:
- ModdifyContact(&con);
- break;
- case SHOW:
- ShowContact(&con);
- break;
- case EXIT:
- printf("退出通讯录\n");
- break;
- default:
- printf("输入有误,请重新输入:");
- break;
- }
- } while (input);
- return 0;
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。