C语言 升级版简单的通讯录 (可将联系人信息存储到电脑中)

写的不好,让您再次见笑了。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#define N 30

typedef struct link 
{
    char name[N];
    char sex;
    char num[N];
    char remark[N];
    struct link *next; 
}node, *z_node;

z_node creat()
{
    z_node p = (z_node)malloc(sizeof(node)) ;
    if (NULL == p)
    {
        printf("malloc failure!\n");
        return NULL;
    }
    p->next = NULL;
    return p;
}

void save_in(z_node L)
{
    z_node p = L->next;
    #if 0
    if (p == NULL)
    {
        printf("没有数据,无法存储\n");
        return;
    }
    #endif
    FILE *name_fp, *sex_fp, *num_fp, *remark_fp;  
    if ((name_fp = fopen("name.txt","w+")) == NULL)
    {
        printf("open failed\n");
        return;
    }
    if ((sex_fp = fopen("sex.txt","w+")) == NULL)
    {
        printf("open failed\n");
        return;
    }
    if ((num_fp = fopen("num.txt","w+")) == NULL)
    {
        printf("open failed\n");
        return;
    }
    if ((remark_fp = fopen("remark.txt","w+")) == NULL)
    {
        printf("open failed\n");
        return;
    }
    while (p != NULL)
    {
        fputs(p->name,name_fp);
        fputc('\n',name_fp);
        fputc(p->sex,sex_fp);
        fputs(p->num,num_fp);
        fputc('\n',num_fp);
        fputs(p->remark,remark_fp);
        fputc('\n',remark_fp);
        p = p->next;
    }
    fclose(name_fp);
    fclose(sex_fp);
    fclose(num_fp);
    fclose(remark_fp);
    return; 
}

void init_in(z_node L)
{
    FILE *name_fp, *sex_fp, *num_fp, *remark_fp;  
    if ((name_fp = fopen("name.txt","r+")) == NULL)
    {
        printf("open failed\n");
        return;
    }
    if ((sex_fp = fopen("sex.txt","r+")) == NULL)
    {
        printf("open failed\n");
        return;
    }
    if ((num_fp = fopen("num.txt","r+")) == NULL)
    {
        printf("open failed\n");
        return;
    }
    if ((remark_fp = fopen("remark.txt","r+")) == NULL)
    {
        printf("open failed\n");
        return;
    }
    int line = 0, i;
    char buf[N];
    z_node new;
    while (fgets(buf,N,name_fp) != NULL)
    {
        if (buf[strlen(buf)-1] == '\n') 
        line++;
    }
    fseek(name_fp,0,SEEK_SET); 
    #if 1
    for (i=0; i<line; i++)
    {
        new = creat();
        fgets(buf,N,name_fp);
        buf[strlen(buf)-1] = '\0';
        strcpy(new->name,buf);

        new->sex = fgetc(sex_fp);

        fgets(buf,N,num_fp);
        buf[strlen(buf)-1] = '\0';
        strcpy(new->num,buf);

        fgets(buf,N,remark_fp);
        buf[strlen(buf)-1] = '\0';
        strcpy(new->remark,buf);

        new->next = L->next;
        L->next = new;
    }
    fclose(name_fp);
    fclose(sex_fp);
    fclose(num_fp);
    fclose(remark_fp);
    #endif
    return; 
}

void init(z_node L)//初始化
{
    z_node new = creat();
    z_node new1 = creat();
    z_node new2 = creat();
    z_node new3 = creat();

    new->next = L->next; 
    L->next = new;
    strcpy(new->name,"jie");
    new->sex = 'm';
    strcpy(new->num,"111111");
    strcpy(new->remark,"yingliuzhizhu");

    new1->next = L->next; 
    L->next = new1;
    strcpy(new1->name,"feizi");
    new1->sex = 'f';
    strcpy(new1->num,"222222");
    strcpy(new1->remark,"chaoxihailing");

    new2->next = L->next; 
    L->next = new2;
    strcpy(new2->name,"kate");
    new2->sex = 'f';
    strcpy(new2->num,"333333");
    strcpy(new2->remark,"buxiangzhiren");

    new3->next = L->next; 
    L->next = new3;
    strcpy(new3->name,"tailong");
    new3->sex = 'm';
    strcpy(new3->num,"666666");
    strcpy(new3->remark,"daofengzhiying");
}

