菜鸟学习历程【18】通讯录项目

通信录项目实战

格式:

编号姓 名住址电话座机
201701xxxxxxxxx183****56688568***1

要求:使用结构体形式对数据存储

功能:使用链表实现增加(在增加人员的过程中有一个自动排序功能,比如按姓名排序)、删除、修改、查找(比如:工号查找、电话查找)的功能;
(1)添加用户信息(号码长度 号码是否重复)
(2)列出好友信息(按姓名排序)
(3)查找好友信息(按姓名查找)
(4)删除好友
(5)退出
(6)将数据保存在文档中,下次使用恢复数据
注意事项:在增、删、改、查过程中,如果姓名相同怎么进行选择操作。

分析:
1.我们使用链表来实现这个功能;
2.新增联系人时,我们可以按照名字的顺序进行插入,当然我的能力有限,目前只能写出英文名字;
3.我们以手机号码作为唯一标示,即允许同名、同座机、同工号的情况存在,当然也可以根据自己的要求进行修改;
4.当查找时,发现同名的,则全部打印;
5.删除时,如果发现有同名的,则将这些人的所有信息打印出来,并提示输入手机号进行删除;
6.修改信息时,提示选择修改的项目,此时要注意倘若修改手机号码,手机号码不能与联系人列表中已有的重复;

头文件:addressbook.h

#ifndef _ADDRESSBOOK_H_
#define _ADDRESSBOOK_H_
#define  SUCCESS           1000000001
#define  FAILURE           1000000002

struct addressbook
{
    char name[10];
    char id[20];
    char address[20];
    char tele[20];
    char mobile[20];
    struct addressbook *next;
};

typedef struct addressbook AddBook;
typedef AddBook  *Address;
void show();
void PrintInfo();
int AddressInit(Address *s); //初始化函数
int Change(Address s);  //修改内容函数
int Search(Address s);  //查找函数
int Delete(Address s);   //删除函数
int Show(Address s);  //打印所有信息
int SaveInfo(Address s);  //将信息保存在文档中
int AddInfo(Address *s);  //将文档信息加载到链表中

#endif

主函数:main.c

#include<stdio.h>
#include<stdlib.h>
#include"addressbook.h"

int main()
{
    Address address;
    int choice, ret;

    ret = AddressInit(&address);   //初始化链表
    if(SUCCESS == ret)
    {
        show();
    }

    AddInfo(&address);   //将文档信息加载到链表中

    while(1)
    {
        PrintInfo();
        scanf("%d",&choice);

        switch(choice)
        {
            case 1:
                Add(&address);
                break;
            case 2:
                Show(address);
                break;
            case 3:
                Search(address);
                break;
            case 4:
                Change(address);
                break;
            case 5:
                Delete(address);
                break;
            case 6:
                SaveInfo(address);
                break;
            default:
                printf("Unkown!\n");
        }

    }
    return 0;
}

接口函数:addressbook.c

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include <sys/types.h>
#include <unistd.h>
#include"addressbook.h"

int CountName(char *s, Address p)
{
    int count = 0;
    Address l = p;
    while(l->next)
    {
        if(strcmp((l->next)->name, s) == 0)
        {
            count++;
        }
            l = l->next;
    }
    return count;

}

int CountMobile(char *s, Address p)
{
    Address l = p->next;
    while(l)
    {
        if(strcmp(l->mobile, s) == 0)
        {
            return 1;
        }
        else
        {
            l = l->next;
        }
    }
    if(l == NULL)
    {
        return 0;
    }

}

int ChangeName(char *tar, Address *p)
{
    char new[10];
    Address s = (*p)->next;
    printf("Please input new name:\n\n");
    scanf("%s", new);
    while(s)
    {
        if(strcmp(tar, s->name) == 0)
        {
            strcpy(s->name, new);
            return 1;
        }
        s = s->next;
    }
}
int ChangeID(char *tar, Address *p)
{
    char newID[10];

    printf("Please input new ID:\n\n");
    scanf("%s", newID);
    Address s = (*p)->next;
    while(s)
    {
        if(strcmp(tar, s->name) == 0)
        {
            strcpy(s->id, newID);
            return 1;
        }
        s = s->next;
    }

}
int ChangeMobile(char *tar, Address *p)
{
    char newMob[12];
    printf("Please input new Mobile:\n\n");
    scanf("%s", newMob);
    Address s = (*p)->next;
    while(s)
    {
        if(strcmp(tar, s->name) == 0)
        {
            strcpy(s->mobile, newMob);
            return 1;
        }
        s = s->next;
    }

}

