//Massage.h文件
#ifndef __MASSAGELIST_H__
#define __MASSAGELIST_H__ #define unit unsigned int #define PASS 0 #define ERROR -1 #define MALLOC_ERROR -2 #define N 40 typedef int LinkData; typedef struct node { LinkData ID; char Name [N]; char Mobile_Phone [N]; char Home_Address [N]; char Company_Phone [N]; struct node *next; //节点指针 }Node; typedef Node *PNode; //重命名节点指针类型 int Time_Display(); //定义时间函数 int Menu(); //显示界面 int Insert_Last(PNode head, LinkData data); //尾插法添加好友 int Get_Information(PNode head) ; //显示好友信息 int Find_Element(PNode head, char *Name); //查找好友 void Delete_Friend(PNode head, char *Name); //删除好友#endif
//Massage.c文件
#include "Massage.h" #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <time.h> int Menu() { system ("clear"); printf ("\t************************************************\n"); printf ("\t* *\n"); printf ("\t* wellcome to use Massage list *\n"); printf ("\t* *\n"); printf ("\t* 1 --------> add friend info *\n"); printf ("\t* 2 --------> list friend info *\n"); printf ("\t* 3 --------> search friend info *\n"); printf ("\t* 4 --------> delete friend *\n"); printf ("\t* 5 --------> exit *\n"); printf ("\t* *\n"); printf ("\t* auther:yuhuofei *\n"); printf ("\t************************************************\n"); printf (" \n"); printf("\t"); Time_Display(); printf (" \n"); printf ("\tPlease enter the num to choose function:"); } // 时间显示函数 int Time_Display() { time_t timep; struct tm *p; time(&timep); /*获得time_t结构的时间,UTC时间*/ p = localtime(&timep); /*转换为struct tm结构的当地时间*/ printf("Time: M/d/d ", 1900+p->tm_year, 1+p->tm_mon, p->tm_mday); printf("d:d:d\n", p->tm_hour, p->tm_min, p->tm_sec); return 0; } //添加好友信息 int Insert_Last(PNode head, LinkData data) { // 创建新的结点 if(head == NULL) { return ERROR; } PNode p = (PNode)malloc(sizeof(Node)/sizeof(char)); if (p == NULL) { return MALLOC_ERROR; } // 将新数据赋给新结点 system ("clear"); printf ("\t********************Add friend********************\n"); p->ID = data; printf ("\tFriend ID is:%d\n", p->ID); printf ("\n"); printf ("\tPlease input your friend name:"); scanf ("%s", p->Name); printf ("\n"); printf ("\tPlease input your friend phone number:"); scanf ("%s", p->Mobile_Phone); printf ("\n"); printf ("\tPlease input your friend home address:"); scanf ("%s", p->Home_Address); printf ("\n"); printf ("\tPlease input your friend company phone:"); scanf ("%s", p->Company_Phone); printf ("\n"); p->next = NULL; //找到最后一个结点 PNode Ptemp; //将头结点地址给临时指针Ptemp Ptemp = head; while (Ptemp->next) { Ptemp = Ptemp->next; } Ptemp->next = p; return PASS; } //显示好友的信息 int Get_Information(PNode head) { if(head == NULL) { return ERROR; } PNode p; printf ("\tID\tName\t\tPhone number\t\tAddress\t\tCcmpany phone\n"); for (p = head->next; p != NULL; p = p->next) { printf ("\t%d\t%s\t\t%s\t\t\t%s\t\t%s\n", p->ID, p->Name, p->Mobile_Phone, p->Home_Address, p->Company_Phone); } printf("\n"); return PASS; } //查找信息 int Find_Element(PNode head, char *Name) { if(head == NULL) { return ERROR; } PNode p; int flag = 1; for (p = head->next; p != NULL; p = p->next) { if (strcmp(p->Name,Name) == 0) { flag = 0; printf ("\tFriend information:\n\tID: %d\n\tName: %s\n\tPhone number: %s\n\tAddress: %s\n\tCompany phone:%s\n", p->ID, p->Name, p->Mobile_Phone, p->Home_Address, p->Company_Phone); } } if (flag) { printf ("\tSorry,This name doesn't in it!\n"); } printf("\n"); return PASS; } //删除好友 void Delete_Friend(PNode head, char *Name) { PNode p = head; PNode q = NULL; while (p != NULL && (p->next) != NULL) { q = p->next; if (q != NULL && strcmp(q->Name,Name) == 0) { p->next = q->next; free(q); int j; printf ("\tDeleting\n"); printf ("\tPlease hold on"); fflush(stdout); //强制刷新缓存,输出显示 for( j=0;j<3;j++ ) { sleep(1); //使用sleep,参数:s printf("."); fflush(stdout); //强制刷新缓存,输出显示 } printf ("\n"); printf ("\tSucceed!\n"); } else if (q->next == NULL && strcmp(q->Name,Name) != 0) { printf ("\tThis name doesn't in it!\n"); } p = p->next; } }
//main.c文件
#include <stdio.h> #include "Massage.h" #include "stdlib.h" int main () { int Function; int i = 0; char Name[N]; int cho; PNode head_node = (PNode)malloc(sizeof(Node)/sizeof(char)); if(head_node == NULL) { return MALLOC_ERROR; } head_node->next = NULL; while(1) { Menu(); scanf ("%d", &Function); switch (Function) //功能选择 { case 1://添加好友 { Function = 0; Insert_Last (head_node,i++); int j; printf ("\tAddinng\n"); printf ("\tPlease hold on"); fflush(stdout); //强制刷新缓存,输出显示 for( j=0;j<3;j++ ) { sleep(1); //linux 使用sleep,参数为秒 printf("."); fflush(stdout); //强制刷新缓存,输出显示 } printf ("\n"); printf ("\tSucceed!\n"); printf ("\tBack the menu,please entre 1:"); scanf ("%d",&cho); if (cho == 1) { break; } else { printf ("\tSorry!your input is error,please try again:"); scanf ("%d", &cho); break; } break; } case 2://显示好友信息 { system ("clear"); printf ("\t********************Friend Information********************\n"); printf ("\n"); Get_Information (head_node); Function = 0; printf ("\tBack the menu,please entre 1:"); scanf ("%d",&cho); if (cho == 1) { break; } else { printf ("\tSorry!your input is error,please try again:"); scanf ("%d", &cho); break; } break; } case 3://查找好友 { system ("clear"); printf ("\t********************Search Friend********************\n"); printf ("\tPlease input your friend name: "); scanf ("%s", Name); printf ("\n"); int j; printf ("\tSearching\n"); printf ("\tPlease hold on"); fflush(stdout); //强制刷新缓存,输出显示 for( j=0;j<3;j++ ) { sleep(1); //linux 使用sleep,参数为秒 printf("."); fflush(stdout); //强制刷新缓存,输出显示 } printf ("\n"); Find_Element(head_node,Name); printf ("\tBack the menu,please entre 1:"); scanf ("%d",&cho); if (cho == 1) { break; } else { printf ("\tSorry!your input is error,please try again:"); scanf ("%d", &cho); break; } break; } case 4://删除好友 { system ("clear"); printf ("\t********************Delete Friend********************\n"); printf ("\tPlease input name taht you want delete:"); scanf ("%s", Name); printf ("\n"); Delete_Friend(head_node,Name); printf ("\tBack the menu,please entre 1:"); scanf ("%d",&cho); if (cho == 1) { break; } else { printf ("\tSorry!your input is error,please try again:"); scanf ("%d", &cho); break; } break; } case 5://退出通讯录 { Function = 0; printf ("\n"); system ("clear"); exit(0); } default://输入错误 { Function = 0; printf ("\tSorry!your input is error,please try again:"); scanf ("%d", &Function); printf ("%d", Function); break; } } } return 0;
运行效果如下: }