c Language Training - Address Book Management System

Address book management system

Address book information includes: name, phone number, category (options are: A personal office Class B Class C business class), e-mail, analog mobile phone address book management system, mobile phone address book to manage. Functional requirements are as follows:
(1) View function: When you select this function, lists the following three options are: A personal office Class B Class C business class, when elected in a class, showing all of these data in the name and phone number ;
(2) an increase function: to enter new data, when entering a name and phone number of repeat, the repeat data entry prompts and cancel entry; when the address book information 15 exceeds the storage space is full and can no longer enter new data; new data entry can be performed automatically in ascending order entry number;
(3) modify the function: when you select a person's name, the corresponding data this person may be modified;
(4) delete function: select someone when the name, the corresponding data this person may be deleted, and automatically adjusts the number of subsequent entries;
(5) be managed with the menu;
(6) only enter the correct username and password to use this system;
(7) All content can be saved to a file. Next time you enter system information is read from the original file.

#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();
}

Guess you like

Origin blog.csdn.net/weixin_44517301/article/details/93214456