Implementation of C language address book

Posted by FramezArt on Fri, 14 Jan 2022 22:47:14 +0100

The whole member management system is designed as data addition module, data modification module, data deletion module, data search module by name or category, data browsing module, data emptying module and data storage module.

contact.h

// Header file 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#include <errno.h>
#define na 20
#define te 20
#define em 30
#define cate 20
#define first_size 3
enum Select
{
	Exit,
	Add,
	Dele,
	Find,
	Modify,
	Sort,
	Show,
	Destory, 
	Save,
};

typedef struct people
{
	char name[na];
	char tele[te];
	char category[cate];
	char email[em];
	
}people;

typedef struct num_con  //mail list
{
	people *data;//The structure array stores many people's information 
	int size;//Record the number of existing elements 
	int capa;//When the current maximum capacity capa ==size, the capacity is full 
}num_con;


void menu();

void check(num_con *ps);//Check whether capacity expansion is required 

int find_kind();//Select find type 

int find(num_con *ps,char name[na]);//Find name 

void Initialize_con(num_con *ps);//initialization

void add(num_con *ps);//increase 

void dele(num_con *ps);//delete

void findkind(num_con *ps);//Find by category 

void findname(num_con *ps);//Find by name

void modify(num_con *ps);//modify 
 
void sort(num_con *ps);//sort 

void show(const num_con *ps);//display 

void destory(num_con * ps);//empty 

void save(num_con *ps); //preservation 

void load(num_con *ps);//load 

main.c 

#include "contact.h"
/* test */

int main(int argc, char *argv[]) 
{	
	//Create address book 
	num_con con;
	
	Initialize_con(&con); //initialization 
	int input=0; 
	int i=0;
//Read data	
	read(&con);	

	do
	{
		menu();
		printf("Choose the duck quickly!\n");
		scanf("%d",&input);
		switch(input)
		{
			case Exit:
				save(&con);//Save data 
				destory(&con);//Destroy address book and free memory 
				system("cls"); 
				printf("Exit successful\n"); 
				break;
				
			case Add:
				add(&con);
				break;
				
			case Dele:
				dele(&con); 
				break;
				
			case Find:
				i=find_kind(); 
				if(i==1)
					findname(&con);
				else if(i==2) 				
					findkind(&con);
				break;
				
			case Modify:
				modify(&con);
				break;
				
			case Sort:
				sort(&con);
				break;
				
			case Show:
				show(&con);
				break;	
			
			case Destory:
				destory(&con);
				break;
				
			case Save:
				save(&con);
				break; 
				
			default:
				printf("Input error, please re-enter!\n");
				break; 
		}
		
	}while(input); 

	return 0;
}

contact.c

//function 
#include "contact.h"

void menu ()
{
	printf("-------—\n");
	printf("************mail list************\n");
	printf("***0.sign out************1.increase***\n");
	printf("***2.delete************3.lookup***\n");
	printf("***4.modify************5.sort***\n");
	printf("***6.display************7.empty***\n");
	printf("**********8.Save file**********\n");
	printf("******************************\n");	
}


void Initialize_con(num_con *ps)//initialization 
{
	ps->data=(people *)malloc(first_size*sizeof(people));//Dynamic initialization space 
	if(ps->data==NULL)
		return;
	ps->size=0;
	ps->capa=first_size; 
	
	//Load the information stored in the file into the address book
	load(ps); 

}
 

void check(num_con *ps)//Check whether capacity expansion is required 
{
	if(ps->size==ps->capa)
	{
		//increase capacity
		//The temporary pointer stores the space after increasing the capacity 
		people * newcapa=(people *)realloc(ps->data,(ps->capa+1)*sizeof(people)); 
		if(newcapa!=NULL)//Successful expansion 
		{	
			ps->data==newcapa; 
			ps->capa+=1;//Capacity expansion 
			printf("Expansion succeeded\n"); 
		} 
		else
			printf("Ouch, the expansion failed\n"); 
	}
}


//Select find type 
int find_kind()
{
	int i=0;
	while(1)
	{
		printf("Please select a search type\n1.By name\t2.By type\n");
		scanf("%d",&i);
		if(i==1||i==2)
		{
			return i;
		
		}
		else
		{
			printf("Input error, please re-enter\n"); 
		}
	}
	
} 


//Find name
int find(num_con *ps,char name[na])
{
	int i=0;
	for(i=0;i<ps->size;i++)
	{
		
		if(strcmp(name,ps->data[i].name)==0)
			return i;
		
	}
	return -1;
} 

