C语言实现简单电子通讯录

需求

制作一个电子通信录,通过该通讯录能存入好友ID号、姓名(英文)、手机号码、公司电话。

模块

·主界面:主要显示软件功能
A) 添加好友信息
B) 列表好友信息
C) 搜索好友
D) 更新好友信息
F) 删除好友

·添加好友:
用户输入命令后,让用户输入好友信息,个人电话号码不能为空,添加成功或失败都需要提示信息。

·列表好友:
可以根据电话号码或名称排序进行显示,使用堆排序和快速排序。

·搜索好友:
要求可以根据三种方式搜索:ID、名字、个人号码。如果未搜索到请提示,如果搜索到,显示出该好友信息。

·更新好友信息:
根据好友名字进行更新,可以改写除名字外所有信息。

·删除好友:
要求可以根据三种方式删除:ID、名字、个人号码。如果不存在该用户则提示,删除成功后提示。

使用文件进行输入输出,初始信息存储在“data.txt”中,完成对通讯录的操作后将所有信息存入“datachange.txt”中。

声明部分

#include 
#include 
#include 
#include 
#define T 1
#define F 0
#define MAX 20
struct ListNode
{
    int number;
    char name[MAX];
    char phonenumber[MAX];
    char homenumber[MAX];
    char qqnumber[MAX];
};

typedef struct list
{
    struct ListNode p;
    struct list *next;
}*List;

int init(List *member,List pointer[]);
void menu();
int select1(List member,List pointer[]);
int input(List member);
void creatpointer(List member,List pointer[]);
int view(List member,List pointer[]);
int search(List member);
int delete(List member);
int change(List member);
void print1(List pointer[],int len);

void Quick  (List pointer[],int low,int high);
int pivotkey1(List pointer[],int low,int high);
int pivotkey2(List pointer[],int low,int high);
void swap(List pointer[],int low,int high);

void heapadjust(List pointer[],int i,int m);
void heap(List pointer[],int len);

int readfile(List member);
int writerfile(List pointer[]);

循环链表初始化

int init(List *member,List pointer[])
{
    List newnode = (List)malloc(sizeof(struct list));
    if(NULL == newnode)
    {
        return F;
    }
    newnode->p.number = 142000;
    newnode->next = newnode;
    *member = newnode;
    pointer[0] = newnode;
    return T;
}

菜单和选择函数

void menu()
{
    printf("***************************************\n");
    printf("*****这 是 一 个 简 单 的 通 讯 录*****\n");
    printf("********键 入 数 字 选 择 功 能********\n");
    printf("*******    1: 新 建 联 系 人    *******\n");
    printf("*******    2: 查 看 联 系 人    *******\n");
    printf("*******    3: 查 找 联 系 人    *******\n");
    printf("*******    4: 删 除 联 系 人    *******\n");
    printf("*******    5: 修 改 联 系 人    *******\n");
    printf("*******    6: 退 出 通 讯 录    *******\n");
    printf("*************** 请 输 入 **************\n");
    printf("***************************************\n");
}

int select1(List member,List pointer[])
{
    creatpointer(member,pointer);
    int option;
    while(option != 6)
    {
        menu();
        scanf("%d",&option);
        switch(option)
        {
            case 1:{
                    input(member);
                    printf("*******输 入 完 成,返 回 菜 单*******\n");
                    creatpointer(member,pointer);
                    break;
                  }

            case 2:{
                    view(member,pointer);
                    printf("*******查 看 完 成,返 回 菜 单*******\n");
                    break;
                 }

            case 3:{
                    search(member);
                    printf("*******查 找 完 成,返 回 菜 单*******\n");
                    break;
                  }

            case 4:{
                    delete(member);
                    printf("*******删 除 完 成,返 回 菜 单*******\n");
                    creatpointer(member,pointer);
                    break;
                  }
            case 5:{
                    change(member);
                    printf("*******修 改 完 成,返 回 菜 单*******\n");
                    break; 
                  }

            case 6:{
                    printf("*******欢 迎 再 次 使 用,谢 谢 !*******\n");
                    writerfile(pointer);
                    break;
                  }


            default:{
                    printf("*******输 入 有 误,请 重 新 输 入 !*******\n");
                    break; 
                }
         }
     }
    return T;
}

功能函数

