2.实现一个通讯录; 通讯录可以用来存储1000个人的信息,每个人的信息包括: 姓名、性别、年龄、电话、住址 提供方法: 1. 添加联系人信息 2. 删除指定联系人信息 3. 查找指定联

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/DuckyLoser/article/details/79688865
实现一个通讯录
  通讯录可以用来存储1000个人的信息,每个人的信息包括:  姓名、性别、年龄、电话、住址   提供方法: 
1.添加联系人信息 
2.删除指定联系人信息  
3.查找指定联系人
4.修改联系人信息
5.显示所有联系人信息 
6. 清空所有联系人 

7. 以名字排序所有联系人

Address.h
#include<stdio.h>
#include<string.h>
#include<assert.h>
#include<stdlib.h>
#pragma once

#define NameMax 20
#define SexMax 3
#define TelMax 11
#define AddressMax 20
#define PeopleMax 1000

typedef char Persontype;

typedef struct PERSON
{
        Persontype Name[NameMax];//  姓名
        Persontype Sex[SexMax];  // 性别
        int Age;          // 年龄
        Persontype Tel[TelMax]; //联系方式
        Persontype Address[AddressMax]; //地址

}Person;

typedef struct BOOK
{
        Person Data[PeopleMax];//Address_Book结构体最大存储容量
        int count;    //当前已存储人数
}Book;

void Initbook(Book* people); //初始化

void show_person(Book* people);//打印

void Add_person(Book *people);// 增加成员

void Del_person(Book* people);//删除成员

int Find_person(Book* people);//查找成员

void Change_person(Book* people);//修改成员

void BubbleSort_person(Book* people);//冒泡排序

Address.c

#include<stdio.h>
#include"Address.h"


//初始化
void Initbook(Book* people)
{
    people->count = 0;
}

//打印
void show_person(Book* people)
{
    if(people == NULL)
    {
        printf("空通讯录!\n");
        return;
    }
    if(people->count == 0)
    {
        printf("空通讯录!\n");
    }
    int i = 0;
    for(;i <=people->count-1;++i)
    {
        printf("Name:%s\n",people->Data[i].Name);
        printf("Sex:%s\n",people->Data[i].Sex);
        printf("Age:%d\n",people->Data[i].Age);
        printf("Tel:%s\n",people->Data[i].Tel);
        printf("Add:%s\n",people->Data[i].Address);
    }
    printf("\n");
}


//增加成员
void Add_person(Book *people)
{
    assert(people);
    int i = 0;
    if (people->count > PeopleMax)
    {
        printf("通讯录已满!\n");
        return;
    }
    printf("请输入姓名:\n");
    scanf("%s", (people->Data[people->count]).Name);
    printf("请输入性别:\n");
    scanf("%s", (people->Data[people->count]).Sex);
    printf("请输入年龄:\n");
    scanf("%d", &(people->Data[people->count]).Age);
    printf("请输入联系方式:\n");
    scanf("%s", (people->Data[people->count]).Tel);
    //for(;i<people->count;++i)
    //{
    //    if(strcmp(people->Data[i].Tel, people->Data[people->count].Tel) == 0)
    //    {
    //        printf("输入有误或联系人已存在!\n");
    //    }
    //    return;
    //}
    printf("请输入住址:\n");
    scanf("%s", (people->Data[people->count]).Address);
    printf("增加成功!\n");
    people->count++;
    printf("count = %d\n",people->count);
    
}
//删除成员
void Del_person(Book* people)
{
    if(people == NULL)
    {
        printf("空通讯录!\n");
        return;
    }
    int ret = Find_person(people);
    if(ret != -1)
    {
        int i = ret;
        for(;i<people->count-1;++i)
        {
            people->Data[i] = people->Data[i+1];
        }
        people->count--;
      printf("count = %d\n",people->count);
    }
    else
    {
        printf("没有该成员!\n");
    }
}

//查找成员
int Find_person(Book* people)
{
    assert(people);
    char name[20];
    scanf("%s",name);
    int i = 0;
    for(;i<people->count;++i)
    {
        if(strcmp(people->Data[i].Name,name) == 0)
        {
            printf("Name:%s\tSex:%s\tAge:%d\tTel:%s\tAdd\n",people->Data[i].Name,people->Data[i].Sex,people->Data[i].Age,people->Data[i].Tel,people->Data[i].Address);
            return i;
            printf("下标为 %d\n",i);
        }
    }
    printf("没有找到该联系人!\n");
    return -1;
}

//修改成员
void Change_person(Book* people)
{
    assert(people);
    int ret = Find_person(people);
    if(ret != -1)
    {
        printf("Name->:\n");
        scanf("%s",people->Data[ret].Name);
        printf("Sex->:\n");
        scanf("%s",people->Data[ret].Sex);
        printf("Age->:\n");
        scanf("%d",&(people->Data[ret]).Age);
        printf("Tel->:\n");
        scanf("%s",people->Data[ret].Tel);
        printf("Add->:\n");
        scanf("%s",people->Data[ret].Address);
    }
    else
    {
        printf("没有该成员!\n");
        return;
    }
}

void BubbleSort_person(Book* people)
{
    if(people == NULL)
    {
        printf("空通讯录!\n");
        return;
    }
    int i = 0;
    for(;i<people->count-1;++i)
    {
        int j = 0;
        for(;j<people->count-1-i;++j)
        {
            if(strcmp(people->Data[j].Name,people->Data[j+1].Name) > 0)
            {
                char tmp[NameMax];
                strcpy(tmp,people->Data[j].Name);
                strcpy(people->Data[j].Name,people->Data[j+1].Name);
                strcpy(people->Data[j+1].Name,tmp);
            }
        }
    }
    show_person(people);
}

void menu()
{
    printf("*************通讯录**********************\n"); 
    printf("*********1  增加成员*********************\n");
    printf("*********2  删除成员*********************\n");
    printf("*********3  查找成员*********************\n");
    printf("*********4  修改成员*********************\n");
    printf("*********5  显示成员*********************\n");
    printf("*********6  清空成员*********************\n");
    printf("*********7  排序*********************\n");
    printf("*********8  退出*********************\n");
}

int main()
{
    Book people;
    Initbook(&people);
    while(1)
    {
        menu();
        int n = 0;
        scanf("%d",&n);
        switch(n)
        {
            case 1:
                Add_person(&people);
                break;
            case 2:
                Del_person(&people);
                break;
            case 3:
                Find_person(&people);
                break;
            case 4:
                Change_person(&people);
                break;
            case 5:
                show_person(&people);
                break;
            case 6:
                Initbook(&people);
                break;
            case 7:
                BubbleSort_person(&people);
                break;
            case 8:
                exit(0);
            default:
                {
                    printf("input error!\n");
                    break;
                }
         }
     }
    return 0;
}

Makefile
Address:Address.o
	gcc -o $@ $^

.c.o:
	gcc -c $<

.PHONY:clean
clean:
	rm *.o Address

阅读更多

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