C语言单向链表写一个简单的通讯录
标签搜索

C语言单向链表写一个简单的通讯录

Leo
Leo
2020-11-19 / 0 评论 / 431 阅读 / 正在检测是否收录...

话不多说,直接贴代码
telbook.c

/****************************************************
    > File Name: tel_book.c
    > Author: Leo Chin
****************************************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define TelBook_FILE "tel_book.txt" //通讯录

typedef struct S_TelBook
{
    char name[16];//姓名
    char mail[32];//邮箱
    char tel[16];;//电话号码
    struct S_TelBook* next;//下一个联系人

}T_S_TelBook;
T_S_TelBook *telbook_head;//创建链表头结点

void showMenu(void)
{
    printf(    "******************************************\n"
            "          - 1.显示所有联系人\n"
            "          - 2.增加联系人\n"
               "          - 3.删除联系人\n"
            "          - 4.修改联系人\n"
            "          - 5.搜索联系人\n"
            "          - 6.退出电话本\n"
            "******************************************\n");
}


void load_tel_list(void)
{
    //*******************loading static T_S_TelBook *
    telbook_head = (T_S_TelBook*)malloc(sizeof(T_S_TelBook));
    telbook_head->next = NULL;
    T_S_TelBook *now = telbook_head;
    FILE *fp = fopen(TelBook_FILE,"a+");
    if (NULL == fp)
    {
        printf("fopen file: %s fail !\n",TelBook_FILE);
    }
    //fp不为空则循环
    int i = 1;
    while(!feof(fp)) 
    {
        fscanf(fp,"%s %s %s",now->name,now->mail,now->tel);
        T_S_TelBook *telbook_body = (T_S_TelBook*)malloc(sizeof(T_S_TelBook));
        telbook_body->next = NULL;
        now->next = telbook_body;
        now = telbook_body;
        i++;
    }
    printf("已载入 [ %d ] 个联系人\n",i);
    //free(now);
    // printf(" !feof(fp) = %d\n", !feof(fp));
    fclose(fp);
}

//显示所有联系人
void show_all_list(void)
{
    //*******************print
    puts("所有联系人:");
    T_S_TelBook *now = telbook_head;
    printf("%-16s %-20s %-16s\n","NAME","EMAIL","TEL_Number");
    printf("----------------------------------------------------\n");
    while(now != NULL)
    {
        printf("%-16s %-20s %-16s\n",now->name,now->mail,now->tel);
        now = now-> next;
    }
}

void search_tel_list(void)
{
    //*******************search
    puts("搜索联系人:");
    char name_find[16] = {0};
    T_S_TelBook *now = telbook_head;
    printf("pls entel name to search:");
    scanf("%s",name_find);
    printf("name is:%s  searching. . .\n",name_find);
    while(now != NULL)
    {
        if (!strcmp(name_find ,now->name))
        {
            printf("%-16s %-20s %-16s\n","NAME","EMAIL","TEL_Number");
            printf("----------------------------------------------------\n");
            printf("%-16s %-20s %-16s\n",now->name,now->mail,now->tel);
            return;
        }
        now = now-> next;
    }
    printf( "Not find the name: %s\n",name_find);
}

void rewrite_file(void)
{
    FILE *fp = fopen(TelBook_FILE,"w");
    T_S_TelBook *now = telbook_head;
    if (NULL == fp)
    {
        printf("rewrite_file,fopen file: %s fail !\n",TelBook_FILE);
    }
    while( now != NULL ) 
    {
        fprintf(fp, "%s %s %s\n",now->name,now->mail,now->tel);
        now = now->next;
    }
    fclose(fp);
}

void add_tel_list(void)
{
    //*******************add_tel_list
    puts("输入格式为【 Tim 123456@qq.ocm 56019587 】");
    T_S_TelBook *telbook_insert = (T_S_TelBook*)malloc(sizeof(T_S_TelBook));
    telbook_insert->next = telbook_head;
    while(1)
    {
        scanf("%s %s %s",telbook_insert->name,telbook_insert->mail,telbook_insert->tel);
        if ((strlen(telbook_insert->name)>0) || (strlen(telbook_insert->mail)>0) || (strlen(telbook_insert->tel)>0))
        {
            break;
        }
        printf("entel error,pls again !\n");
    }
    telbook_head = telbook_insert;
    rewrite_file();
}

void delete_tel_list(void)
{
    puts("删除联系人:");
    char name_find[16] = {0};
    T_S_TelBook *now = telbook_head;
    T_S_TelBook *next_now = telbook_head->next;
    printf("pls entel name to delete:");
    scanf("%s",name_find);
    printf("name is:%s  deleteing. . .\n",name_find);
    if (now!=NULL && !strcmp(name_find ,now->name))
    {
        telbook_head = telbook_head->next;
        free(now);
        rewrite_file();
        return;
    }
    while(next_now != NULL)
    {
           // printf("name_find:%s\n",name_find);
        // printf("now->name:%s\n",next_now->name);
        if (!strcmp(name_find ,next_now->name))
        {
            now->next = next_now->next;
            free(next_now);
            rewrite_file();
            return;
        }
        now = now->next;
        next_now = next_now->next;
    }
    printf( "Not find the name: %s",name_find);
}
void modify_tel_list(void)
{
    puts("修改联系人:");
    char name_find[16] = {0};
    T_S_TelBook *now = telbook_head;
    printf("Please enter the name you want to modify:");
    scanf("%s",name_find);
    while(now != NULL)
    {
           // printf("name_find:%s\n",name_find);
        // printf("now->name:%s\n",now->name);
        if (!strcmp(name_find ,now->name))
        {
            while(1)
            {
                printf("Please enter the changed infor:\n");
                puts("输入格式为【 Tim 123456@qq.ocm 56019587 】");
                scanf("%s %s %s",now->name,now->mail,now->tel);
                //对输入的格式没有强制控制可以自己增加
                if ((strlen(now->name)>0) || (strlen(now->mail)>0) || (strlen(now->tel)>0))
                {
                    break;
                }
                printf("entel error,pls again !\n");
            }
            printf("modify name is:%s  modifying. . .\n",name_find);
            rewrite_file();
            return;
        }
        now = now->next;
    }
    printf( "Not find the name: %s",name_find);

}

void cmd_menu(void)
{
    int cmd_menu;
    scanf("%d",&cmd_menu);
    switch(cmd_menu)
    {
        case 1:show_all_list();break;
        case 2:add_tel_list();break;
        case 3:delete_tel_list();break;
        case 4:modify_tel_list();break;
        case 5:search_tel_list();break;
        case 6:puts("退出中. . .");exit(0);
        default:printf("Cmd not fould,pls enter again: ");break;
    }
}
void main(void)
{
    printf(    "*** 载入中,请稍后 .  . . . . . . . .  ***\n");
    load_tel_list();
    printf(    "*** 欢迎使用电话本,请选择:[输入序号] ***\n");
    while(1)
    {
        showMenu();
        printf("pls enter : ");
        cmd_menu();
        // printf(    "continue ? \n");
    }
}

BOOK File: tel_book.txt 文件格式

456123 123654 123456
new 123456 621621
kkkk 123@qq.com 15915918888
jake 123@haha.com 159159519
leoc 137056@q.com 123456
asdasd 13546 123123
kkkk 123@qq.com 15915918888
leochin1 137123456@qq.com 1370561111
leochin2 137123457@qq.com 1370562222
leochin3 137123458@qq.com 1370563333
leochin4 137123459@qq.com 1370564444
leochin5 137123460@qq.com 1370565555
leochin6 137123461@qq.com 1370566666
leochin7 137123462@qq.com 1370566677
0

评论 (0)

取消