通讯录小项目(实现增删改查)

main.c文件

#define _CRT_SECURE_NO_WARNINGS 1
#include "contact.h"


void test(SUM* pc)
{
    /*char arr[] = "222";
    change(pc,arr);
    print(pc);*/
    int i = 0;
    char arr[name_MAX];
    printf("请输入指令:");
    while (1)
    {
        scanf("%d", &i);
        switch (i)
        {
        case ADD_1:
            ADD(pc);
            printf("请再次输入指令:");
            break;
        case delet_2:
            printf("请输入要删除的联系人姓名:");
            scanf("%s", arr);
            delet(pc, arr);
            printf("请再次输入指令:");
            break;
        case change_3:
            printf("请输入要修改的联系人姓名:");
            scanf("%s", arr);
            change(pc, arr);
            printf("请再次输入指令:");
            break;
        case search_4:
            printf("请输入要搜索的联系人姓名:");
            scanf("%s", arr);
            print_one(pc, search(pc, arr));
            printf("请再次输入指令:");
            break;
        case print_5:
            print(pc);
            printf("请再次输入指令:");
            break;
        case print_one_6:
            printf("请输入要显示的联系人姓名:");
            scanf("%s", arr);
            print_one(pc, search(pc, arr));
            printf("请再次输入指令:");
            break;
        default:
            printf("输入有误!请重新输入:");
            break;
        }
    }
    
    return;
}

int main()
{
    //结构体初始化
    SUM Data = {
        .people = {{"111","444","777"},{"222","555","888"},{"333","666","999"}},
        .size = 3
    };

    test(&Data);
    return 0;
}

contact.c文件

#define _CRT_SECURE_NO_WARNINGS 1

#include "contact.h"

//增加联系人
void ADD(SUM* const pc)
{
    
    printf("请输入姓名:");
    scanf("%s", &(pc->people[pc->size].name));
    printf("请输入单位:");
    scanf("%s", &(pc->people[pc->size].danwei));
    printf("请输入号码:");
    scanf("%s", &(pc->people[pc->size].tele));
    (pc->size)++;
    return;
}

//搜索联系人,根据姓名查找,返回对应的下标
int search(SUM* const pc, const char * arr)
{
    for (int i = 0;isize;i++)
    {
        if (strcmp(((pc->people)[i].name), arr) == 0)
        {
            return i;   //找到了,返回对应下标
        }
    }
    return -1;  //-1表示没找到
}

//删除联系人
void delet(SUM* const pc, const char *arr)
{
    int a = search(pc,arr);
    for (a;a<(pc->size-1);a++)
    {
        pc->people[a] = pc->people[a + 1];
    }
    (pc->size)--;
    return;
}

//打印联系人
void print(SUM* const pc)
{
    for (int i = 0; i < pc->size; i++)
    {
        printf("%s    ", (pc->people)[i].name);
        printf("%s    ", (pc->people)[i].danwei);
        printf("%s    ", (pc->people)[i].tele);
        printf("\n");
    }
    return;
}

//打印某一个联系人
void print_one(SUM* const pc, const int i)
{
    
    printf("%s    ", (pc->people)[i].name);
    printf("%s    ", (pc->people)[i].danwei);
    printf("%s    ", (pc->people)[i].tele);
    printf("\n");
    return;
}

//改
void change(SUM* const pc, const char* arr)
{
    int a = search(pc, arr);
    printf("请输入姓名:");
    scanf("%s",pc->people[a].name);
    printf("请输入单位:");
    scanf("%s", pc->people[a].danwei);
    printf("请输入号码:");
    scanf("%s", pc->people[a].tele);
    return;
}

contact.h文件

#pragma once
#include 
#include 


#define name_MAX 20
#define danwei_MAX 20
#define tele_MAX 12
#define people_MAX 100


#define ADD_1 1
#define delet_2 2
#define change_3 3
#define search_4 4
#define print_5 5
#define print_one_6 6

typedef struct Contact
{
    char name[name_MAX];
    char danwei[danwei_MAX];
    char tele[tele_MAX];

}Contact;

typedef struct SUM
{
    Contact people[people_MAX];
    int size;
}SUM;


void ADD(SUM* const pc);
void delet(SUM* const pc, const char* arr);
void change(SUM* const pc, const char* arr);
int search(SUM* const pc, const char* arr);
void print(SUM* const pc);
void print_one(SUM* const pc, const int i);

你可能感兴趣的:(c)