【Ex.】通讯录的增删查改清空以及排序操作的实现

来源:互联网 发布:网络机柜的作用 编辑:程序博客网 时间:2024/05/21 18:30

C语言的结构体能够帮我们实现一些程序的增删查改操作,能够让我们更轻易的实现一些操作,在结构体中,一定要注意传参和类型的定义,而且一个小小的建议就是在写程序时可以写一点测一点,这样容易排查错误,最好不要全部写完了再测。

//contact.h#include <stdio.h>#include <stdlib.h>#include <string.h>#include <assert.h>#define MAX_NAME 20#define MAX_SEX 5#define MAX_ADD 25#define MAX_TEL 15#define MAX 1000typedef struct PeoInfo {    char name[MAX_NAME];    char sex[MAX_SEX];    int age;    char add[MAX_ADD];    char tel[MAX_TEL];}PeoInfo;typedef struct contact{    PeoInfo data[MAX];    int sz;}Contact,*pCon;void InitContact(pCon p);void AddContact(pCon p);void ShowContact(pCon p);void DeletContact(pCon p);void SearchContact(pCon p);void ModifyContact(pCon p);void EmptyContact(pCon p);void SortContact(pCon p);
//测试函数#define _CRT_SECURE_NO_WARNINGS 1/***************************************** 实现通讯录的增删查改, 显示以及清空排序**       1. 添加联系人信息 *       2. 删除指定联系人信息 *       3. 查找指定联系人信息 *       4. 修改指定联系人信息 *       5. 显示所有联系人信息 *       6. 清空所有联系人 *       7. 以名字排序所有联系人*****************************************/#include "contact.h"void menu(){    printf("\n");    printf("************************************\n");    printf("******  1.Add       2.Delet   ******\n");    printf("******  3.Serch     4.Modify  ******\n");    printf("******  5.Show      6.Empty   ******\n");    printf("******  7.Sort      0.Exit    ******\n");    printf("************************************\n");    printf("\n");}enum  Option{    EXIT,    Add,    Delet,    Search,    Modify,    Show,    Empty,    Sort};void test(){    int input = 0;    Contact con;    InitContact(&con);    do{        menu();        printf("请选择:");        scanf("%d", &input);        switch (input)        {        case Add :            AddContact(&con);            break;        case Delet:            DeletContact(&con);            break;         case Search:            SearchContact(&con);            break;        case Modify:            ModifyContact(&con);            break;         case Show:            ShowContact(&con);            break;        case Empty:            EmptyContact(&con);            break;         case Sort:            SortContact(&con);            break;        case EXIT:            break;        default:            printf("选择错误\n");            break;        }    } while (input);}int main(){    test();    system("pause");    return 0;}
//具体函数接口的实现//contact.c#define _CRT_SECURE_NO_WARNINGS 1#include "contact.h"//初始化void InitContact(pCon p){    assert(p != NULL);    memset(p->data, 0, sizeof(p->data));    p->sz = 0;}//添加联系人void AddContact(pCon p){    if (p->sz == MAX)    {        printf("通讯录已满\n");    }    else    {        printf("\n");        printf("请输入姓名:");        scanf("%s", p->data[p->sz].name);        printf("\n");        printf("请输入年龄:");        scanf("%d", &(p->data[p->sz].age));        printf("\n");        printf("请输入性别:");        scanf("%s", p->data[p->sz].sex);        printf("\n");        printf("请输入地址:");        scanf("%s", p->data[p->sz].add);        printf("\n");        printf("请输入电话:");        scanf("%s", p->data[p->sz].tel);        printf("\n");        printf("添加成功\n");    }    p->sz++;}//显示联系人void ShowContact(pCon p){    int i = 0;         printf("  %5s        %5s         %5s         %5s          %5s\n","name","age","sex","add","tel");    for (i = 0; i < p->sz; i++)    {        printf("  %5s        %5d         %5s         %5s          %5s  \n",            p->data[i].name,p->data[i].age,p->data[i].sex,p->data[i].add,p->data[i].tel);    }}//删除联系人int FindCon(pCon p,char name[]){    int i = 0;    for (i = 0; i < p->sz; i++)    {        if (strcmp(name, p->data[i].name)==0)//在存储中寻找是否有输入的联系人        {            return i;        }    }    return -1;}void DeletContact(pCon p){    char name[MAX] = { 0 };    int pos = 0;    printf("请输入要删除的联系人:");    scanf("%s", name);    pos = FindCon(p,name);    if (pos == -1)    {        printf("没找到\n");    }    else    {        int i = 0;        for (i = pos; i < p->sz - 1; i++)        {            p->data[i] = p->data[i + 1];        }        p->sz--;        printf("删除成功\n");        printf("\n");    }}//查找联系人void SearchContact(pCon p){    char name[MAX] = { 0 };    printf("请输入你要查找的联系人的姓名:");    scanf("%s", name);    int k = FindCon(p, name);    if (k==-1)    {        printf("输入有误,无法找到联系人!\n");        return;    }    else    {        printf("%5s", p->data[k].name);        printf("%5d", p->data[k].age);        printf("%5s", p->data[k].sex);        printf("%5s", p->data[k].add);        printf("%5s\n", p->data[k].tel);        printf("查找成功\n");    }    return;}//修改指定联系人信息void ModifyContact(pCon p){    printf("请输入想修改联系人的姓名:");    char name[MAX] = { 0 };    scanf("%s", name);    int k = FindCon(p, name);    if (k == -1)    {        printf("输入有误,无法找到联系人!\n");        return;    }    else    {        printf("请输入年龄:");        scanf("%d", &(p->data[k].age));        printf("\n");        printf("请输入性别:");        scanf("%s", p->data[k].sex);        printf("\n");        printf("请输入地址:");        scanf("%s", p->data[k].add);        printf("\n");        printf("请输入电话:");        scanf("%s", p->data[k].tel);        printf("\n");        printf("修改成功\n");    }    return;}//清空所有联系人void EmptyContact(pCon p){    int i = 0;    if (p->sz == 0)    {        return;    }    else    {        p->sz = 0;        printf("清空成功\n");    }    return;}//以名字排序所有联系人void Swap(char* left, char *right){    char tmp = *left;    *left = *right;    *right = tmp;}void SortContact(pCon p){    int i = 0;    int j = 0;    PeoInfo tmp;    if (p->sz == 0)    {        return;    }    for (; i < p->sz-1; i++)    {        for (; j < p->sz - i-1; j++)        {            if (strcmp(p->data[j].name, p->data[j + 1].name)>0)            {                tmp = p->data[j];                p->data[j] = p->data[j + 1];                p->data[j + 1] = tmp;            }            else if (strcmp(p->data[j].name, p->data[j + 1].name) < 0)            {                tmp = p->data[j+1];                p->data[j+1] = p->data[j];                p->data[j] = tmp;            }        }    }    return;}
原创粉丝点击