通信錄管理系統(C語言)

/*
* 對通信錄進行插入、刪除、排序、查找、單個顯示功能
*/

#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include <stdlib.h>

int n;

typedef struct _Address_List
{
    char name[30];            //名字
    char work[30];            //職業
    char handset[20];        //手機號碼
    char email[30];            //電子郵件
    char address[30];        //地址
    struct _Address_List *next;
}address_List;
#define LEN sizeof(address_List)

address_List *Release(address_List *head);

//建立一個通信錄
address_List *Create(void)
{
    address_List *head,*p1,*p2;
    char name[30];
    n = 0;
    p1 = (address_List *)malloc(LEN);
    p2 = p1;
    printf("請輸入通信錄的內容!\n姓名輸入爲0時表示建立完畢!\n");
    printf("請輸入姓名:");
    gets(name);
    if(strcmp(name,"0")!=0)
    {
        strcpy(p1->name,name);
        printf("請輸入職業:");
        gets(p1->work);
        printf("請輸入手機:");
        gets(p1->handset);
        printf("請輸入電子郵件:");
        gets(p1->email);
        printf("請輸入通信地址:");
        gets(p1->address);
        head = NULL;
        while(1)
        {
            n = n+1;
            if(n == 1)
            {
                head = p1;
            }
            else
            {
                p2->next = p1;
            }
            p2 = p1;
            printf("請輸入姓名:");
            gets(name);
            if(strcmp(name,"0") == 0)
            {
                break;
            }
            else
            {
                p1 = (address_List *)malloc(LEN);
                strcpy(p1->name,name);
                printf("請輸入職業:");
                gets(p1->work);
                printf("請輸入手機:");
                gets(p1->handset);
                printf("請輸入電子郵件:");
                gets(p1->email);
                printf("請輸入通信地址:");
                gets(p1->address);
            }
        }
        p2->next = NULL;
        return head;
    }
    else
    {
        return 0;
    }
}

//打印整個通信錄
void print(address_List *head)
{
    address_List *p;
    if(head != NULL)
    {
        p = head;
        printf("本通信錄如今共有%d人;\n",n);
        printf("---姓名---------職業----------手機--------Email-------------通信地址\n");
        printf("====================================================================\n");
        do
        {
            printf("=%s\t\t",p->name);
            printf("=%s\t\t",p->work);
            printf("=%s\t\t",p->handset);
            printf("=%s\t\t",p->email);
            printf("=%s\n",p->address);
            p = p->next;
        }while(p != NULL);
        printf("==================================================================\n");
    }
    else
    {
        printf("通信錄爲空,沒法輸出!\n");
    }
}

//在通信錄插入
address_List *insert(address_List *head)
{
    address_List *p0,*p1,*p2;
    char name[20];
    p1 = head;
    printf("請輸入增長的內容:\n");
    printf("請輸入姓名:");
    gets(name);
    if(strcpy(name,"0") == 0)
    {
        printf("姓名不能爲0,增長失敗!\n");
        return head;
    }
    else
    {
        p0 = (address_List *)malloc(LEN);
        strcpy(p0->name,name);
        printf("請輸入職業:");
        gets(p1->work);
        printf("請輸入手機:");
        gets(p1->handset);
        printf("請輸入電子郵件:");
        gets(p1->email);
        printf("請輸入通信地址:");
        gets(p1->address);
        n = n+1;
        if(head == NULL)
        {
            head = p0;
            p0->next = NULL;
            return head;
        }
        else
        {
            while(strcmp(p0->name,p1->name) > 0 && (p1->next != NULL))
            {
                p2 = p1;
                p1 = p1->next;
            }
            if(strcmp(p0->name,p1->name) <0 || strcmp(p0->name,p1->name) == 0)
            {
                if(head == p1)
                {
                    head = p0;
                }
                else
                {
                    p2->next = p0;
                }
                p0->next = p1;
            }
            else
            {
                p1->next = p0;
                p0->next = NULL;
            }
            return head;
        }
    }
}

//刪除通信錄中某我的
address_List *delete_txl(address_List *head)
{
    address_List *p,*q;
    char name[30];
    if(head == NULL)
    {
        printf("通信錄爲空,沒法刪除!\n");
        return head;
    }
    p = head;
    printf("請輸入須要刪除的人姓名:");
    gets(name);
    if(strcmp(head->name,name) == 0)
    {
        head = head->next;
        free(p);
        printf("刪除操做成功!\n");
        return head;
    }
    else
    {
        q = head;
        p = head->next;
        while(p != NULL)
        {
            if(strcmp(p->name,name) == 0)
            {
                q->next = p->next;
                free(p);
                printf("刪除操做成功!\n");
                return head;
            }
            p = p->next;
            q = q->next;
        }
    }
}

