本文实例为大家分享了C语言实现电子通讯录的具体代码,供大家参考,具体内容如下
制作一个电子通讯录,通过该通讯录能录入好友ID号、姓名(英文)、手
机号码,家庭住址,公司电话。**
原理:分成5个模块,将模块功能实现写入头文件中。主函数部分代码:
显示函数部分,在Markdown里对不齐,意思就这样,将就一下=。=
#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#include #include // 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为:%dn", 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; //将头结点地址给临时指针Ptmp
Ptmp = 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 ("tIDt姓名tt手机号tt住址ttt公司电话n");
while (p)
{
printf ("t%dt%stt%stt%sttt%sn", 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好友信息: ntID:%dnt姓名: %snt手机号码: %snt家庭地址:%snt公司电话: %sn", q->ID, q->Name, q->Mobile_Phone, q->Home_Address, q->Company_Tell);
}
else
{
printf ("t对不起,您的通讯录没有该好友!n");
}
p = p->next;
}
}
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;
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。