void outputall(z_node L)//输出所有
{
    z_node p = L->next;
    while ( p != NULL )
    {
        printf("------------------------\n");
        printf("姓名:%s\n",p->name);
        if ('m' == p->sex) 
        printf("性别:男\n");
        else
        printf("性别:女\n");
        printf("电话:%s\n",p->num);
        printf("备注:%s\n",p->remark);
        p = p->next;
        printf("------------------------\n");
    }
    return;
}

void cleanall(z_node L)//万念俱灰,删除所有
{
    z_node p = L->next, q;
    if (NULL == p)
    {
        printf("你的通讯录已经是空的了。。\n");
        return;
    }
    L->next = NULL; 
    q = p->next;
    while (q != NULL)  
    {
        free(p);
        p = q;
        q = q->next;
    }
    printf("全都删除完了\n");
    return;
}

void insert(z_node L)//添加联系人
{
    z_node new = creat();

    new->next = L->next; 
    L->next = new;
    printf("请输入姓名:");
    gets(new->name);
    printf("请输入性别字母m(男)或者f(女):");
    do{
        scanf("%c",&new->sex);
        getchar();
        if (new->sex!='m' && new->sex!='f' )
        printf("你朋友还有从泰国回来的吗,m or f,重选一个\n");
    }while ('m' != new->sex && 'f' != new->sex);
    printf("请输入电话号码:");
    gets(new->num);
    printf("请输入备注:");
    gets(new->remark);

    return;
}

z_node location_byname(z_node L,char look_for[N])//通过名字定位
{
    z_node p = L;
    while ( p->next != NULL )
    {
        if ( strcmp(look_for,p->next->name) == 0 )
        return p;
        p = p->next; 
    }
    printf("没有叫这个名字的人啊大佬,重新输一个:\n");
    return p;
}

z_node location_bynum(z_node L,char look_for[N])//通过号码定位
{
    z_node p = L;
    while ( p->next != NULL )
    {
        if ( strcmp(look_for,p->next->num) == 0 )
        return p;
        p = p->next; 
    }
    printf("哪有这个号码,0.0,重新输一个:\n");
    return p;

}

void seek(z_node L)//按姓名或者号码查找联系人
{
    int s; 
    z_node p;
    char look_for[N];
    printf("请选择:\n");
    printf("1.按姓名查找\n");
    printf("2.按电话号码查找\n");
    do{
        scanf("%d",&s);
        getchar();
        if (s!=1 && s!=2 )
        printf("没有这个选项啊老大,重新输一个\n");
    }while (1 != s && 2 != s);

    if (1 == s)
    {
        printf("他/她叫啥啊:\n");
        do{
            gets(look_for);
            p = location_byname(L,look_for);
        }while (NULL == p->next);
                printf("------------------------\n");
                printf("姓名:%s\n",p->next->name);
        if ('m' == p->next->sex)
                printf("性别:男\n");
        else
                printf("性别:女\n");
                printf("电话:%s\n",p->next->num);
                printf("备注:%s\n",p->next->remark);
                printf("------------------------\n");
    }

    if (2 == s)
    {
        printf("他/她的电话号码多少:\n");
        do{
            gets(look_for);
            p = location_bynum(L,look_for);
        }while (NULL == p->next);
        p = location_bynum(L,look_for);
                printf("------------------------\n");
                printf("姓名:%s\n",p->next->name);
        if ('m' == p->next->sex)
                printf("性别:男\n");
        else
                printf("性别:女\n");
                printf("电话:%s\n",p->next->num);
                printf("备注:%s\n",p->next->remark);
                printf("------------------------\n");
    }
    return;
}