int ChangeTele(char *tar, Address *p)
{
    char newTEL[9];
    printf("Please input new Telephone:\n\n");
    scanf("%s", newTEL);
    Address s = (*p)->next;
    while(s)
    {
        if(strcmp(tar, s->name) == 0)
        {
            strcpy(s->tele, newTEL);
            return 1;
        }
        s = s->next;
    }

}
int ChangeAddress(char *tar, Address *p)
{
    char newadd[10];
    printf("Please input new address:\n\n");
    scanf("%s", newadd);
    Address s = (*p)->next;
    while(s)
    {
        if(strcmp(tar, s->name) == 0)
        {
            strcpy(s->address, newadd);
            return 1;
        }
        s = s->next;
    }

}
void show()
{
    printf("*******************************************************\n\n");
    printf("**                  ADDRESS BOOK                     **\n\n");
    printf("*******************************************************\n\n");
    sleep(1);
    system("clear");
}

void PrintInfo()
{
    printf("******************************************************\n");
    printf("**1.Add Information             2.Show Information  **\n");
    printf("**3.Search Information          4.Change Information**\n");
    printf("**5.Delete Information          6.Quit              **\n");
    printf("******************************************************\n");
    printf("Please input your choice:\n\n");
}

int AddressInit(Address *l)
{
    (*l) = (Address)malloc(sizeof(AddBook));
    if(NULL == (*l))
    {
        return FAILURE;
    }
    (*l)->next = NULL;
    return SUCCESS;     
}

int Add(Address *l)
{
    Address p = *l;
    Address s;

    printf("Please input information:(NAME ID MOBILE TELEPHONE ADDRESS)\n\n");

    s = (Address)malloc(sizeof(AddBook));
    scanf("%s%s%s%s%s",s->name,s->id,s->mobile,s->tele,s->address);

    if(strlen(s->mobile) != 11)  //手机号码不为11位
    {
        printf("The Mobile number is't correctly!\n\n");
        return 0;
    }
    if(strlen(s->tele) != 8)    //座机号码不为8位
    {
        printf("The Telephone number is't correctly!\n\n");
        return 0;
    }
       while(p->next)
    {
        if(strcmp((p->next)->mobile,s->mobile) == 0)  //判断是否已存在这个手机号码
        {

            printf("The Mobile number is existed!\nPlease check and input again!\n\n");
            return 0;
        }
        p = p->next;
    }

    //判断工作结束后,输入的数据满足要求,则增加到链表中去
    p=*l;
    if(p->next ==NULL)   //如果是空表,则直接增加
    {
        s->next = p->next;
        p->next = s;
    }
    else          //不为空表,则比较名字大小,找到该插入的位置
    {
        while(p->next)
        {
            if(strcmp((p->next)->name,s->name)>0)    
            {           
                s->next = p->next;
                p->next = s;
                break;
            }   
            p = p->next;
        }
        if(p->next == NULL)
        {
            s->next = p->next;
            p->next = s;
        }
    }
}

int Show(Address l)
{
    if(l->next == NULL)    
    {
        printf("The address book is Empty\n\n");
    }
    else
    {
        Address p = l->next;
        printf("NAME    ID        MOBILE          TELE        ADDRESS\n\n");
        while(p)
        {
            printf("%s    %s    %s      %s     %s\n\n", p->name, p->id, p->mobile, p->tele, p->address);
            p=p->next;
        }
    }
}

int Search(Address l)
{
    char target[10];
    int ret;
    if(l->next == NULL)
    {
        printf("There is no data!!!\n\n");
        return 0;
    }
    Address s = l;
    printf("Please input the name you want to search:\n\n");
    scanf("%s", target);

    ret = CountName(target, l);
    if(0 == ret)
    {
        printf("The person does not exist!\n\n");
    }
    else
    {
        printf("NAME    ID        MOBILE          TELE        ADDRESS\n\n");
        while(s->next)
        {

            if(strcmp(target,(s->next)->name) == 0)
            {
                printf("%s    %s    %s      %s     %s\n\n", (s->next)->name, (s->next)->id, (s->next)->mobile, (s->next)->tele, (s->next)->address);
            }
            s = s->next;
        }
    }
}

