c语言通讯录项目

通讯录说明文档(一)
语言:c语言

格式: 编号 姓 名 住址 电话 住宅电话
201701 * xxxx 183****5668 33****77

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

功能:使用链表实现增加(在增加人员的过程中有一个自动排序功能,比如按姓名排序)、删除、修改、查找(比如:工号查找、电话查找)的功能;
(1)添加用户信息(号码长度 号码是否重复)
(2)列出好友信息(按姓名排序)
(3)查找好友信息(按姓名查找)
(4)删除好友
(5)退出(保存在文件中)

注意事项:在增、删、改、查过程中,如果姓名相同怎么进行选择操作。

头文件1.h

#ifndef a
#define a

#define SUCCESS 10000
#define FAILURE 10001

//typedef char Elemtype;

struct node
{
    char id[20];
    char name[20];
    char address[20];
    char phone[20];
    char homephone[20];
    struct node *next;
};

typedef struct node Node;
typedef Node *LinkList;

int ListInit(LinkList *L);
int ListInsert(LinkList *L);
int ListTraverse(LinkList L);

#endif

主函数

#include <stdio.h>
#include "1.h"
#include <stdlib.h>
#include <string.h>

int main()
{
    int i,j,ret;
    LinkList L,p;

    ListInit(&L);
    ListInit(&p);

    FILE *fp;
    fp=fopen("infor.txt","r");

    L=p;

    while(fp!=NULL)
    {
        while(1)
        {
            LinkList n=(LinkList)malloc(sizeof(Node));

            ret=fread(n,sizeof(Node),1,fp);
            if(ret == 0)
            {
                break;
            }
            n->next=p->next;
            p->next=n;
        }
        break;
    }

    while(1)
    {
        printf("\n\n\n");
        printf("             *********************\n"
               "             ****1.Insert  *******\n"
               "             ****2.Show    *******\n"
               "             ****3.Search  *******\n"
               "             ****4.Delete  *******\n"
               "             ****5.Change  *******\n"
               "             ****6.Quit    *******\n"
               "             *********************\n");
        printf("\n\n\n");   
        printf("please input you choice:\n");
            scanf("%d",&i);

            switch(i)
            {
                case 1:ListInsert(&L);
                       break;
                case 2:ListTraverse(L);
                       break;
                case 3:ListSearch(L);
                       break;
                case 4:ListDel(L);
                       break;
                case 5:ListChange(&L);
                       break;

                case 6:
                        fp=fopen("infor.txt","w");

                        LinkList n=(LinkList)malloc(sizeof(Node));

                        while(L->next != NULL)
                        {
                            n=L->next;
                            fwrite(n,sizeof(Node),1,fp);
                            L=L->next;
                        }

                       :wqclose(fp);
                       exit(1);
                       break;
                default:
                       printf("Unkown Input!Please Input Again:\n");
                       scanf("%d",&i);
                       break;
            }
    }

    return 0;
}

接口函数

#include <stdio.h>
#include <stdlib.h>
#include "1.h"
#include <string.h>

int ListInit(LinkList *L)
{
    (*L)=(LinkList)malloc(sizeof(Node));
    if((*L)==NULL)
    {
        exit(1);
    }
    (*L)->next=NULL;
}
int ListInsert(LinkList *L)
{
    LinkList p=(*L);
    LinkList n=(LinkList)malloc(sizeof(Node));
    if(n==NULL)
    {
        exit(1);
    }
    printf("please input information(id  name  address  phone  homephone):\n");
    scanf("%s %s %s %s %s",n->id,n->name,n->address,n->phone,n->homephone);
    /*printf("%s\n",x.phone);
    {
        strcpy(n->id,x.id);
        strcpy(n->name,x.name);
        strcpy(n->address,x.address);
        strcpy(n->phone,x.phone);
        strcpy(n->homephone,x.homephone);
    }*/

    if(strlen(n->phone)!=11)
    {
        printf("the phone is false!\n");
        return FAILURE;
    }
    if(strlen(n->homephone)!=8)
    {
        printf("the homephone is false!\n");
        return FAILURE;
    }
    while(p->next!=NULL)
    {
        if(strcmp((p->next)->phone,n->phone)==0)
        {
            printf("the phone is the same!Please input again:\n");
            scanf("%s",n->phone);
        }
        p=p->next;
    }
    p = *L;
    if(p -> next == NULL)
    {
        n->next = p->next;
        p->next = n;
    }
    else
    {
        while(p->next)
        {
            if(strcmp((p->next)->name,n->name)>=0)
            {
                n->next = p->next;
                p->next = n;
                break;
            }
            else
            {
                p=p->next;
            }
        }
        if(NULL == p->next)
        {
            n->next = p->next;
            p->next = n;

        }
    }
    return SUCCESS;
}
int ListTraverse(LinkList L)
{
    if(NULL==L->next)
    {   
        printf("empty!\n");
    }
    LinkList p=L->next;
    while(p!=NULL)
    {
        printf("id:%s name:%s address:%s phone:%s homephone:%s \n",p->id,p->name,p->address,p->phone,p->homephone);
        p=p->next;
    }
        return SUCCESS;
}
int ListSearch(LinkList L)
{
    LinkList p=L->next;
    char x[20];
    printf("Please input the name you want find:\n");
    scanf("%s",x);

    while(p!=NULL)
    {
        if(strcmp(p->name,x)==0)
        {
            printf("id:%s name:%s address:%s phone:%s homephone:%s \n",p->id,p->name,p->address,p->phone,p->homephone);
        }
        p=p->next;
    }
    /*if(p==NULL)
    {
        printf("no exist!\n");
    }*/
    return SUCCESS;
}
int ListDel(LinkList L)
{
    LinkList p=L;
    char y[20];
    printf("please input the name you want delete:\n");
    scanf("%s",y);
    LinkList tmp;
    while(p->next!=NULL)
    {
        if(strcmp(p->next->name,y)==0)
        {
            tmp=p->next;
            p->next=tmp->next;
            free(tmp);
            printf("Delete %s success!\n",y);
            break;
        }
        p=p->next;
    }
    return SUCCESS;
}
int ListChange(LinkList *L)
{
    LinkList p=(*L)->next;
    LinkList t=*L;
    char z[20];
    printf("please input the name you want change:\n");
    scanf("%s",z);

    while(p!=NULL)
    {
        LinkList n=(LinkList)malloc(sizeof(Node));
        if(strcmp(p->name,z)==0)
        {

            printf("please input information(id  name  address  phone  homephone):\n");
            scanf("%s %s %s %s %s",n->id,n->name,n->address,n->phone,n->homephone);
            if(strlen(n->phone)!=11)
            {
                printf("the phone is false!\n");
                return FAILURE;
            }
            if(strlen(n->homephone)!=8)
            {
                printf("the homephone is false!\n");
                return FAILURE;
            }
            while(t->next!=NULL)
            {
                if(strcmp((t->next)->phone,n->phone)==0)
                {
                    printf("the phone is the same!Please input again:\n");
                    scanf("%s",n->phone);
                }
                t=t->next;
            }
            strcpy(p->name , n->name);
            strcpy(p->id , n->id);
            strcpy(p->homephone , n->homephone);
            strcpy(p->phone , n->phone);
            strcpy(p->address , n->address);
            free(n);
            printf("change success!\n");
            break;
        }
        else
        {
            p=p->next;
        }
    }
    /*if(p==NULL)
    {
        printf("no exist!\n");
    }*/
    return SUCCESS; 
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值