C/C++动态实现通讯录

版权声明: https://blog.csdn.net/qq_41880190/article/details/80317438
通讯录代码:
#define _CRT_SECURE_NO_WARNINGS 1

#define NAME_MAX 32  
#define SEX_MAX 32  
#define TEL_MAX 32  
#define ADDER_MAX 32  
#define PEOPLE_MAX 32  

#include<stdio.h>  
#include<stdlib.h>  
#include<string.h>  
#include<windows.h>

typedef struct people    
{  
    char name[NAME_MAX];  
    char sex[SEX_MAX];  
    int age;  
    char tel[TEL_MAX];  
    char adder[ADDER_MAX];  
}people;  
typedef struct contact    
{  
    int capacity;  
    people *human;  
    int count_p;  
}Con, *Pcon;  

void menu()  
{  
	printf("******************************************************\n");
	printf("******************************************************\n");
    printf("*****************    欢迎使用通讯录      *************\n");  
    printf("*****************  1.添加联系人信息      *************\n");  
    printf("*****************  2.删除指定联系人信息  *************\n");  
    printf("*****************  3.查找指定联系人信息  *************\n");  
    printf("*****************  4.修改指定联系人信息  *************\n");  
    printf("*****************  5.显示所有联系人信息  *************\n");  
    printf("*****************  6.清空所有联系人      *************\n");  
    printf("*****************  7.以名字排序所有联系人*************\n");  
    printf("*****************  0.exit                *************\n");  
    printf("******************************************************\n");
	printf("******************************************************\n");
}  
void print(Pcon pcon, int i)    
{  
    printf("|  姓名  |  性别  |  年龄  |    电话    |      住址     | \n");  
    printf("|  %4s  |  %2s  |  %d  |  %6s  |  %6s  |\n", pcon->human[i].name, pcon->human[i].sex, pcon->human[i].age,  pcon->human[i].tel, pcon->human[i].adder);
}    
void add_people(Pcon pcon)  
{  
    people *p;  
    if (pcon->count_p == pcon->capacity)
    {  
		p = (people*)realloc(pcon->human, ((pcon->capacity) + 1)*sizeof(people));   
        if (p == NULL)  
            printf("out of memery");  
        else  
            pcon->human = p;  
    } 
	printf("请输入添加联系人的姓名,性别,年龄,电话,住址:\n");  
	scanf("%s", pcon->human[pcon->count_p].name);  
	scanf("%s", pcon->human[pcon->count_p].sex);  
	scanf("%d", &pcon->human[pcon->count_p].age);  
	scanf("%s", pcon->human[pcon->count_p].tel);  
	scanf("%s", pcon->human[pcon->count_p].adder);  
	print(pcon, pcon->count_p);  
	(pcon->count_p)++;  
	printf("添加成功!\n");  
}  
int find_people(Pcon pcon)  
{  
    char name[NAME_MAX];  
    int i = 0;  
    printf("请输入姓名:");  
    scanf("%s", name);  
    for (i = 0; i < pcon->count_p; i++)  
    {  
        if (strcmp(pcon->human[i].name, name) == 0)  
        {  
            print(pcon, i);  
            return i;   
        }  
    }  
    printf("不存在该联系人\n");  
    return -1;  
}  
void delete_people(Pcon pcon)  
{  
    int i = 0;  
    int ret = find_people(pcon);  
    if (ret != -1)  
    {  
        for (i = ret; i < pcon->count_p; i++)  
        {  
            pcon->human[i] = pcon->human[i + 1];   
        }  
        pcon->count_p--;  
        printf("删除成功!\n");  
    }  
    else  
        printf("不存在该联系人!\n");  
}  
void alter_people(Pcon pcon)  
{  
    int ret = find_people(pcon);   
    if (ret != -1)  
    {  
        printf("请重新输入该联系人信息:\n");  
        scanf("%s", pcon->human[ret].name);  
        scanf("%s", pcon->human[ret].sex);  
        scanf("%d", &pcon->human[ret].age);  
        scanf("%s", pcon->human[ret].tel);  
        scanf("%s", pcon->human[ret].adder);  
        print(pcon, ret);  
        printf("修改成功!\n");  
    }  
}  
void show_people(Pcon pcon)  
{  
    int i = 0;  
    for (i = 0; i < pcon->count_p; i++)  
    {  
        print(pcon, i);  
    }  
}  
void clean_people(Pcon pcon)   
{  
    pcon->count_p = 0;  
    printf("成功清空!");  
}  
void order_people(Pcon pcon)  
{  
    int i = 0;  
    for (i = 0; i < (pcon->count_p) - 1; i++)   
    {  
		int j = 0;
        for (j = 0; j<(pcon->count_p) - 1 - i; j++)  
        {  
            int min = 0;  
            if (strcmp(pcon->human[j].name, pcon->human[j + 1].name)>0)  
            {  
                people temp = pcon->human[j];  
                pcon->human[j] = pcon->human[j + 1];  
                pcon->human[j + 1] = temp;  
            }  
        }  
    }  
    printf("排序完成\n");  
    show_people(pcon);  
}   
void inittel(Pcon pcon)  
{  
	struct people  *p;
    pcon->capacity = PEOPLE_MAX;  
    p = (people *)malloc((pcon->capacity)*sizeof(people));    
    if (p == NULL)  
    {  
        printf("out of memery");  
    }  
    else  
        pcon->human = p;  
    pcon->count_p = 0;  
}    
void free_mem(Pcon pcon)       
{  
    free(pcon->human);  
    pcon->human = NULL;  
}
int main()  
{  
    Con con;
	int input = 0; 
    inittel(&con);   
    menu();  
    while (1)  
    {  
        printf("请选择:");  
        scanf("%d", &input);  
        switch (input)  
        {  
        case 1:  
            add_people(&con);  
            break;  
        case 2:  
            delete_people(&con);  
            break;  
        case 3:  
            find_people(&con);  
            break;  
        case 4:  
            alter_people(&con);  
            break;  
        case 5:  
            show_people(&con);  
            break;  
        case 6:  
            clean_people(&con);  
            break;  
        case 7:  
            order_people(&con);  
            break;  
        case 0: 
			return 0;  
        default:  
            printf("指令输入错误!");  
            break;  
        }  
    }  
    free_mem(&con);  
    system("pause");  
    return 0;  
}
阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页