//顯示通信錄中某我的
address_List *display(address_List *head)
{
    address_List *p1,*p2;
    char name[30];
    int m;
    if(head == NULL)
    {
        printf("通信錄爲空,沒法顯示!\n");
        return head;
    }
    p1 = head;
    m = 0;
    printf("請輸入要顯示人的姓名:");
    gets(name);
    while(p1 != NULL)
    {
        while(strcmp(p1->name,name) != 0 && p1->next != NULL)
        {
            p2 = p1;
            p1 = p1->next;
        }
        if(strcmp(p1->name,name) == 0)
        {
            m++;
            printf("%s的通信內容以下:\n",name);
            printf("---姓名---------職業----------手機--------Email-------------通信地址\n");
            printf("====================================================================\n");
            printf("=%s=\t\t",p1->name);
            printf("=%s=\t\t",p1->work);
            printf("=%s=\t\t",p1->handset);
            printf("=%s=\t\t",p1->email);
            printf("=%s=\n",p1->address);
            printf("====================================================================\n");
        }
        p1 = p1->next;
    }
    if(m == 0)
    {
        printf("此人不在通信錄中!\n");
    }
    return head;
}

//對通信錄進行排序操做
address_List *Sort(address_List *head)
{
    address_List *p1,*p2;
    int i,j;
    typedef struct _Address_List1
    {
        char name[30];            //名字
        char work[30];            //職業
        char handset[20];        //手機號碼
        char email[30];            //電子郵件
        char address[30];        //地址
    }address_List1;
    address_List1 Sort[200];
    address_List1 temp;
    if(head == NULL)
    {
        printf("通信錄爲空,沒法排序!\n");
        return head;
    }
    p1 = head;
    for(i = 0;i < n,p1 != NULL; i++)
    {
        strcpy(Sort[i].name,p1->name);
        strcpy(Sort[i].work,p1->work);
        strcpy(Sort[i].handset,p1->handset);
        strcpy(Sort[i].email,p1->email);
        strcpy(Sort[i].address,p1->address);
        p2 = p1;
        p1 = p1->next;
    }
    head = Release(head);
    for(j = 0; j < n-1; j++)
    {
        for(i = j+1; i < n; i++)
        {
            if(strcmp(Sort[i].name,Sort[j].name) < 0)
            {
                Sort[i] = temp;
                temp = Sort[j];
                Sort[j] = temp;
            }
        }
    }
    p1 = (address_List *)malloc(LEN);
    p2 = p1;
    strcpy(p1->name,Sort[0].name);
    strcpy(p1->work,Sort[0].work);
    strcpy(p1->handset,Sort[0].handset);
    strcpy(p1->email,Sort[0].email);
    strcpy(p1->address,Sort[0].address);

    head = p1;
    for(i = 1; i < n; i++)
    {
        p1 = (address_List *)malloc(LEN);
        strcpy(p1->name,Sort[i].name);
        strcpy(p1->work,Sort[i].work);
        strcpy(p1->handset,Sort[i].handset);
        strcpy(p1->email,Sort[i].email);
        strcpy(p1->address,Sort[i].address);

        p2->next = p1;
        p2 = p1;
    }
    p2->next = NULL;
    printf("按姓名排序後的結果是:\n");
    print(head);
    return head;
}

//查找通信錄中某我的
address_List *Search_name(address_List *head)
{
    address_List *p1,*p2;
    int m;
    char name[30];
    if(head == NULL)
    {
        printf("通信錄爲空,沒法查找!\n");
        return head;
    }
    p1 = head;
    printf("**************************\n");
    printf("****請輸入要查找的姓名:**\n");
    printf("**************************\n");
    m = 0;
    gets(name);
    while(p1 != NULL)
    {
        while(strcmp(p1->name,name) != 0 && (p1->next != NULL))
        {
            p2 = p1;
            p1 = p1->next;
        }
        if(strcmp(p1->name,name) == 0)
        {
            m++;
            printf("你查找的內容是:\n");
            printf("++++++++++++++++++++++++++++++++\n");
            printf("++ %s\t%s\t%s\t%s\t%s  ++",p1->name,p1->work,p1->handset,p1->email,p1->address);
            printf("++++++++++++++++++++++++++++++++\n");
        }
        p1 = p1->next;
        if(m == 0)
        {
            printf("你查找的姓名不在通信錄中!\n");
        }
        break;
    }
    return head;
}

//釋放整個通信錄
address_List *Release(address_List *head)
{
    address_List *p;
    while(head != NULL)
    {
        p = head;
        head = head->next;
        free(p);
    }
    return head;
}

