通讯录管理系统(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;
}

 

转载于:https://www.cnblogs.com/tslDream/p/4455972.html

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,您想了解通讯录管理系统C语言实现。通讯录管理系统可以用来存储联系人的姓名、电话号码、地址等信息,并且可以进行添加、删除、修改、查询等操作。以下是一个简单的通讯录管理系统C语言实现。 ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_CONTACTS 100 struct contact { char name[50]; char phone[20]; char address[100]; }; struct contact contacts[MAX_CONTACTS]; int num_contacts = 0; void add_contact() { if (num_contacts == MAX_CONTACTS) { printf("The contact list is full.\n"); return; } struct contact new_contact; printf("Enter the name: "); scanf("%s", new_contact.name); printf("Enter the phone number: "); scanf("%s", new_contact.phone); printf("Enter the address: "); scanf("%s", new_contact.address); contacts[num_contacts++] = new_contact; printf("Contact added.\n"); } void delete_contact() { if (num_contacts == 0) { printf("The contact list is empty.\n"); return; } char name[50]; printf("Enter the name of the contact to delete: "); scanf("%s", name); int found = 0; for (int i = 0; i < num_contacts; i++) { if (strcmp(name, contacts[i].name) == 0) { found = 1; for (int j = i; j < num_contacts - 1; j++) { contacts[j] = contacts[j + 1]; } num_contacts--; printf("Contact deleted.\n"); break; } } if (!found) { printf("Contact not found.\n"); } } void modify_contact() { if (num_contacts == 0) { printf("The contact list is empty.\n"); return; } char name[50]; printf("Enter the name of the contact to modify: "); scanf("%s", name); int found = 0; for (int i = 0; i < num_contacts; i++) { if (strcmp(name, contacts[i].name) == 0) { found = 1; printf("Enter the new phone number: "); scanf("%s", contacts[i].phone); printf("Enter the new address: "); scanf("%s", contacts[i].address); printf("Contact modified.\n"); break; } } if (!found) { printf("Contact not found.\n"); } } void display_contact() { if (num_contacts == 0) { printf("The contact list is empty.\n"); return; } for (int i = 0; i < num_contacts; i++) { printf("Name: %s\n", contacts[i].name); printf("Phone: %s\n", contacts[i].phone); printf("Address: %s\n", contacts[i].address); printf("\n"); } } int main() { int choice; while (1) { printf("1. Add a contact\n"); printf("2. Delete a contact\n"); printf("3. Modify a contact\n"); printf("4. Display all contacts\n"); printf("5. Exit\n"); printf("Enter your choice: "); scanf("%d", &choice); switch (choice) { case 1: add_contact(); break; case 2: delete_contact(); break; case 3: modify_contact(); break; case 4: display_contact(); break; case 5: exit(0); default: printf("Invalid choice.\n"); } printf("\n"); } return 0; } ``` 这个通讯录管理系统使用了一个结构体 `contact` 来存储每个联系人的信息,包括姓名、电话号码和地址。在主函数中,使用一个循环来显示菜单,让用户可以选择进行添加、删除、修改、查询等操作。每个操作都对应一个函数,实现相应的功能。需要注意的是,这个通讯录管理系统最多只能存储100个联系人的信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值