int Change(Address l)
{
    char target[10];
    char num[13];
    int choice, ret;
    printf("Please input the one you want to change:\n\n");
    scanf("%s",target);
    ret = CountName(target, l);
    if(0 == ret)
    {
        printf("The person does not exist!\n\n");
    }
    else if(1 == ret)
    {
        Address s = l->next;
        while(s)
        {
            if(strcmp(target,s->name) == 0)
            {
                printf("*************************************************\n");
                printf("**1.NAME                                2.ID   **\n");
                printf("**3.MOBILE                              4.TELE **\n");
                printf("**5.ADDRESS                                    **\n");
                printf("*************************************************\n");
                printf("Please choose which you want to change:\n\n");
                scanf("%d",&choice);
                switch(choice)
                {
                    case 1:
                        ChangeName(target, &l);
                        break;
                    case 2:
                        ChangeID(target, &l);
                        break;
                    case 3:
                        ChangeMobile(target, &l);
                        break;
                    case 4:
                        ChangeTele(target, &l);
                        break;
                    case 5:
                        ChangeAddress(target, &l);
                        break;
                    default:
                        printf("Unkown choice!");
                }
                break;
            }
            s = s->next;
        }
    }
    else
    {
        printf("Information about the person you wanna change:\n\n");
        Address s = l;
        while(s->next)
        {
            if(strcmp((s->next)->name, target) == 0)
            {
                printf("%s %s %s %s %s\n\n",(s->next)->name, (s->next)->id, (s->next)->mobile, (s->next)->tele, (s->next)->address);
            }
            s = s->next;
            if(s == NULL)
                break;
        }
        printf("Please input the Mobile of the person you wanna change:\n\n");
        scanf("%s", num);

         s = l->next;
        while(s)
        {
            if(strcmp(num ,s->mobile) == 0)
            {
                printf("*************************************************\n");
                printf("**1.NAME                                2.ID   **\n");
                printf("**3.MOBILE                              4.TELE **\n");
                printf("**5.ADDRESS                                    **\n");
                printf("*************************************************\n");
                printf("Please choose which you want to change:\n\n");
                scanf("%d",&choice);
                switch(choice)
                {
                    case 1:
                        ChangeName(target, &l);
                        break;
                    case 2:
                        ChangeID(target, &l);
                        break;
                    case 3:
                        ChangeMobile(target, &l);
                        break;
                    case 4:
                        ChangeTele(target, &l);
                        break;
                    case 5:
                        ChangeAddress(target, &l);
                        break;
                    default:
                        printf("Unkown choice!");
                }
                break;
            }
            s = s->next;
        }
    }

}

int Delete(Address l)
{
    char tar[10];
    char mobile[12];
    Address s = l;
    Address tmp;
    int ret;
    if(s->next == NULL)
    {
        printf("The Address Book is empty!\n\n");
    }
    printf("Please input the one you want to delete:\n\n");
    scanf("%s", tar);
    ret = CountName(tar, l);
    if(0 == ret)
    {
        printf("The person does not exist!\n\n");
        return;
    }
    if(1 == ret)
    {
        while(s->next)
        {
            if(strcmp(tar, (s->next)->name) == 0)
            {
                tmp = s->next;
                s->next = tmp->next;
                free(tmp);


                return 0;
            }
            else
            {
                s = s->next;
            }
        }
    }
    else
    {
        printf("Information about the person you wanna delete:\n\n");
        while(s->next)
        {
            if(strcmp((s->next)->name, tar) == 0)
            {
                printf("%s %s %s %s %s\n\n",(s->next)->name, (s->next)->id, (s->next)->mobile, (s->next)->tele, (s->next)->address);
            }
            s = s->next;
            if(s == NULL)
                break;
        }
        printf("Please input the mobile of the person you wanna delete:\n\n");
        scanf("%s", mobile);
        s = l;
        while(s->next)
        {
            if(strcmp(mobile, (s->next)->mobile) == 0)
            {
                tmp = s->next;
                s->next = tmp->next;
                free(tmp);

                return 0;
            }
            else
            {
                s = s->next;
            }
        }
    }
}

int SaveInfo(Address s)
{
    int  ret, i = 0;
    FILE *fp;
    Address l = s->next;
    Address tmp = (Address)malloc(sizeof(AddBook));

    fp = fopen("address.txt", "w");
    if(NULL == fp)
    {
        perror("fopen");
        exit(1);
    }

    while(l)
    {
        strcpy(tmp->id, l->id);
        strcpy(tmp->tele, l->tele);
        strcpy(tmp->mobile, l->mobile);
        strcpy(tmp->address, l->address);
        strcpy(tmp->name, l->name);

        ret = fwrite(tmp, 1, sizeof(AddBook), fp);

        if(0 == ret)
        {
            perror("write");
            exit(1);
        }

        l = l->next;
    }

    free(tmp);
    fclose(fp);
    exit(1);
}


int AddInfo(Address *s)
{
    Address p = *s;
    int  ret = 1;
    FILE *fp;

    fp = fopen("address.txt", "a+b");
    if(NULL == fp)
    {
        perror("fopen");
        return 0;
    }

    while(ret)
    {
        Address l = (Address)malloc(sizeof(AddBook));
        ret = fread(l, 1, sizeof(AddBook), fp);
        if(0 == ret)
        {
            return 0;
        }

        l->next = p->next;
        p->next = l;
        p = p->next;
    }

    fclose(fp);

}

对于代码,有任何看法的,可以提出,此文可随时修改。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值