C语言项目——通讯录

来源:互联网 发布:js mvvm框架有哪些 编辑:程序博客网 时间:2024/06/05 08:49

那个,我为了添加娱乐效果,在程序中加入了一些哲学♂,大家复制的时候记得改一下printf里的内容即可

首先是头文件

#ifndef _TEST_H__#define _TEST_H__#define FALSE  0#define TRUE   1typedef struct friend //朋友相关信息的结构体{    int       ID;    char      name[20];    char      Mobile_Phone[20];    char      Add[30];    char      Company_Phone[20];}Friend;typedef struct _node{    Friend data;    struct _node * next;}Node;Node * Creat_List();void menu();int Insert_Last(Node *h,int id);//添加好友int Delete_Name (Node *,char name[]);//以名字删除好友int Delete_ID (Node *h);//ID删除好友int Find_Element(Node *h,char name[]);//找好友void Rank (Node *h);//好友排序void Display(); //打印好友信息#endif

接着是实现函数

#include <stdio.h>#include <stdlib.h>#include <string.h>#include "test.h"//创建链表Node * Creat_List(){    Node *list = (Node*)malloc(sizeof(Node)/sizeof(char));    if (list == NULL)        return NULL;    list->next = NULL; //空的链表    return list;}//菜单界面void menu(){    system("clear");    printf ("********************************\n");    printf ("        欢迎来到基友之家        \n");    printf ("******   1.添加基友信息   ******\n");    printf ("******   2.列表基友信息   ******\n");    printf ("******   3.搜 索 基 友    ******\n");    printf ("******   4.删 除 基 友    ******\n");    printf ("******   5.退 出 系 统    ******\n");    printf ("                                \n");    printf ("******              作者:小码农\n");    printf ("********************************\n");    printf ("                                \n");    printf ("                                \n");    printf ("请输入需要的功能:");}//添加好友信息int Insert_Last(Node *h,int id){    if (h == NULL)        return FALSE;    Node *node = (Node*)malloc(sizeof(Node)/sizeof(char));    if (node == NULL)    {        return FALSE;    }    node->data.ID = id;    node->next = NULL;    printf ("请输入好友姓名    :");    scanf ("%s",node->data.name);    printf ("请输入好友手机号码:");    scanf ("%s",node->data.Mobile_Phone);    printf ("请输入好友地址    :");    scanf ("%s",node->data.Add);    printf ("请输入好友公司号码: ");    scanf ("%s",node->data.Company_Phone);    Node* tmp = h;    while (tmp->next)    {        tmp = tmp->next;    }    tmp->next = node;    printf ("\n");    printf ("添加好友成功\n");    printf ("\n");    printf ("ID      : %d\n",node->data.ID);    printf ("姓名    :%s\n",node->data.name);    printf ("手机    :%s\n",node->data.Mobile_Phone);    printf ("地址    :%s\n",node->data.Add);    printf ("公司号码:%s\n",node->data.Company_Phone);    return TRUE;}//找基友int Find_Element(Node* h, char name[]){    if (h == NULL)    {        printf ("当前没有交到基友,请继续努力\n");        return FALSE;    }    Node *tmp = h->next;    int k = 1;    while (tmp)    {        if (strcmp(tmp->data.name,name) == 0)        {            printf ("ID      : %d\n",tmp->data.ID);            printf ("姓名    :%s\n",tmp->data.name);            printf ("手机    :%s\n",tmp->data.Mobile_Phone);            printf ("地址    :%s\n",tmp->data.Add);            printf ("公司号码:%s\n",tmp->data.Company_Phone);            printf ("\n");            k = 0;        }        tmp = tmp->next;    }    if (k)    {        printf("你没有这么一个基友\n");        return FALSE;    }}//删除基友int Delete_Name(Node* h, char name[]){    if (h == NULL)        return FALSE;    Node *tmp = h;    int flag = 0;    while (tmp->next)    {        if (strcmp(tmp->next->data.name,name) == 0)        {            flag++;        }        tmp = tmp->next;    }    if (flag == 0)    {        printf ("咦,你没有这个基友\n");        return FALSE;    }    else if (flag == 1)    {        int flag =0;        tmp = h;        while (tmp->next)        {            if (strcmp(tmp->next->data.name,name) == 0)            {                break;            }        }        Node *p = tmp->next;        tmp->next = p->next;        free (p);        printf ("删除成功\n");    }    else    {        Find_Element(h,name);        Delete_ID(h);    }    return TRUE;}int Delete_ID(Node *h){    if (h == NULL)        return FALSE;    printf ("请输入删除的ID:");    int id;    scanf ("%d",id);    Node *tmp = h;    while (tmp->next)    {        if (tmp->next->data.ID == id)            break;        tmp = tmp->next;    }    tmp = h;    if (tmp->next == NULL)    {        printf ("没有这个ID\n");        return FALSE;    }    Node *p = tmp->next;    tmp->next = p->next;    free (p);    printf ("删除成功\n");    return TRUE;}void Rank (Node *h){    if (h == NULL)        return;}void Display (Node *h){    if (h == NULL)        return;    if (h->next == NULL)    {        printf ("当前没有交到基友,请继续努力\n");        return;    }    Node *tmp = h->next;    while(tmp)    {        printf ("ID      : %d\n",tmp->data.ID);        printf ("姓名    :%s\n",tmp->data.name);        printf ("手机    :%s\n",tmp->data.Mobile_Phone);        printf ("地址    :%s\n",tmp->data.Add);        printf ("公司号码:%s\n",tmp->data.Company_Phone);        printf ("\n");        tmp = tmp->next;    }    printf ("\n");}

最后是主函数

#include <stdio.h>#include "test.h"int main(){    char ch;    Node *head = Creat_List();    if (head == NULL)    {        printf ("ERROR\n");        return FALSE;    }    int i = 1;    while (i)    {        menu();        int num;        scanf ("%d",&num);        while (getchar() != '\n');        switch(num)        {            case 1:            {                printf ("添加基友");                printf ("\n");                printf ("请添加你的基友信息:\n");                Insert_Last (head,i);                printf ("\n");                scanf("%c",&ch);                scanf("%c",&ch);                i++;                break;            }            case 2:            {                printf ("您的基友们\n");                printf ("\n");                Display(head);                scanf("%c",&ch);                break;            }            case 3:            {                char name[20];                printf ("搜索基友");                printf ("\n");                printf ("输入你的基友名字:");                scanf  ("%s",name);                Find_Element(head,name);                printf ("\n");                scanf("%c",&ch);                break;            }            case 4:            {                char name[20];                printf ("请输入你要删除的基友名字:");                scanf ("%s",name);                Delete_Name (head,name);                scanf("%c",&ch);                break;            }            case 5:            {                printf ("谢谢使用,记得多交基友,散播哲学!\n");                i = 0;                break;            }            default:            {                printf ("错误指令!\n");                scanf("%c",&ch);                break;            }        }    }    return 0;}

把这三个代码分别放在创建好的test.h,test.c和main.c文件中,在一起运行就可以进行操作了。里面有一些♂奇怪的词语请不要在意,纯属娱乐。

原创粉丝点击