//保存(以文件的形式保存)
void save(address_List *head)
{
    FILE *fp;
    address_List *p;
    char Filename[30];        //保存後的文件名
    if(head ==NULL)
    {
        printf("待保存的通信錄爲空,沒法保存!\n");
        return ;
    }
    printf("請輸入保存後的文件名:");
    gets(Filename);

    fp = fopen("Filename.txt","w");
    if(fp == NULL)
    {
        printf("沒法打開文件!\n");
        return ;
    }

    p = head;
    fprintf(fp,"姓名\t職業\t手機\tEmail\t地址\n");
    for(;p != NULL;)
    {
        fprintf(fp,"姓名\t職業\t手機\tEmail\t地址",p->name,p->work,p->handset,p->email,p->address);
        p = p->next;
    }
    printf("保存完畢!\n");
    fclose(fp);
}

//文件讀出函數
address_List *Load(address_List *head)
{
    FILE *fp;
    char Filename[30];
    address_List *p1,*p2;
    printf("請輸入要輸出的文件名:");
    gets(Filename);
    fp = fopen("Filename.txt","r");
    if(fp == NULL)
    {
        printf("此通信錄不存在,沒法輸出!\n");
        return head;
    }
    else
    {
        head = Release(head);
    }
    p1 = (address_List *)malloc(LEN);
    fscanf(fp,"%s%s%s%s%s",p1->name,p1->work,p1->handset,p1->email,p1->address);
    if(feof(fp) != 0)
    {
        printf("文件爲空,沒法打開!\n");
        return head;
    }
    else
    {
        rewind(fp);
        p2 = p1;
        head = p1;
        n = 0;
        while(feof(fp) == 0)
        {
            fscanf(fp,"%s%s%s%s%s",p1->name,p1->work,p1->handset,p1->email,p1->address);
            if(feof(fp) != 0)
            {
                break;
            }
            p2->next = p1;
            p2 = p1;
            p1 = (address_List *)malloc(LEN);
            n = n+1;
        }
        p2->next = NULL;
        p1 = head;
        head = head->next;
        n = n-1;
        free(p1);
        print(head);
        printf("打開完畢!\n");
        return head;
    }
    fclose(fp);
}


//菜單選擇函數
address_List *menu(address_List *head)
{
    char num[10];
    while(1)
    {
        printf("*******************************\n");
        printf("*****1. 姓名查找     **********\n");
        printf("*****2. 單個顯示     **********\n");
        printf("*****3. 增長         **********\n");
        printf("*****4. 退出         **********\n");
        printf("*******************************\n");
        printf("請輸入你選擇的操做:");
        gets(num);
        switch(*num)
        {
        case '1':
            {
                head = Search_name(head);
                print(head);
            }
            break;
        case '2':
            {
                head = display(head);
            }
            break;
        case '3':
            {
                head = insert(head);
                print(head);
            }
            break;
        case '4':
            return head;
        default:
            printf("操做有誤,此項不存在!\n");
            break;
        }
        if(strcmp(num,"6") == 0)
        {
            break;
        }
    }
    return head;
}

//主函數
int main(void)
{
    address_List *head = NULL;
    char num[10];
    printf("*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*\n");
    printf("*=*=*=*=*=*=*=            程序說明        *=*=*=*=*=*=*\n");
    printf("*=*=*=*=*=*=*=    請及時保存建立完畢的通信錄內容        *=*=*=*=*=*=*\n");
    printf("*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*\n");
    while(1)
    {
        printf("*******************************\n");
        printf("******    1. 建立通信錄   *******\n");
        printf("******    2. 按名字排序    *******\n");
        printf("******    3. 綜合操做    *******\n");
        printf("******    4. 保存        *******\n");
        printf("******    5. 打開        *******\n");
        printf("******    6. 刪除        *******\n");
        printf("******    7. 退出        *******\n");
        printf("*******************************\n");
        printf("請輸入你選擇的操做:");
        gets(num);
        switch(*num)
        {
        case '1':
            {
                if(head == NULL)
                {
                    head = Create();
                    print(head);
                }
                else
                {
                    head = Release(head);
                    head = Create();
                    print(head);
                }
            }
            break;
        case '2':
            {
                head = Sort(head);
            }
            break;
        case '3':
            {
                head = menu(head);
            }
            break;
        case '4':
            {
                save(head);
                print(head);
            }
            break;
        case '5':
            {
                head = Load(head);
            }
            break;
        case '6':
            {
                head = delete_txl(head);
                print(head);
            }
            break;
        case '7':
            {
                head = Release(head);
            }
            break;
        default:
            {
                printf("操做有誤,此項不存在!\n");
            }
            break;
        }
        if(strcmp(num,"7") == 0)
        {
            break;
        }
    }
    return 0;
}
相關文章
相關標籤/搜索