//add to 
void add(num_con *ps)
{
	check(ps);//Check current address book capacity
	char a[cate];
	int b=1;
	//Add a person's data 
		printf("name:>\n");
		scanf("%s",ps->data[ps->size].name);//data array subscript = = size 
		printf("Telephone:>\n");
		scanf("%s",ps->data[ps->size].tele);
		
		do 
		{
		printf("category:Please input friends, colleagues, business and general information accurately:>\n");
		scanf("%s",a);
		getchar();//Accept carriage return 
		if(strcmp(a,"friend")==0||
		   strcmp(a,"colleague")==0||
		   strcmp(a,"business affairs")==0||
		   strcmp(a,"commonly")==0)
		{ 
			b=0;
			strcpy(ps->data[ps->size].category,a);
		}
		else
		{
			printf("Input error, please re-enter\n");
		}
		
		}while(b);
		
		printf("address:>\n");
		scanf("%s",ps->data[ps->size].email);

		ps->size++;
		printf("Yeah! Successfully added!\n"); 

}


//delete 
void dele(num_con *ps)
{
	printf("Who will delete it?\n");
	char a[na];
	scanf("%s",a);
	
	//Look for a name 
	int result=find(ps,a);
	if(result==-1)
	{
		printf("I can't find it. Woo woo\n"); 		
	} 
	else
	{		
		int j=0;
		for(j=result;j<ps->size-1;j++)
		{
			ps->data[j]=ps->data[j+1];	
			 
		}
		ps->size--;
		printf("Delete succeeded!\n");
	}
} 

//Find by category 
void findkind(num_con *ps)
{
	int i=0,t=0;
	char a[cate]; 
	printf("Find by category:\n Please enter:>Friends, colleagues, business, general:>\n");
	scanf("%s",a);

	printf("Looking for...\n");
	printf("----------\n"); 
	for(i=0;i<ps->size;i++)
	{

			if(strcmp(ps->data[i].category,a)==0)
			{
				t=1;
				printf("%-10s\t%-10s\t%-10s\t%-10s\n","name","Telephone","category","address"); 
		
			printf("%-10s\t%-10s\t%-10s\t%-10s\n",
			ps->data[i].name,
			ps->data[i].tele,
			ps->data[i].category,
			ps->data[i].email);	
			}
	}
	if(t==0)
	{
		printf("I can't find it. Woo woo\n"); 
	
	}

	
}


//Find by name
void findname(num_con *ps)
{
	int i=0;
	if(0==ps->size)
	{
		printf("The address book is empty and cannot be found\n");
		return;
	}
	char a[na]; 
	printf("Find by name:\n Please enter:>\n"); 
	scanf("%s",a);
	getchar();//Accept carriage return 
	printf("Looking for...\n");
	printf("----------\n");
	int result=find(ps,a);
	if(result==-1)
	{
		printf("I can't find it. Woo woo\n"); 		
	} 
	else
	{
		printf("%-10s\t%-10s\t%-10s\t%-10s\n","name","Telephone","category","address"); 
		printf("%-10s\t%-10s\t%-10s\t%-10s\n",
			ps->data[result].name,
			ps->data[result].tele,
			ps->data[result].category,
			ps->data[result].email);
	}	
}


//modify 
void modify(num_con *ps)
{
	int i=0;
	if(0==ps->size)
	{
		printf("The address book is empty and cannot be modified\n");
		return;
	}
	
	char a[na];
	printf("Please enter the name of the person to modify\n");
	scanf("%s",a);
	
	//Looking for a name 
	int result=find(ps,a);
	if(result==-1)
	{
		printf("I can't find it. Woo woo\n"); 		
	} 
	else
	{
		printf("Found\n Please enter the modified content:>\n");
		printf("Change name to:>\n");
		scanf("%s",ps->data[result].name);
		printf("The telephone number is modified to:>\n");
		scanf("%s",ps->data[result].tele);
		printf("Category changed to:>\n");
		scanf("%s",ps->data[result].category);
		printf("Mailbox is modified to:>\n"); 
		scanf("%s",ps->data[result].email); 
		printf("Modification succeeded!\n");
	}
	
	
}


//sort
void sort(num_con *ps)
{
	int i=0,j=0;
	people peo;
	printf("Sorting by name:>\n");
	for(i=0;i<ps->size;i++)
	{
		for(j=i+1;j<ps->size;j++)
		if(strcmp(ps->data[i].name,ps->data[j].name)>=0)
		{
			peo=ps->data[i];
			ps->data[i]=ps->data[j];
			ps->data[j]=peo;
		}
	}
	printf("Line up! Want to see it~\n");
}
 
 
//display 
void show(const num_con *ps)
{
	int i=0;
	if(ps->size==0)
		printf("Address book is empty\n");
	else
	{
		printf("%-10s\t%-10s\t%-10s\t%-10s\n","name","Telephone","category","address"); 
		for(i=0;i<ps->size;i++)
		{
			printf("%-10s\t%-10s\t%-10s\t%-10s\n",
			ps->data[i].name,
			ps->data[i].tele,
			ps->data[i].category,
			ps->data[i].email);	
		}	
	}
}


