C语言通讯录管理系统
本文转载自:http://blog.csdn.net/hackbuteer1/article/details/6573488
实现了通讯录的录入信息、保存信息、插入、删除、排序、查找、单个显示等功能。。
完整的代码如下:
#include <stdio.h>
#include <malloc.h> //得到指向大小为Size的内存区域的首字节的指针//
#include <string.h>
#include <stdlib.h> //标准库函数//
#define NULL 0
#define LEN sizeof(struct txlproject) //计算字节//
int n;
struct txlproject
{
char name[30]; //名字
char work[30]; //职业
char handset[30]; //手机
char email[30]; //电子邮件
char address[30]; //通讯地址
struct txlproject *next;
};
struct txlproject *shifang(struct txlproject *head); // 释放内存函数声明
//创建函数,不带头结点的链表
struct txlproject *creat(void)
{
struct txlproject *head,*p1,*p2;
char name[20];
n=0;
p1=(struct txlproject *)malloc(LEN);
p2=p1; //强制内存转换
printf("请输入通讯录的内容!/n姓名输入为0时表示创建完毕!/n");
printf("请输入姓名:");
gets(name);
if(strcmp(name,"0")!=0)
{
strcpy(p1->name,name);
printf("请输入职业:"); gets(p1->work);
printf("请输入手机:"); gets(p1->handset);
printf("请输入电子邮件:"); gets(p1->email);
printf("请输入通讯地址:"); gets(p1->address);
head=NULL;
while(1)
{
n=n+1; //记录通讯录人数个数
if(n==1)
head=p1;
else
p2->next=p1;
p2=p1;
printf("请输入姓名:");
gets(name);
if(strcmp(name,"0")==0)
{
break;
}
else
{
p1=(struct txlproject *)malloc(LEN);
strcpy(p1->name,name);
printf("请输入职业:"); gets(p1->work);
printf("请输入手机:"); gets(p1->handset);
printf("请输入电子邮件:"); gets(p1->email);
printf("请输入通讯地址:"); gets(p1->address);
}
}
p2->next=NULL;
return head;
}
else
return 0;
}
//输出函数
void print(struct txlproject *head)
{
struct txlproject *p;
if(head!=NULL)
{
p=head;
printf("本通讯录现在共有%d人:/n",n);
printf("---姓名-------职业--------手机-------Email-------通讯地址/n");
printf("==================================/n");
do
{
printf("== %s",p->name); printf(" ");
printf("%s",p->work); printf(" ");
printf("%s",p->handset); printf(" ");
printf("%s",p->email); printf(" ");
printf("%s",p->address); printf(" /n");
p=p->next;
}while(p!=NULL);
printf("==================================/n");
}
else
printf("通讯录为空,无法输出!/n");
}
//增加函数
struct txlproject *insert(struct txlproject *head)
{
struct txlproject *p0,*p1,*p2;
char name[20];
p1=head;
printf("请输入增加的内容:/n");
printf("请输入姓名:"); gets(name);
if(strcmp(name,"0")==0)
{
printf("姓名不能为0,增加失败!/n");
return(head);
}
else
{
p0=(struct txlproject *)malloc(LEN);
strcpy(p0->name,name);
printf("请输入职业:"); gets(p0->work);
printf("请输入手机:"); gets(p0->handset);
printf("请输入电子邮件:"); gets(p0->email);
printf("请输入通讯地址:"); gets(p0->address);
n=n+1;
if(head==NULL)
{
head=p0;
p0->next=NULL;
return head;
}
else
{
while(strcmp(p0->name,p1->name)>0&&(p1->next!=NULL))
{
p2=p1;
p1=p1->next;
}
if(strcmp(p0->name,p1->name)<0 || strcmp(p0->name,p1->name)==0)
{
if(head==p1)
{
head=p0;
}
else
{
p2->next=p0;
}
p0->next=p1;
}
else
{
p1->next=p0;
p0->next=NULL;
}
return head;
}
}
}
struct txlproject *delete(struct txlproject *head)
{
struct txlproject *p,*q;
char name[30];
if(head==NULL)
{
printf("通讯录为空,无法显示!/n");
return head;
}
p=head;
printf("请输入需要删除的人的姓名:");
gets(name);
if(strcmp(head->name,name)==0)
{
head=head->next;
free(p);
printf("删除操作成功!/n");
return head;
}
else
{
q=head,p=head->next;
while(p!=NULL)
{
if(strcmp(p->name,name)==0)
{
q->next=p->next;
free(p);
printf("删除操作成功!/n");
return head;
}
p=p->next;
q=q->next;
}
}
}
//显示函数
struct txlproject *display(struct txlproject *head)
{
struct txlproject *p1,*p2;
char name[30];
int m;
if(head==NULL)
{
printf("通讯录为空,无法显示!/n");
return head;
}
p1=head;
m=0;
printf("请输入需要显示人的姓名:");
gets(name);
while(p1!=NULL)
{
while((strcmp(p1->name,name))!=0 && p1->next!=NULL)
{
p2=p1;
p1=p1->next;
}
if(strcmp(p1->name,name)==0)
{
m++;
printf("%s的通讯内容如下:/n",name);
printf("---姓名--------职业--------手机-------Email------通讯地址/n");
printf("==================================/n");
printf("== %s",p1->name);printf(" ");
printf("%s",p1->work);printf(" ");
printf("%s",p1->handset);printf(" ");
printf("%s",p1->email);printf(" ");
printf("%s",p1->address); printf(" /n");
printf("==================================/n");
}
p1=p1->next;
}
if(m==0)
{
printf("此人未在本通讯录中!/n");
}
return(head);
}
//排序函数
struct txlproject *paixu(struct txlproject *head)
{
struct txlproject *p1,*p2;
int i,j;
struct txlproject1
{
char name[30];
char work[30];
char handset[30];
char email[30];
char address[30];
};
struct txlproject1 px[200];
struct txlproject1 temp;
if(head==NULL)
{
printf("通讯录为空,无法排序!/n");
return(head);
}
p1=head;
for(i=0;i<n,p1!=NULL;i++)
{
strcpy(px[i].name,p1->name);
strcpy(px[i].work,p1->work);
strcpy(px[i].handset,p1->handset);
strcpy(px[i].email,p1->email);
strcpy(px[i].address,p1->address);
p2=p1;
p1=p1->next;
}
head=shifang(head);
for(j=0;j<n-1;j++)
{
for(i=j+1;i<n;i++)
{
if(strcmp(px[i].name,px[j].name)<0)
{
temp=px[i];
px[i]=px[j];
px[j]=temp;
}
}
}
p1=(struct txlproject *)malloc(LEN);
p2=p1;
strcpy(p1->name,px[0].name);
strcpy(p1->work,px[0].work);
strcpy(p1->handset,px[0].handset);
strcpy(p1->email,px[0].email);
strcpy(p1->address,px[0].address);
head=p1;
for(i=1;i<n;i++)
{
p1=(struct txlproject *)malloc(LEN);
strcpy(p1->name,px[i].name);
strcpy(p1->work,px[i].work);
strcpy(p1->handset,px[i].handset);
strcpy(p1->email,px[i].email);
strcpy(p1->address,px[i].address);
p2->next=p1;
p2=p1;
}
p2->next=NULL;
printf("按姓名排序后为:/n");
print(head);
return(head);
}
//姓名查找函数
struct txlproject *search(struct txlproject *head)
{
struct txlproject *p1,*p2;
int m;
char name[30];
if(head==NULL)
{
printf("通讯录为空,无法分类查找!/n");
return(head);
}
p1=head;
printf("********************/n");
printf("** 请输入需要查找的姓名 **/n");
printf("********************/n");
m=0;
gets(name);
while(p1!=NULL)
{
while(strcmp(p1->name,name)!=0&&p1->next!=NULL)
{
p2=p1;
p1=p1->next;
}
if(strcmp(p1->name,name)==0)
{
m++;
printf("你查找的内容是:/n");
printf("+++++++++++++++++++++++++++++++++++/n");
printf("++ %s %s %s %s %s/n",p1->name,p1->work,p1->handset,p1->email,p1->address);
printf("+++++++++++++++++++++++++++++++++++/n");
}
p1=p1->next;
if(m==0)
{
printf("此人未在本通讯录中!/n");
}
break;
}
return(head);
}
//释放内存函数
struct txlproject *shifang(struct txlproject *head)
{
struct txlproject *p1;
while(head!=NULL)
{
p1=head;
head=head->next;
free(p1);
}
return(head);
}
//文件写入函数
void save(struct txlproject *head)
{
FILE *fp;
struct txlproject *p1;
char tong[30];
if(head==NULL)
{
printf("通讯录为空,无法存储!/n");
return;
}
printf("请输入保存后的文件名:");
gets(tong);
fp=fopen("(tong).txt","w");
if(fp==NULL)
{
printf("cannot open file/n");
return;
}
p1=head;
fprintf(fp,"姓名 职业 手机 Email 通讯地址/n");
for(;p1!=NULL;)
{
fprintf(fp,"%s %s %s %s %s/n",p1->name,p1->work,p1->handset,p1->email,p1->address);
p1=p1->next;
}
printf("保存完毕!/n");
fclose(fp);
}
//文件读出函数
struct txlproject *load(struct txlproject *head)
{
FILE *fp;
char tong[30];
struct txlproject *p1,*p2;
printf("请输入要输出的文件名:");
gets(tong);
fp=fopen("(tong).txt","r");
if(fp==NULL)
{
printf("此通讯录名不存在,无法输出!/n");
return(head);
}
else
{
head=shifang(head);
}
p1=(struct txlproject *)malloc(LEN);
fscanf(fp,"%s%s%s%s%s",&p1->name,&p1->work,&p1->handset,&p1->email,&p1->address);
if(feof(fp)!=0)
{
printf("文件为空,无法打开!/n");
return(head);
}
else
{
rewind(fp);
p2=p1;
head=p1;
n=0;
while(feof(fp)==0)
{
fscanf(fp,"%s%s%s%s%s",&p1->name,&p1->work,&p1->handset,&p1->email,&p1->address);
if(feof(fp)!=0)
break;
p2->next=p1;
p2=p1;
p1=(struct txlproject *)malloc(LEN);
n=n+1;
}
p2->next=NULL;
p1=head;
head=head->next;
n=n-1;
free(p1);
print(head);
printf("打开完毕!/n");
return(head);
}
fclose(fp);
}
//综合操作函数
struct txlproject *menu(struct txlproject *head)
{
char num[10];
while(1)
{
printf("*********************/n");
printf("*** 1 姓名查找 ****/n");
printf("*** 2 单个显示 ****/n");
printf("*** 3 增加 ****/n");
printf("*** 4 退出 ****/n");
printf("*********************/n");
printf("请输入您选择的操作:");
gets(num);
switch(*num)
{
case '1':
{
head=search(head); //姓名查找
print(head);
}
break;
case '2':
{
head=display(head); //显示
}
break;
case '3':
{
head=insert(head); //增加
print(head);
}
break;
case '4':
return head;
default:
printf("操作错误,此项不存在!/n");
break;
}
if(strcmp(num,"6")==0)
break;
}
return head;
}
//主函数
void main()
{
struct txlproject *head=NULL;
char num[10];
printf("*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/n");
printf("*=* 程序说明 *=*/n");
printf("*=* 请及时保存创建完毕的通讯录内容! *=*/n");
printf("*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/n");
while(1)
{
printf("************************/n");
printf("*** 1 创建通讯录 ****/n");
printf("*** 2 按名字排序 ****/n");
printf("*** 3 综合操作 ****/n");
printf("*** 4 保存 ****/n");
printf("*** 5 打开 ****/n");
printf("*** 6 删除 ****/n");
printf("*** 7 退出 ****/n");
printf("************************/n");
printf("请输入您选择的操作:");
gets(num);
switch(*num)
{
case '1':
{
if(head==NULL)
{
head=creat(); //创建
print(head);
}
else
{
head=shifang(head);
head=creat(); //重新创建
print(head);
}
}
break;
case '2':
{
head=paixu(head); //排序
}
break;
case '3':
{
head=menu(head); //综合操作
}
break;
case '4':
{
save(head); //文件保存
print(head);
}
break;
case '5':
{
head=load(head); //文件输出
}
break;
case '6':
{
head=delete(head); //删除
print(head);
}
break;
case '7':
head=shifang(head);
break;
default:
printf("操作错误,此项不存在!/n");
break;
}
if(strcmp(num,"7")==0)
break;
}
}
C语言通讯录管理系统的更多相关文章
- c语言实现通讯录管理系统(c课程设计)
工具:Visual C++6.0 说明: 本系统基于C语言实现班级通讯录管理系统,为大一时学习C语言刚入门所做的课程设计.功能包括增.删.查.改等,非常适合初学者练手.通讯录包括的个人信息有姓名.学号 ...
- iOS学习之Object-C语言简单的通讯录管理系统
用这几天学的OC的知识,写了一个实现简单功能的通讯录管理系统,在这里分享给大家: 通讯录管理系统 * 需求: 1.定义联系人类Contact.实例变量:姓名(拼音,首字母大写).性别.电话号码.住址 ...
- C++课程设计 通讯录管理系统 原码及解析
设计题目:通信录管理系统 用C++设计出模拟手机通信录管理系统,实现对手机中的通信录进行管理. (一)功能要求 查看功能:选择此功能时,列出下列三类选择. A 办公类B 个人类C 商务类,当选中某类时 ...
- c++实现通讯录管理系统(控制台版)
c++实现通讯录管理系统(控制台版) 此项目适合c++初学者,针对c++基础知识,涉及到变量.结构体定义使用.数组定义使用.指针定义使用等. 运行之后的结果如下: 代码: #include <i ...
- C++入门-控制台版的通讯录管理系统
通讯录管理系统 1.系统需求 通讯录是一个可以记录亲人.好友信息的工具. 本教程主要利用C++来实现一个通讯录管理系统 系统中需要实现的功能如下: 添加联系人:向通讯录中添加新人,信息包括(姓名.性别 ...
- C++通讯录管理系统(添加联系人,显示联系人,删除联系人,查找联系人,修改联系人,清空联系人,退出通讯录)
1 /** 2 * ProjectNmae:通讯录管理系统 3 * 功能: 4 * 添加联系人:向通讯录添加新人 5 * 显示联系人:显示通讯录中的所有联系人信息 6 * 删除联系人:按照姓名进行删除 ...
- c++—通讯录管理系统
一.运用所学的结构体.地址指针等基础知识,完成通讯录管理系统 二.系统主要有以下6个功能: 1.添加联系人2.显示联系人 3.删除联系人 4.查找联系人5.修改联系人 6.清空联系人 1 #inclu ...
- 通讯录管理系统(C语言)
/* * 对通讯录进行插入.删除.排序.查找.单个显示功能 */ #include <stdio.h> #include <malloc.h> #include <str ...
- C/C++编程笔记:C语言成绩管理系统!链式结构的管理系统源码分享
最近很多同学因为学校的要求,需要完成自己的那个C语言课程设计,于是就有很多人私信或者加我私聊我,问的最多的还是<学生成绩管理系统>,其实当你项目写多了你就会发现:其实各类的管理系统都离不开 ...
随机推荐
- JSP运行过程 JSP脚本 静态动态包含 jsp指令 jsp内置对象jsp四大作用域 jsp动作元素 EL表达式 JSTL 设计模式 JSP开发模式 EL内置对象
Day38 JSP JSP的运行过程具体如下: (1)客户端发出请求,请求访问JSP文件. (2)JSP容器先将JSP文件转换成一个Java源文件(Java Servlet源程序),在转换过程中,如果 ...
- scan函数用法详解
scan函数: scan(s,n,"char")表示从字串string中以char为分隔符提取第n个字串 功能(function):从字符表达式s中搜取给定的n个单词语法(synt ...
- java基础复习+大数运算
String: Array: 下面分别是大数加法,加法,乘法,取模
- DISC社交风格测试题--老虎 孔雀 考拉 猫头鹰
凭直觉,迅速回答 "我是谁",而不是"我应该是谁,或我想我是谁". 1.关于人生观,我的内心其实是: A 希望能够有尽量多的人生体验,所以会有非常多样化的想法. ...
- android M Launcher之LauncherModel (三)
通过前两篇的分析,我们已经知道了LauncherModel的初始化及工作流程,如果您还不熟悉的话请看前两篇博文 android M Launcher之LauncherModel (一) android ...
- 自定义View实现五子棋游戏
成功的路上一点也不拥挤,因为坚持的人太少了. ---简书上看到的一句话 未来请假三天顺带加上十一回家结婚,不得不说真是太坑了,去年婚假还有10天,今年一下子缩水到了3天,只能赶着十一办事了. 最近还在 ...
- Android布局概述
布局 布局定义用户界面的视觉结构,如Activity或应用小部件的 UI.您可以通过两种方式声明布局: 在 XML 中声明 UI 元素.Android 提供了对应于 View 类及其子类的简明 XML ...
- 从1....n中随机输出m个不重复的数
void knuth(int n, int m) { srand((unsigned) time( NULL)); for (int i = 0; i < n && m; i++ ...
- activiti 动态配置 activiti 监听引擎启动和初始化(高级源码篇)
1.1.1. 前言 用户故事:现在有这样一个需求,第一个需求:公司的开发环境,测试环境以及线上环境,我们使用的数据库是不一样的,我们必须能够任意的切换数据库进行测试和发布,对数据库连接字符串我们需要加 ...
- Asp.net 在刷新或提交页面后保持滚动条的位置
网页内容在较长时,每次回传刷新页面或提交网页时都会定位到最顶端,非常不利于用户交互. 将Page.MaintainScrollPositionOnPostBack属性值设置为true即可实现刷新后保持 ...