C语言————实现简单通讯录

来源:互联网 发布:java 去除图片水印 编辑:程序博客网 时间:2024/06/04 20:28

电子通讯录,通过该通讯录能存入好友ID 号、姓名(英文)、手机号码、家庭住址、公司电话。
模块
                               主界面:主要显示软件功能。
                                                 A)添加好友信息。
                                                 B)列表好友信息。(包含排序功能)
                                                 C)搜索好友
                                                 D)删除好友
添加好友:
               用户输入 INSERT 命令后,让用户输入好友信息。添加成功或失败都需要提示用户
列表好友:
              用户输入 DISPLAY 命令后,好友信息升序排列。
搜索好友:
               用户输入 SEARCH 命令后,让用户输入将要搜索好友姓名查询。如果未搜索到请友好提示。如果搜索到,显示出该好友信息。
删除好友:
              用户输入 DELETE 命令后,让用户输入将要删除好友姓名删除,如果存在同名的多个好友,则列表出,所有同名的好友信息,让用户通过输入 ID 号删除。

              提示用户删除成功。

//Addreess.h(头文件)

#ifndef __ADDRESS_H__#define __ADDRESS_H__#define FALSE 0#define TRUE  1#define N 20typedef struct _information{int  ID;char name[N];char H_num[N];char home[N];char o_num[N];}LinkData;typedef struct _node{LinkData data;struct _node *next;}Node;//创建链表Node *Create_List();//菜单void Menu();//添加好友void Add(Node *h,int ID);//列表好友void List(Node *h);//查找好友void Search(Node* h);//删除好友void Delete(Node* h);int Delete_Pos(Node* h, int pos);int Clean_List(Node * h);int Destroy(Node *h);#endif
//Address.c(函数实现部分)
#include <stdio.h>#include "Address.h"#include <stdlib.h>#include <string.h>//创建链表Node *Create_List(){Node *list = (Node*)malloc(sizeof(Node)/sizeof(char));if (list == NULL)return NULL;list->next = NULL;return list;}//菜单void Menu(){printf ("\t\t\t\t\t\t\t **********************************************\n");printf ("\t\t\t\t\t\t\t *                                            *\n");printf ("\t\t\t\t\t\t\t*               通讯录主菜单\t\t\t*\n");printf ("\t\t\t\t\t\t\t *                                            *\n");printf ("\t\t\t\t\t\t\t*\t1)添加好友信息  \t\t\t*\n");printf ("\t\t\t\t\t\t\t *                                            *\n");printf ("\t\t\t\t\t\t\t*\t2)列表好友信息  \t\t\t*\n");printf ("\t\t\t\t\t\t\t *                                            *\n");printf ("\t\t\t\t\t\t\t*\t3)搜索好友      \t\t\t*\n");printf ("\t\t\t\t\t\t\t *                                            *\n");printf ("\t\t\t\t\t\t\t*\t4)删除好友      \t\t\t*\n");printf ("\t\t\t\t\t\t\t *                                            *\n");printf ("\t\t\t\t\t\t\t*\t5)退出          \t\t\t*\n");printf ("\t\t\t\t\t\t\t *                                            *\n");printf ("\t\t\t\t\t\t\t **********************************************\n");printf ("\n\n\t\t\t\t\t\t\t请输入操作指令(1,2,3,4)\n");printf ("\n\t\t\t\t\t\t\t操作指令:");}//添加好友void Add(Node *h,int ID){if (h == NULL){printf ("\t\t添加好友信息失败\n");return;}Node *node = (Node *)malloc(sizeof(Node)/sizeof(char));if (node == NULL){printf ("\t\t添加好友信息失败\n");return;}         //自定生成ID···再改node->data.ID = ID++;printf ("\t\t请输入好友姓名(英文):\t");scanf ("%s",node->data.name);printf ("\t\t请输入好友手机号码:  \t");scanf ("%s",&node->data.H_num);printf ("\t\t请输入好友家庭住址:  \t");scanf ("%s",node->data.home);printf ("\t\t请输入好友公司电话:  \t");scanf ("%s",&node->data.o_num);getchar();node->next = NULL;node->data.ID = ID;Node* tmp = h;while (tmp->next){tmp = tmp->next;}tmp->next = node;printf ("\n\t\t添加好友信息成功\n");}/* //升序排序void sort(Node *h){if (h == NULL || h->next == NULL ||h->next->next == NULL)return ;int i,j;Node *p1 = h->next;Node *p2 = h->next->next;int count = 0;while (p1){count++;p1 = p1->next;}p1 = h->next;// 外层控制比较的轮数 for (i = 0; i < count - 1; i++){// 内层循环控制每一轮比较需要比较的次数for (j = 0; j < count-i-1; j++){LinkData tmp;if (p1->data.ID > p2->data.ID){//应该交换结点中的数据tmp = p1->data;p1->data= p2->data;p2->data = tmp;}p1 = p1->next;p2 = p2->next;}p1 = h->next;p2 = h->next->next;}}*///列表好友void List(Node *h){if (h == NULL){printf ("\t\t\t\t\t\t\t无法显示好友列表\n");return;}Node *tmp = h->next;printf ("\t\t\t\t\t\t\t全部好友信息\n\n\n");while (tmp){printf ("\t\t好友的ID:%010d\t", tmp->data.ID);printf ("好友姓名:%-10s\t", tmp->data.name);printf ("手机号码:%-11s\t", tmp->data.H_num);printf ("家庭住址:%-15s\t", tmp->data.home);printf ("公司电话:%-11s\t", tmp->data.o_num);printf ("\n");tmp = tmp->next;}getchar();}//查找好友void Search(Node* h){if (h == NULL){printf("\n\t\t无法查找好友\n");return ;}char str[N];printf ("\n\t\t请输入好友姓名:");scanf ("%s",str);printf ("\n");Node *tmp = h->next;int count = 0;while (tmp){    if (strcmp(tmp->data.name,str) == 0){count++;printf ("\t\t好友ID: %010d\t", tmp->data.ID);printf ("好友姓名:%-10s\t", tmp->data.name);printf ("手机号码:%011d\t", tmp->data.H_num);printf ("家庭住址:%-15s\t", tmp->data.home);printf ("公司电话:%011d\t", tmp->data.o_num);printf ("\n");}tmp = tmp->next;}if (count == 0){printf ("\n\t\t无此好友请重新查找\n");}getchar();}//删除好友void Delete(Node* h){if (h == NULL || h->next == NULL){printf("\n\t\t无法删除好友\n");return ;}int ID;char str[N];printf ("\n\t\t请输入要删除好友的名字: ");scanf ("%s",str);printf ("\n");Node *tmp = h;Node *p = NULL;int count = 0;while (tmp->next){if (strcmp(tmp->next->data.name,str) == 0){count++;if (count == 1) {p = tmp;}}tmp = tmp->next;}if (count == 0){printf ("\n\t\t查无此好友请重新删除\n");getchar();return;}if (count == 1){printf ("\n\t\t要删除的要有信息为:\n");printf ("\n\t\t好友的ID:%010d\t", p->next->data.ID);printf ("好友姓名:%-10s\t", p->next->data.name);printf ("手机号码:%-11s\t", p->next->data.H_num);printf ("家庭住址:%-15s\t", p->next->data.home);printf ("公司电话:%-11s\t", p->next->data.o_num);printf ("\n");getchar();printf ("\n\t\t请按 ENTER键 删除");getchar();tmp = p;p = tmp->next;tmp->next = p->next;free(p);printf("\n\t\t好友删除成功\n");}else{printf ("\n\t\t%s好友有多个\n",str);tmp = h;while (tmp->next){if (strcmp(tmp->next->data.name,str) == 0){printf ("\n\t\t好友的ID:%010d\t", tmp->next->data.ID);printf ("好友姓名:%-10s\t", tmp->next->data.name);printf ("手机号码:%-11s\t", tmp->next->data.H_num);printf ("家庭住址:%-15s\t", tmp->next->data.home);printf ("公司电话:%-11s\t", tmp->next->data.o_num);printf ("\n");}tmp = tmp->next;}printf ("\n\t\t请输入好友ID号删除: ");scanf ("%d",&ID);getchar();p = NULL;tmp = h;while (tmp->next){if (tmp->next->data.ID == ID){p = tmp;}tmp = tmp->next;    }tmp = p;p = tmp->next;tmp->next = p->next;free(p);printf("\n\t\t好友删除成功\n");}return ;}int Delete_Pos(Node* h, int pos){if (h == NULL || pos < 1)return FALSE;// 找要插入位置的前一个结点Node *tmp = h;int i;for (i = 0; i < pos-1; i++){if (tmp->next == NULL)break;tmp = tmp->next;}if (tmp->next == NULL)   // 越界{printf("删除位置越界\n");return FALSE;}Node *p = tmp->next;tmp->next = p->next;free(p);return TRUE;}int Clean_List(Node * h){if (h == NULL)return FALSE;Node *tmp = h;while (tmp->next){Delete_Pos(h, 1);}return 0;}int Destroy(Node *h){if (h == NULL)return FALSE;Clean_List(h);free(h);return TRUE;}
//main()函数
//通讯录能存入好友ID 号、姓名(英文)、手机号码、家庭住址、公司电话。
#include <stdio.h>#include "Address.h"#include <string.h>#include <stdlib.h>int main(){system("clear");Node* head = Create_List();if (head == NULL){printf ("创建通讯录失败\n");return -1;}int i;Node *node = NULL;Node* tmp = NULL;for (i = 0; i < 3; i++){node = (Node *)malloc(sizeof(Node)/sizeof(char));    if (node == NULL){printf ("创建通讯录失败\n");return  -1;}node->data.ID    = 1 + i;strcpy (node->data.name,"123zhangsan"+i);strcpy (node->data.H_num,"12345678");strcpy (node->data.home,"123jiangsu"+i);strcpy (node->data.o_num,"12387654");node->next = NULL;tmp = head;while (tmp->next){tmp = tmp->next;}tmp->next = node;}int ID = 2;int num;while(num != 5){system("clear");Menu();scanf ("%d",&num);printf ("\n");switch (num){case 1:      //添加好友,并完成排序ID++;Add(head,ID);printf ("\n\t\t\t\t\t\t\t请输入 ENTER 键返回主菜单");       break;case 2:      //打印好友信息List(head);printf ("\n\t\t\t\t\t\t\t请输入 ENTER 键返回主菜单");break;case 3:      //查找好友Search(head);printf ("\n\t\t\t\t\t\t\t请输入 ENTER 键返回主菜单");break;case 4:      //删除好友Delete(head);printf ("\n\t\t\t\t\t\t\t请输入 ENTER 键返回主菜单");break;default://printf ("\n\t\t\t\t\t\t\已退出通讯录");break;} getchar();}//system("clear");Destroy(head);return 0;}




原创粉丝点击