//empty 
void destory(num_con * ps)
{
	free(ps->data);
	ps->data=NULL;	
	ps->size=0;
	system("cls");
	printf("Has been emptied\n");
		
}

//load 
void load(num_con *ps)
{
	
	people t={0};//Create temporary variables for member information 
	FILE*pf=fopen("CONTACT","rb");
	if(pf==NULL)
	{
		printf("load::%s\n",strerror(errno));//error message
		return; 
	}
	//read file
	while(fread(&t,sizeof(people),1,pf))//If 0 is read, the reading is completed 
	{
		check(ps);//Check capacity
		ps->data[ps->size]=t;
		ps->size++; 
	}
	fclose(pf); 
	pf=NULL;
}


void save(num_con *ps)
{
	int i=0;
	FILE* pf=fopen("CONTACT","wb");
	if(pf==NULL)
	{
		printf("save::%s\n",strerror(errno));//error message 
		return ;
	}
	
	//write file
	for(i=0;i<ps->size;i++)
	{
		fwrite(&(ps->data[i]),sizeof(people),1,pf);

	}
	fclose(pf);
	pf=NULL; 
	system("cls"); 
	printf("Saved successfully\n"); 
 }

It is strongly recommended that you debug in time after typing a section of function, so as not to make the error difficult to find. It's better to have a function and a file.

Finally, the course design report is attached, hoping to be helpful to you.

1. Introduction to course design

Using computer to manage the address book member information has the advantage that manual management can not achieve. It has the advantages of rapid search, convenient search, convenient modification, large storage capacity and low cost. Saving the address book in the computer is not only very convenient, but also can update the data in time to prevent data loss.

In this address book, you can classify contacts into colleagues, friends, business and general categories. After grouping, you can add, delete, modify and query. When users need to query contacts, they can query contacts directly by search name or by grouping. The system improves the query efficiency and provides more convenience for users.

2. Demand analysis

1. Address book record file storage shall provide file input and output function. When running the program, the address book data shall be read from the file to the memory;

2. Realize the deletion of member information, so as to provide the modification and deletion of files, and save the modified information to the file;

3. To browse all member records, display operation is provided;

4. The output information should be sorted by name, so sorting operation is provided;

5. To clear the address book information, you need to clear it;

6. In addition, visual menu and function selection shall be provided.

3. Overall design

The whole member management system is designed as data addition module, data modification module, data deletion module, data search module, data browsing module, data emptying module and data saving module

 

4. Detailed design

The data structure adopts the structure, designs the member information record structure, and creates a new name people for struct people:

typedef struct people

{

char name[na];

char tele[te];

char category[cate];

char email[em];

}people;

The selection function of the main function menu is realized by switch, and enumeration is used to remind the programmer of the user-defined function corresponding to each number.

enum Select

{

Exit,

Add,

Dele,

Find,

Modify,

Sort,

Show,

Destory,

Save,

};

(1) Main function output prompt menu

Add member record, modify member record, delete member record, find member record, browse member record, clear

Empty member record.

(2) Check capacity module

Check whether the capacity needs to be expanded. If the capacity is insufficient, use realloc to increase the space.

(3) Select find category module

Select by what category, by name, or by category through the menu.

(4) Find name module

For the search and modification of address book, you can use this function to find whether there is a member, so as to save the amount of code.

(5) Data addition module

Select add member record from the menu.

(6) Data modification module

Select Modify member record from the menu.

(7) Data deletion module

Select Delete member record from the menu.

(8) Data search module

Select find member record from the menu, by name or by type.

(9) Data sorting module

Sort by name string size through menu selection.  

  

(10) Data display module

Display member records, newly entered and information in the file through menu selection.      

(11) Data clearing module

Select Clear member information from the menu.

(12) File reading module

Open the CONTACT file with fopen and read the CONTACT file with fread. Put the module into the function of initializing the structure array, and then open the program to use the display function to load the member information.

(13) File saving module

Write the member information into the CONTACT file with fwrite. You can select the save function through the menu or automatically save when exiting the function.

Create an address book structure array, including the following contents.

typedef struct num_con  //mail list

{

people *data;//The structure array stores many people's information

int size;//Record the number of existing elements

int capa;//When the current maximum capacity capa ==size, the capacity is full

}num_con;

Thanks for watching! Leave a praise before you go! I wish you a happy life!

Topics: C