制作一个电子通讯录(终端执行源码)

来源:互联网 发布:淘宝折扣价在哪设置 编辑:程序博客网 时间:2024/05/17 22:01

/***************************************************
 Copyright (C), 1988-1999, Huawei Tech. Co., Ltd.
  File name: test.c
  Author : xuliang Version: 0.0  Date:2013/7/31
  Description :该项目适用于通讯录

 ****************************************************/
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "myenum.h"


struct node
{
    int id;
    char name[20];
    char coph[11];                //公司电话
    char mobtel[11];            //手机
    char addr[50];              //住址
    struct node *next;
};

typedef struct node link;


/***********************************************显示和选择部分************************************************************/
int choice()
{
    char yourch[10];             //该数组用来存放你输入的选项
    scanf("%s",yourch);
    if (strcmp(yourch, "Insert")==0)
    {
        return INSERT;
    }
    if (strcmp(yourch,"Display")==0)
    {
        return DISPLAY;
    }
    if (strcmp(yourch, "Search")==0)
    {
        return SEARCH;
    }
    if (strcmp(yourch, "Delete")==0)
    {
        return DELETE;
    }
    if (strcmp(yourch,   "Quit")==0)
    {
        return QUIT;
    }

 


}

void interface()
{
    printf("**********************************My Chat*************************************\n");
    printf("*                                                                            *\n");
    printf("* 该项目可以实现的功能列表是:                                               *\n");
    printf("*                                                                            *\n");
    printf("*     1.Insert                                                               *\n");
    printf("*                                                                            *\n");
    printf("*     2.Display                                                              *\n");
    printf("*                                                                            *\n");
    printf("*     3.Search                                                               *\n");
    printf("*                                                                            *\n");
    printf("*     4.Delete                                                               *\n");
    printf("*                                                                            *\n");
    printf("*     5.Quit                                                                 *\n");
    printf("*                                                                            *\n");
    printf("*                                                                            *\n");
    printf("* 请输入你所要求的功能,不可以输入数字:                                      *\n");
    printf("********************************************************************^ >^******\n");
}

/*美化输出单个节点*/
void print(link *nownode)
{
    printf("#############################################\n");
    printf("# Id        :           %d\n",nownode->id);
    printf("# name      :           %s\n",nownode->name);
    printf("# co-phone  :           %s\n",nownode->coph);
    printf("# mob tel   :           %s\n",nownode->mobtel);
    printf("# adrr      :           %s\n",nownode->addr);
    printf("#############################################\n");
}

/***********************************************用户信息创建部分************************************************************/
/*函数描述
 *Function :
 *
 */
/*结尾插入*/
link *insert_end_link(link *head, link *newnode)
{
    if(newnode == NULL)
    {
        exit(IN_NO);
    }
    if(head ==NULL)
    {
        newnode->next = NULL;
        head = newnode;
        return head;
    }
    link *tmp = head;
    while(tmp->next != NULL)//将tmp置于最后一位
    {
        tmp =tmp->next;
    }
    tmp->next = newnode;
    newnode->next = NULL;

 

    return head;
}

/***********************************************用户信息显示************************************************************/
/*显示函数*/
void display_link(link *head)
{
    link *temp = head;
    while(temp != NULL)
    {
        print(temp);
        temp = temp->next;
    }
    printf("\n");

}

int search(link *head)
{
    link *tmp = head;
    char *p;
    int count=0;
    if((p = (char*)malloc(sizeof(char))) == NULL)
    {
        printf("p malloc error!\n");
        exit(0);
    }

    printf("请输入你要查询的名字\n");
    scanf("%s",p);


    while(tmp != NULL)
    {
        if(strcmp(tmp->name, p) == 0)
        {
            count++;
            print(tmp);
        }
        tmp=tmp->next;
    }
    return count;
}

/*节点的删除*/
link *delete_node(link *head,int id)
{
    if(head ==NULL)
    {
        printf("HEAD is NULL !delete error!");
        exit(OUT_NO);
    }
    link *tmp = head, *pre;
   
    if(head->id == id)
    {
        tmp = head;
        head = head->next;
    }
    else
    {
        pre = head;
        tmp = pre->next;
        while(tmp != NULL &&tmp->id !=  id)
        {
            pre = tmp;
            tmp = tmp->next;
        }
        if(tmp->id == id)
        {
            pre->next =tmp->next;
          
            
        }
       
    }
    free(tmp);
    return(head);
}

/*测试主函数*/
int main()
{
    int i=1,sum;//用于分配序号的,sum 用于记录同名数

    struct node *head, *ptr;//ptr用于指向临时变量的
    head = NULL;
   
    while(1)
    {
       
        interface();
        switch (choice())
        {
            case INSERT :
            {
                printf("您选择的是Insert\n");
                if((ptr = (link *)malloc(sizeof(link))) == NULL)
                {
                    printf("数据插入失败\n");
                   
                }
                else
                {
                    printf("名字:\t公司电话:\t手机:\t地址:\n");
                    scanf("%s%s%s%s",ptr->name, ptr->coph, ptr->mobtel, ptr->addr);

                    printf("数据插入成功\n");
                    ptr->id = i;
                    i++;
                    head = insert_end_link(head,ptr);
                }
                break;
            }
            case DISPLAY :
            {
                printf("您选择的是DISPLAY\n");
                display_link(head);
                break;
            }
            case SEARCH :
            {
               

                printf("您选择的是SEARCH\n");
                sum = search(head);
                printf("共找到%d个同名人物\n",sum);              
                break;
            }
            case DELETE :
            {
                printf("您选择的是DELETE\n");
                break;
            }
            case QUIT :
            {
                printf("您选择的是QUIT\n");
                exit(OUT_TEST);
            }
            default:
            {
                printf("您的输入有误,请重新输入\n");
            }

        }
       
    }
    return 0;
}

原创粉丝点击