c语言实训——通讯录管理系统

通讯录管理系统

通讯录信息包括:姓名、电话号码、分类(可选项有:A 办公类 B 个人类 C 商务类)、电子邮件,模拟手机通信录管理系统,实现对手机中的通信录进行管理。功能要求如下:
(1)查看功能:选择此功能时,列出下列三类选择:A 办公类 B 个人类 C 商务类 ,当选中某类时,显示出此类所有数据中的姓名和电话号码;
(2)增加功能:能录入新数据,当录入了重复的姓名和电话号码时,则提示数据录入重复并取消录入;当通信录中超过15条信息时,存储空间已满,不能再录入新数据;录入的新数据能按递增的顺序自动进行条目编号;
(3)修改功能:选中某个人的姓名时,可对此人的相应数据进行修改;
(4)删除功能:选中某个人的姓名时,可对此人的相应数据进行删除,并自动调整后续条目的编号;
(5)用菜单进行管理;
(6)只有正确输入用户名密码才能使用此系统;
(7)所有内容能够保存到文件中。下次进入系统是从文件中读取原有信息。

#include<stdio.h>
#include <string.h>
#include <stdlib.h>
#include<conio.h>
#define N 15 
struct person { 
	char name[10]; 
	char phone[11]; 
	char classify[10]; 
	char email[20]; 
} per[N]; 

char A[]={"office"}; 
char B[]={"personal"}; 
char C[]={"business"};
void menu();
int chcode() 
{
	char pw[50]; 
	char * syspw="abc"; 
	int m=0; 
	printf("请输入密码:\n"); 
	while(m<3) 
	{ 
		scanf("%s",pw); 
		if(strcmp(pw,syspw)!=0) 
		{ 
			printf("密码错误,请重新输入!\n"); 
			m++; 
		} 
		else 
		{ 
			printf("密码正确!\n"); 
			menu();
		} 
	} 
	printf("连续3次输入错误,退出!\n"); 
	system("pause"); 
	return 1;
}
int load()//**********加载函数**********/ 
/***********加载所有记录,并且可以返回所有记录的个数********/ 
{ 
	FILE *fp; 
	int i; 
	if((fp=fopen("d:\\person.txt","r"))==NULL) 
	{
		printf("\nCannot open file\n"); 
		return 0; 
	} 
		for(i=0;!feof(fp);i++) 
			fscanf(fp,"%s%s%s%s",&per[i].name,&per[i].phone,&per[i].classify,&per[i].email); 
		/*把fp指针指向的文件中的ascii对应的字符转换成%s的类型赋值给后面的变量*/
		fclose(fp); 	
		return(i);
} 
int search(char *name)
{
	int i,n;
	i=0;
	n=load();
	while(strcmp(per[i].name,name)!=0 &&i<n )
		i++;
	if(i<n) return i;
	return -1;
}
/***************查看功能**************/