int input(List member)
{
    while(1)
    {
        char a[MAX];
        List newnode = (List)malloc(sizeof(struct list));
        if(NULL == newnode)
        {
            return F;
        }
        printf("*******请 输 入 姓 名*******\n");
        gets(a);
        gets(newnode->p.name);
        printf("*******请 输 入 个 人 电 话*******\n");
        gets(newnode->p.phonenumber);
        printf("*******请 输 入 家 庭 电 话*******\n");
        gets(newnode->p.homenumber);
        printf("*******请 输 入 Q Q 号 码*******\n");
        gets(newnode->p.qqnumber);
        if(strlen(newnode->p.phonenumber) == 0)
        {
            printf("*******个 人 电 话 不 能 为 空,重 新 建 立!*******\n");
            free(newnode);
        }
        else
        {
            List f = member;
            while(f->next != member)
            {
                f = f->next;
            }
            newnode->p.number = f->p.number + 1;
            newnode->next = f->next;
            f->next = newnode;
        }
        printf("*******请 选 择 功 能*******\n");
        printf("*******1 继 续 输 入*******\n");
        printf("*******2 返 回 菜 单*******\n");
        int option;
        scanf("%d",&option);
        if(option == 1)
        {
        }
        else if(option == 2)
        {
            break;
        }
        else if(option != 1 && option != 2)
        {
            printf("*******输 入 错 误 !*******\n");
            break;
        }
    }
}

void creatpointer(List member,List pointer[])
{
    List p = member;
    int i = 1;
    while(p->next != member)
    {
        pointer[i] = p->next;
        i++;
        p = p->next;
    }
    while(i+1 < 20)
    {
        pointer[i] = 0;
        i++;
    }
}

void print1(List pointer[],int len)
{
    int i;
    for(i = 1;i <= len;i++)
    {
        printf("学 号    姓名             个人电话      家庭电话      Q Q号码\n");
        printf("%d  %-15s  %-11s    %-11s   %-11s\n",pointer[i]->p.number,pointer[i]->p.name,
           pointer[i]->p.phonenumber,pointer[i]->p.homenumber,pointer[i]->p.qqnumber);
    }
}

int view(List member,List pointer[])
{
    printf("*******请 选 择 显 示 方 式 !*******\n");
    printf("*******1. 按 字 母 从 小 到 大 顺 序 显 示 。*******\n");
    printf("*******2. 按 个 人 号 码 从 小 到 大 顺 序 显 示。*******\n");
    printf("*******3. 直 接 查 看*******\n");
    int i;
    int len = 0;
    List p = member;
    while(p->next != member)
    {
        len++;
        p = p->next;
    }
    printf("len = %d\n",len);
    int option;
    scanf("%d",&option);
    switch(option)
    {
        case 1:{
            Quick(pointer,1,len);
            print1(pointer,len);
            break;
        }
        case 2:{
            heap(pointer,len);
            print1(pointer,len);
            break;
        }
        case 3:{
            List f = member;
            while(f != member->next)
            {
                printf("学 号    姓名             个人电话      家庭电话      Q Q号码\n");
                printf("%d  %-15s  %-11s    %-11s   %-11s\n",
                member->next->p.number,member->next->p.name,
                member->next->p.phonenumber,member->next->p.homenumber,member->next->p.qqnumber);
                member = member->next;
            }
        }
    }

}

