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语言课程设计任务书(4) 一、题目:通讯录管理 二、目的与要求 1. 目的: (1)基本掌握面向过程程序设计的基本思路和方法; (2)达到熟练掌握C语言的基本知识和技能; (3)能够利用所学的基本知识和技能,解决简单的程序设计问题 2. 要求 基本要求: 1.         要求利用C语言面向过程的编程思想来完成系统的设计; 2.       突出C语言的函数特征,以多个函数实现每一个子功能; 3.         画出功能模块图; 4.         具有清晰的程序流程图和数据结构的详细定义; 5.       熟练掌握C语言对文件的各种操作。 创新要求: 在基本要求达到后,可进行创新设计,如系统用户功能控制,对管理员级和一般级别的用户系统功能操作不同 三、信息描述 有关该系统基本信息的描述,如:姓名、电话、城市和邮编等。 四、功能描述 1.       名单基本信息(姓名,城市,电话,邮编等)的录入,并存放在文件当中。 2.       基本信息的查询与修改。 3.       记录的添加和删除。 4.       对同一类型记录的查找:如查找同一城市的记录或同一省份的记录。 五、解决方案 1.       分析程序的功能要求,划分程序功能模块。 2.       画出系统流程图。 3.       代码的编写。定义数据结构和各个功能子函数。 4.       程序的功能调试。 5.       完成系统总结报告以及使用说明书 六、进度安排 此次课程设计时间为一周或两周,分四个阶段完成: 1.       分析设计阶段。指导教师应积极引导学生自主学习和钻研问题,明确设计要求,找出实现方法,按照需求分析、总体设计、详细设计这几个步骤进行。 2.       编码调试阶段:根据设计分析方案编写C代码,然后调试该代码,实现课题要求的功能。 3.       总结报告阶段:总结设计工作,写出课程设计说明书,要求学生写出需求分析、总体设计、详细设计、编码、测试的步骤和内容。 4.       考核阶段。 七、撰写课程设计报告或课程设计总结 课程设计报告要求: 总结报告包括需求分析、总体设计、详细设计、编码(详细写出编程步骤)、测试的步骤和内容、课程设计总结、参考资料等,不符合以上要求者,则本次设计以不及格记。 八、参考资料  《C语言程序设计教程》   网上相关资料(....略)
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值