void delete(z_node L)//按姓名或者号码删除联系人
{
    int s; 
    z_node p, q;
    char look_for[N];
    printf("请选择:\n");
    printf("1.按姓名删除\n");
    printf("2.按电话号码删除\n");
    do{
        scanf("%d",&s);
        getchar();
        if (s!=1 && s!=2 )
        printf("没有这个选项啊老大,重新输一个\n");
    }while (1 != s && 2 != s);

    if (1 == s)
    {
        printf("他/她叫啥啊:\n");
        do{
            gets(look_for);
            p = location_byname(L,look_for);
        }while (NULL == p->next);
        q = p->next;
        p->next = q->next;
        free(q);
        printf("删好了,厉害吧\n");
    }

    if (2 == s)
    {
        printf("他/她的电话号码多少:\n");
        do{
            gets(look_for);
            p = location_bynum(L,look_for);
        }while (NULL == p->next);
        p = location_bynum(L,look_for);
        q = p->next;
        p->next = q->next;
        free(q);
        printf("删好了,厉害吧\n");
    }
    return;

}

void output_bysex(z_node L)//按性别输出所有联系人
{
    #if 1
    int s; 
    z_node p;
    //printf("请选择:1(男) or 2(女)\n");
    printf("请选择:\n");
    printf("1.输出所有喜欢你的男士\n");
    printf("2.输出所有喜欢你的女士\n");
    do{
        scanf("%d",&s);
        getchar();
        if (s!=1 && s!=2 )
        printf("没有这个选项啊老大,重新输一个\n");
    }while (1 != s && 2 != s);

    if (1 == s)
    {
        p = L->next;
        while (p != NULL)    
        {
            if (p->sex == 'm')
            {
                printf("------------------------\n");
                printf("姓名:%s\n",p->name);
                printf("性别:男\n");
                printf("电话:%s\n",p->num);
                printf("备注:%s\n",p->remark);
                printf("------------------------\n");
            }
            p = p->next;
        }
    }

    if (2 == s)
    {
        p = L->next;
        while (p != NULL)    
        {
            if (p->sex == 'f')
            {
                printf("------------------------\n");
                printf("姓名:%s\n",p->name);
                printf("性别:女\n");
                printf("电话:%s\n",p->num);
                printf("备注:%s\n",p->remark);
                printf("------------------------\n");
            }
            p = p->next;
        }
    }
    #endif
    return;
}

void menu(z_node L)//菜单
{
    int i, j = 0;
    while ( j<1 )
    {
        system("clear");
        printf("********************************\n");
        printf("欢迎使用,请输入以下编号:\n");
        printf("1:输出通讯录全部信息\n");
        printf("2:清空通讯录全部信息\n");
        printf("3:插入新的联系人\n");
        printf("4:查找联系人\n");
        printf("5:删除联系人\n");
        printf("6:按性别输出联系人\n");
        printf("0:退出\n");
        printf("********************************\n");
        scanf("%d",&i);
        getchar();
        switch (i)
        {
            case 1:
            outputall(L);
            break;
            case 2:
            cleanall(L);
            break;
            case 3:
            insert(L);
            break;
            case 4:
            seek(L);
            break;
            case 5: 
            delete(L);
            break;
            case 6: 
            output_bysex(L);
            break;
            case 0:
            j = 1;
            break;
            default:
            printf("没有这个选项啊老大,重新输一个\n");
        }
        if (j != 1)
        {
            printf("按Enter键继续\n");
            getchar();
        }
        else
        printf("再见,拜拜-0-,欢迎下次再来\n");
    }
}

void freeall(z_node L)
{
    z_node p = L->next;
    free(L);
    z_node q;
    while (p)
    {
        q = p->next;
        free(p);
        p = q;
    }
    return; 
}

int main(int argc, char *argv[])
{
    z_node L = creat();
    init_in(L);
    //init(L);
    menu(L);
    save_in(L);
    freeall(L);

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值