int search(List member)
{
    printf("请选择查找方式!\n");
    printf("1.按学号查找。\n");
    printf("2.按姓名查找。\n");
    printf("3.按个人电话查找。\n");
    int option;
    scanf("%d",&option);
    char a[MAX];
    gets(a);
    switch(option)
    {
        case 1:{
                printf("*******请 输 入 要 查 找 联 系 人 的 学 号*******\n");
                int a;
                scanf("%d",&a);
                int flag = 0;
                List p = member;
                while(p != member->next)
                {
                    if(a == member->next->p.number)
                    {
                        printf("学 号    姓名             个人电话      家庭电话      Q Q号码\n");
                        printf("%d  %-15s  %-11s    %-11s   %-11s\n",
                        member->next->p.number,member->next->p.name,
                        member->next->p.phonenumber,member->next->p.homenumber,member->next->p.qqnumber);
                        member = member->next;
                        flag = 1; 
                    }
                    member = member->next;
                }
                if(flag = 0)
                {
                    printf("*******未 找 到 该 联 系 人 !*******\n");
                }
                break;
        }
        case 2:{
                printf("*******请 输 入 要 查 找 人 的 姓 名*******\n");
                char sn[MAX];
                gets(sn);
                int flag = 0;
                List p = member;
                while(p != member->next)
                {
                    if(0 == strcmp(sn,member->next->p.name))
                    {
                        printf("学 号    姓名             个人电话      家庭电话      Q Q号码\n");
                        printf("%d  %-15s  %-11s    %-11s   %-11s\n",
                        member->next->p.number,member->next->p.name,
                        member->next->p.phonenumber,member->next->p.homenumber,member->next->p.qqnumber);
                        member = member->next;
                        flag = 1;
                    }
                    member = member->next;
                }
                if(flag = 0)
                {
                    printf("*******未 找 到 该 联 系 人 !*******\n");
                }
                break;
            }
        case 3:{
                printf("*******请 输 入 要 查 找 人 的 个 人 电 话*******\n");
                char sp[MAX];
                gets(sp);
                int flag = 0;
                List p = member;
                while(p != member->next)
                {
                    if(0 == strcmp(sp,member->next->p.phonenumber))
                    {
                        printf("学 号    姓名             个人电话      家庭电话      Q Q号码\n");
                        printf("%d  %-15s  %-11s    %-11s   %-11s\n",
                        member->next->p.number,member->next->p.name,
                        member->next->p.phonenumber,member->next->p.homenumber,member->next->p.qqnumber);
                        member = member->next;
                        flag = 1;
                    }
                    member = member->next;
                }
                if(flag = 0)
                {
                    printf("*******未 找 到 该 联 系 人 !*******\n");
                }
                break;
        }
    }
}

int delete(List member)
{
    printf("请选择删除方式!\n");
    printf("1.按学号删除。\n");
    printf("2.按姓名删除。\n");
    printf("3.按个人电话删除。\n");

    int option;
    scanf("%d",&option);
    char a[MAX];
    gets(a);
    switch(option)
    {
        case 1:{
                printf("请输入要删除联系人的学号\n");
                int sa;
                scanf("%d",&sa);
                int flag = 0;
                List p = member;
                List temp;
                while(p != member->next)
                {
                    if(sa == member->next->p.number)
                    {
                        temp = member->next;
                        member->next = temp->next;
                        free(temp);
                        flag = 1; 
                    }
                    member = member->next;
                }
                if(flag == 0)
                {
                    printf("未找到该联系人!\n");
                }
                break;
        }
        case 2:{
                printf("请输入要查找人的姓名\n");
                char sn[MAX];
                gets(sn);
                int flag = 0;
                List p = member;
                List temp;
                while(p != member->next)
                {
                    if(0 == strcmp(sn,member->next->p.name))
                    {
                        temp = member->next;
                        member->next = temp->next;
                        free(temp);
                        flag = 1;
                    }
                    member = member->next;
                }
                if(flag == 0)
                {
                    printf("未找到该联系人!\n");
                }
                break;
            }
        case 3:{
                printf("请输入要查找人的个人电话\n");
                char sp[MAX];
                gets(sp);
                int flag = 0;
                List p = member;
                List temp;
                while(p != member->next)
                {
                    if(0 == strcmp(sp,member->next->p.phonenumber))
                    {
                        temp = member->next;
                        member->next = temp->next;
                        free(temp);
                        flag = 1;
                    }
                    member = member->next;
                }
                if(flag == 0)
                {
                    printf("未找到该联系人!\n");
                }
                break;
        }
    }
}

int change(List member)
{
    char a[MAX];
    gets(a);
    printf("请输入要修改联系人的名字\n");
    char qq[MAX];
    gets(qq);
    printf("请输入修改后联系人的名字\n");
    char cn[MAX];
    gets(cn);
    printf("请输入修改后联系人的个人电话\n");
    char cp[MAX];
    gets(cp);
    printf("请输入修改后联系人的家庭电话\n");
    char ch[MAX];
    gets(ch);
    printf("请输入修改后联系人的QQ号码\n");
    char cq[MAX];
    gets(cq);
    List f = member;
    int flag = 0;
    while(f != member->next)
    {
        if(0 == strcmp(qq,member->next->p.name))
        {
            printf("yes\n");
            strcpy(member->next->p.name,cn);
            strcpy(member->next->p.phonenumber,cp);
            strcpy(member->next->p.homenumber,ch);
            strcpy(member->next->p.qqnumber,cq);
            flag = 1;
        }
        member = member->next;
    }
    if(flag = 0)
    {
        printf("未找到该联系人!\n");
    }
    return T;
}

