**制作一個電子通訊錄,通過該通訊錄能錄入好友ID號、姓名(英文)、手
機號碼,家庭住址,公司電話。**
原理:分成5個模塊,將模塊功能實現寫入頭文件中。主函數部分代碼:
顯示函數部分,在Markdown里對不齊,意思就這樣,將就一下=。=
/*******************************************************************
需求:制作一個電子通訊錄,通過該通訊錄能錄入好友ID號、姓名(英文)、手
機號碼,家庭住址,公司電話。
模塊:主界面:主要顯示軟件功能,A)添加好友信息 B)列表好友信息。(包含排序功能) C)搜索好友 D)刪除好友A)用戶輸入INSERT命令后,讓用戶輸入好友信息。添加成功或失敗都需提示B)用戶輸入DISPLAY命令后,好友信息升序排列C)用戶輸入SEARCH命令后,讓用戶輸入將要搜索好友姓名查詢。如果未搜索到請友好提示。如果搜索到,顯示處該好友信息D)用戶輸入DELETE命令后,讓用戶輸入將要刪除好友姓名刪除,如果存在同名的多個好友,則列表出,所有同名的好友信息,讓用戶通過輸入ID號刪除提示用戶刪除成功。
**********************************************************************/
#include "head.h"int main ()
{int Function;int i = 0;char Name[N];int cho;PNode head_node = (PNode) malloc(sizeof(Node)/sizeof(char));if (NULL == head_node){return MALLOC_ERROR;}head_node->next = NULL;while (1){Interface_Display ();scanf ("%d", &Function);switch (Function) // 功能選擇{case 1: // 添加好友{Function = 0;Add_Friend (head_node, i++);int j;printf ("\t正在添加\n");printf ("\t請稍候");fflush (stdout); // 強制刷新緩存,輸出顯示for (j = 0; j < 3; j++){sleep (1); // Linux 使用sleep,參數為秒printf (".");fflush (stdout); // 強制刷新緩存,輸出顯示}printf ("\n");printf ("\t添加成功!\n");printf ("\t返回主菜單請輸入1:");scanf ("%d", &cho);if (1 == cho){break;}else{printf ("\t對不起!您的輸入有誤!請重新輸入:");scanf ("%d", &cho);break;}break;} case 2: // 顯示好友信息{system ("clear");printf ("\t*********好友信息********\n");printf ("\n");Friend_Information (head_node);Function = 0;printf ("\t返回主菜單請輸入1:");scanf ("%d", &cho);if (1 == cho){break;}else{printf ("\t對不起!您的輸入有誤!請重新輸入:");scanf ("%d", &cho);break;}break;}case 3: // 查找好友{system ("clear");printf ("\t*************查找好友*************\n");printf ("\t請輸入您要查找的好友姓名:");scanf ("%s", Name);printf ("\n");int j; printf ("\t正在查找\n");printf ("\t請稍候");fflush (stdout); // 強制刷新緩存,輸出顯示for (j = 0; j < 3; j++){sleep (1); // Linux 使用sleep,參數為秒printf (".");fflush (stdout); // 強制刷新緩存,輸出顯示}printf ("\n");Search_Friend (head_node, Name);printf ("\t返回主菜單請輸入1:");scanf ("%d", &cho);if (1 == cho){break;}else{printf ("\t對不起!您的輸入有誤!請重新輸入:");scanf ("%d", &cho);break;}break;}case 4: //刪除好友{system ("clear");printf ("\t*************刪除好友*************\n");printf ("\t請輸入要刪除好友的姓名:");scanf ("%s", Name);printf ("\n");Delete_Friend (head_node, Name);printf ("\t返回主菜單請輸入1:");scanf ("%d", &cho);if (1 == cho){break;}else{printf ("\t對不起!您的輸入有誤!請重新輸入:");scanf ("%d", &cho);break;}break;} case 5: //退出通訊錄{Function = 0;system ("clear");exit (0);}default: //輸入有誤{Function = 0;printf ("\t對不起!您的輸入有誤!請重新輸入:");scanf ("%d", &Function);break;} } } return 0;
}
head.h部分:
#ifndef HEAD_H_
#define HEAD_H_#include <stdlib.h>
#include <stdio.h>
#include <unistd.h> // sleep函數頭文件#define uint unsigned int
#define OK 0
#define ERROR -1
#define MALLOC_ERROR -2
#define N 20 typedef int ElementType;
typedef struct node
{ElementType ID; // ID號char Name [N]; // 姓名char Mobile_Phone [N]; // 手機號碼char Home_Address [N]; // 家庭住址char Company_Tell [N]; // 公司電話struct node* next; // 節點指針
}Node;
typedef Node* PNode; //重命名節點指針類型//顯示操作界面
int Interface_Display ();//添加好友信息 (尾插法)
int Add_Friend (PNode head, ElementType num);//顯示所有好友信息
int Friend_Information (PNode head);//查找好友
int Search_Friend (PNode head, char* Name);//刪除好友
void Delete_Friend (PNode head, char* Name);#endif
head.c的代碼:
#include "head.h"//顯示操作界面
int Interface_Display ()
{system ("clear");printf ("\t************************************** \n");printf ("\t~ 歡迎使用通訊錄 ~\n");printf ("\t~ ~\n");printf ("\t~ 1 >>>>>>>> 添加好友信息 ~\n");printf ("\t~ 2 >>>>>>>> 列表好友信息 ~\n");printf ("\t~ 3 >>>>>>>> 搜索好友 ~\n");printf ("\t~ 4 >>>>>>>> 刪除好友 ~\n");printf ("\t~ 5 >>>>>>>> 退出 ~\n");printf ("\t~ ~\n");printf ("\t~ ~\n");printf ("\t~ 作者:believe ~\n");printf ("\t~*************************************~\n");printf (" \n");printf (" \n");printf ("\t請輸入對應數字選擇相應功能:");
}//添加好友信息 (尾插法)
int Add_Friend (PNode head, ElementType num)
{if (NULL == head){return ERROR;}//創建一個新的結點PNode p = (PNode) malloc(sizeof(Node)/sizeof(char));if (NULL == p){return MALLOC_ERROR;}//將新數據賦給新結點system("clear"); printf ("\t*************添加好友***************\n");p->ID = num;printf ("\t好友的ID為:%d\n", p->ID);printf ("\n");printf ("\t請輸入好友的名字:");scanf ("%s", p->Name);printf ("\n");printf ("\t請輸入好友的手機號:");scanf ("%s", p->Mobile_Phone);printf ("\n");printf ("\t請輸入好友的家庭住址:");scanf ("%s", p->Home_Address);printf ("\n");printf ("\t請輸入好友的公司電話:");scanf ("%s", p->Company_Tell);printf ("\n");p->next = NULL;//找到最后一個結點PNode Ptmp; //將頭結點地址給臨時指針PtmpPtmp = head;while (Ptmp->next){Ptmp = Ptmp->next;}Ptmp->next = p;return OK;
}//顯示所有好友信息
int Friend_Information (PNode head)
{if (NULL == head){return ERROR;}PNode p = head->next;printf ("\tID\t姓名\t\t手機號\t\t住址\t\t\t公司電話\n");while (p){printf ("\t%d\t%s\t\t%s\t\t%s\t\t\t%s\n", p->ID,p->Name, p->Mobile_Phone, p->Home_Address, p->Company_Tell);p = p->next;}putchar('\n');return OK;
}//查找好友
int Search_Friend (PNode head, char* Name) //通過名字查找好友
{PNode p = head;PNode q = NULL;if ((NULL != p) && NULL != (p->next)){while (p->next) {q = p->next;if ((NULL != q) && 0 == (strcmp(q->Name, Name))){printf ("\t好友信息: \n\tID:%d\n\t姓名: %s\n\t手機號碼: %s\n\t家庭地址:%s\n\t公司電話: %s\n", q->ID, q->Name, q->Mobile_Phone, q->Home_Address, q->Company_Tell);}else{printf ("\t對不起,您的通訊錄沒有該好友!\n");}p = p->next;}}/* 另一種做法if (NULL == head){return ERROR;}PNode p;int flag = 1;for (p = head->next; p != NULL; p = p->next){if (0 == strcmp(p->Name, Name)){flag = 0;printf ("\t好友信息:\n\tID: %d\n\t姓名: %s\n\t手機號碼: %s\n\t家庭地址: %s\n\t公司電話: %s\n", p->ID, p->Name, p->Mobile_Phone, p->Home_Address, p->Company_Tell);}}fi (flag){printf ("\t對不起,您的通訊錄沒有該好友!\n");}putchar('\n');*/return OK;
}//刪除好友
void Delete_Friend (PNode head, char* Name)
{PNode p = head;PNode q = NULL;while (NULL != p && NULL != (p->next)){q = p->next;if (NULL != q && 0 == strcmp(q->Name, Name)){p->next = q->next;free(q);int j;printf ("\t正在刪除\n");printf ("\t請稍候");fflush (stdout); //強制刷新緩存,輸出顯示for (j = 0; j < 3; j++){sleep (1); //linux使用sleep,參數為秒printf (".");fflush(stdout); //強制刷新緩存,輸出顯示}printf ("\n");printf ("\t該好友已成功刪除!\n");}else if (NULL == q->next && 0 != strcmp(q->Name, Name)){printf ("\t您的通訊錄沒有該好友!\n");}p = p->next;}
}