void chakan() 
{ 
	void menu(); 
	int n,j,k=-1; 
	char p[10]; 
	n=load(); 
	printf("\n\nClassify A:office\nClassify B:single\nClassify C:business\n\n Enter classify that you want to search! Classify:"); 
	/*************输入要找的人的分类*********/ 
	scanf("%s",p); 
	for(j=0;j<n;j++) 
		if(strcmp(p,per[j].classify)==0) 
		{ 
			k=j; 
			printf("\n(%d).%s  %s  %s\n",j,per[j].name,per[j].phone,per[j].email); 
		} 
		if(k==-1) 
/**********如果要查找的分类不存在,则显示不存在**********/ { 
		printf("\n\nNO EXIST!"); 
	}
		menu(); 
} 
/**************增加功能*********/ 
void add()
{ 
	void menu();
	int n=0,j; 
	FILE *fp; 
	char name[10];
	char classify[10];
	char email[20]; 
	char phone[11]; 
	printf("\n\nThe name and phone of the person you want to add are:\n\n"); 
/******输入要增加人的姓名和电话号码******/
	scanf("%s%s",name,phone); 
	if((fp=fopen("d:\\person.txt","a+"))==NULL) 
	{ 
		printf("\n\nCan not open!\n\n"); 
	} 
		for(j=0;j<n;j++) 
		{ 
			if(strcmp(name,per[j].name)==0&&strcmp(phone,per[j].phone)==0)/*如果输入的姓名和电话已存在,则提示已存在*/ 
		{ 
			printf("\n\nThe message is exist!\n");break; 
		} 
		} 
			if(j==15) 
			{ 
				if(j>=15) 
					printf("\n\nThe room is full!\n\n");/*如果记录多余15条,则提示空间已满*/ 
			} 
		else 
			{ 
				printf("\nOK!Put the classify and email:\n"); 
/****如果输入的是新信息,则继续输入这个人的分类和电子邮件*****/ 
				scanf("%s%s",classify,email); 
				fprintf(fp,"\n%s %s %s %s",name,phone,classify,email); 
			} 
				fclose(fp); 
				menu(); 
} 
/****************修改功能************/
void xiugai() 
{ 
	void menu() ;
	int i,n,j,c; 
	char name[10];
	FILE *fp; 
	n=load(); 
	printf("\n\nModify by name:\n"); 
/*****输入要修改人的姓名******/ 
	scanf("%s",name); 
	if((fp=fopen("d:\\person.txt","r+"))==NULL) 
	{ 
		printf("\n\nCan not open!\n\n"); 
	} 
		for(j=0;j<n;j++) 
		if(strcmp(name,per[j].name)==0) 
		{ 
			do 
			{ 
				puts("\nModifyby=>\n\n1).name2).phone:3).classify:4).email:");
				printf("Whitch you needed?:[ ]\b\b"); 
/*******输入要修改的选项********/ 
				scanf("%d",&c); 
				if(c>4||c<1) 
				{ 
					puts("\nChioce error!Please again!"); 
					getchar(); 
				} 
			}while(c>4||c<1);
			break; 
		} 
	
		
			switch(c) 
			{ 
				case 1:printf("name");scanf("%s",per[j].name);break; 
				case 2:printf("phone");scanf("%s",per[j].phone);break; 
				case 3:printf("classify");scanf("%s",per[j].classify);break; 
				case 4:printf("email");scanf("%s",per[j].email);break; 
			} 
		

		if(j==n)
			printf("\n\nThe name you want is not exist!\n"); 
		for(i=0;i<n;i++) 
		fprintf(fp,"%s %s %s %s\n",per[i].name,per[i].phone,per[i].classify,per[i].email); 
		fclose(fp); 
		menu (); 
} 
/******************删除功能****************/ 
void shanchu() 
{ 
	void menu(); 
	int i,n; 
	FILE *fp; 
	char name[10]; 
	n=load(); 
	printf("\n\nDel by name:"); 
/******输入要删除人的姓名********/ 
	scanf("%s",name); 
	i=search(name);

	if(i>=0){
		while(i<n){
			per[i]=per[i+1];
			i++;
		}
		n--;
	}else {
		printf("%s not found!!!!!!\n",name);
	}


	if((fp=fopen("d:\\person.txt","w"))==NULL)
	{ 
		printf("\n\nCan not open!\n\n"); 
	} 
	for(i=0;i<n;i++) 
/********删除选项,并自动调整其他所有选项*************/ 
		fprintf(fp,"\n%s\t%s\t%s\t%s",per[i].name,per[i].phone,per[i].classify,per[i].email); 
	fclose(fp); 
	menu(); 
} 
/****************显示功能***************/
void xianshi() 
{ 
	void menu(); 

	int n,j; 
	n=load(); 
	for(j=0;j<n;j++) 
	{ 
		printf("\n(%d).%s\t%s\t%s\t%s",j,per[j].name,per[j].phone,per[j].classify,per[j].email); 
	} 
	printf("\n");
	menu(); 
} 
/***************菜单*****************/ 
void menu() 
{
	int n,w1;/*变量n保存选择菜单数字,w判断输入的数字是否在功能菜单对应数字范围内*/ 
		do 
	{ 
		puts("\t\t**********通讯录主界面**********\n\n"); 
		puts("\t\t\t\t 1. 查询成员资料");
		puts("\t\t\t\t 2. 增加一位成员资料"); 
		puts("\t\t\t\t 3. 修改一位成员资料"); 
		puts("\t\t\t\t 4. 删除一位成员资料"); 
		puts("\t\t\t\t 5. 成员总名单查看"); 
		puts("\t\t\t\t 6. 退出系统");
		puts("\n\n\t\t*********************************\n"); 
		printf("Choice your number(1-6):[ ]\b\b"); 
		scanf("%d",&n); 
		if(n<1||n>6) 
		{
			w1=1;
			getchar();
		} 
		else w1=0; 
	}
		while(w1==1); 
		switch(n) 
	{
		case 1:chakan();break; /*查看模块*/ 
		case 2:add();break; /*增加模块*/ 
		case 3:xiugai();break; /*修改模块*/ 
		case 4:shanchu();break; /*删除模块*/ 
		case 5:xianshi();break; /*显示模块*/ 
		case 6:exit(0); /*退出*/ 
	}
} 
/***************主函数****************/
main () 
{
chcode();
}

猜你喜欢

转载自blog.csdn.net/weixin_44517301/article/details/93214456