//快速排序
void Quick  (List pointer[],int low,int high)
{
    int pivot;
    if(low < high)
    {
        pivot = pivotkey1(pointer,low,high);
        Quick(pointer,low,pivot-1);
        Quick(pointer,pivot+1,high); //pivot处不再参与排序
    }
}

int pivotkey1(List pointer[],int low,int high)
{
    char pivotkey[MAX]={0};
    strcpy(pivotkey,pointer[low]->p.name);
    while(low < high)
    {
        while(low < high && strcmp(pointer[high]->p.name,pivotkey) == 1)
        {
            high--;
        }
        swap(pointer,low,high);
        while(low < high && strcmp(pointer[low]->p.name,pivotkey) == -1)
        {
            low++;
        }
        swap(pointer,low,high);
    }
    return low;
}
int pivotkey2(List pointer[],int low,int high)
{
    char pivotkey[MAX]={0};
    strcpy(pivotkey,pointer[low]->p.name);
    //pivotkey = arr[low];   
    int i;
    int tail = low + 1;
    for(i = low + 1;i <= high;i++)
    {
        if(-1 == strcmp(pointer[i]->p.name,pivotkey)) //arr[i] <= pivotkey 
            swap(pointer,i,tail++);
    }
    swap(pointer,low,tail-1);
    return tail-1;
}

//堆排序
void heap (List pointer[],int len)
{
    int i;
    for(i = len/2;i > 0;i--)
    {
        heapadjust(pointer,i,len);//开始时数组并无顺序,排len/2个结点;
    }
    for(i = len;i > 1;i--)
    {
        swap(pointer,1,i);
        heapadjust(pointer,1,i-1);//最后一个脱离排序;
    }
}

void heapadjust(List pointer[],int i,int m)
{
    int j;
    List temp;
    temp = pointer[i]; //temp = arr[i];
    for(j = i * 2;j <= m;j *= 2)
    {
        if(j < m && -1 == strcmp(pointer[j]->p.phonenumber,pointer[j+1]->p.phonenumber))
        {
            ++j;
        }
        if(strcmp(temp->p.phonenumber,pointer[j]->p.phonenumber) >= 0) //temp >= arr[j]
            break;
        pointer[i] = pointer[j];//arr[i] = arr[j];
        i = j;
    }
    pointer[i] = temp;
}

void swap(List pointer[],int low,int high)
{
    List temp;
    temp = pointer[low];
    pointer[low] = pointer[high];
    pointer[high] = temp;
}

文件操作

//读入文件
int readfile(List member)
{
    FILE *file1 = fopen("./data.txt","r"); //打开文件
    if(NULL == file1)
    {
        perror("fopen1");
        exit(1);
    }
    List f = member;
    int flag;
    fscanf(file1,"%d",&flag);
    while(flag != 0)
    {
        List newnode = (List)malloc(sizeof(struct list));
        if(NULL == newnode)
        {
            return F;
        }
        while(f->next != member)
        {
            f = f->next;
        }
        newnode->next = member;
        f->next = newnode;
        newnode->p.number = flag;
        fscanf(file1,"%s %s %s %s",newnode->p.name,newnode->p.phonenumber,
            newnode->p.homenumber,newnode->p.qqnumber);
        fscanf(file1,"%d",&flag);
    }
}

//写入文件
int writerfile(List pointer[])
{
    FILE *file2 = fopen("./datachange.txt","w+"); //创建文件
    if(NULL == file2)
    {
        perror("fopen2");
        exit(2);
    }
    int i = 1;
    while(pointer[i] != 0)
    {
        fprintf(file2,"%d %s %s %s %s\n",pointer[i]->p.number,pointer[i]->p.name,
            pointer[i]->p.phonenumber,pointer[i]->p.homenumber,
            pointer[i]->p.qqnumber);
        i++;
    }
    fprintf(file2,"%d",0);
}

你可能感兴趣的:(C语言实现简单电子通讯录)