Phonebook management

1. Preface

In the study of C language, there is often a project to be completed, that is, the management of telephone book. After learning the basic content of playing C language, there is often no way to start in the face of so many project needs. This paper will complete the creation of telephone book step by step. This paper provides an idea. If there is a better implementation, welcome to discuss.

2. Function realization

The functions of this phonebook are
Initial creation of phone book
Post add of phonebook
Delete phonebook
View the phone book (sort by linked list)
Realize the search of telephone book (name and number)
Realize the sorting of phone book (name and number)

3. Train of thought analysis

3.1 the secondary directory realizes the initial creation of the phone book

To create a phone book, you need to know what information to save. This is to use our structure to create and save information

typedef struct phone_book
{
	char job[30];//Work information
	char number[20];//Telephone number
	char name[15];//Name Pinyin
	char email[30];//Mailbox information
	struct phone_book* next;//Point to next node
}phone_book;

Because the phone book is not limited to how large and changes dynamically, it is obviously inappropriate to use an array to save information. At this time, it needs to be realized by linked list.
When the phone book is run for the first time, there is no information in it. We need to create the phone book. We need three pointers to help us solve the problem. One of them is the head pointer of the linked list. The other two help us open up new nodes and point to the next node to continue our linked list.

	phone_book* head, * p1, * p2;
	head = NULL;//The header pointer is initialized to null
	p1 = (phone_book*)malloc(LEN);
	p2 = p1;

To complete the creation of the pointer, we need to enter the information. The following code helps us to create the contact. I don't want to enter one by one, so I use a loop to make it run until the name is entered 0. (I didn't think of any other good way to end it ~)
After each input of information, it is necessary to add it to the linked list. A variable is used to record the number of contacts. The first one is to get it into the head pointer, and the subsequent contacts are processed through two pointers.

while (1)
	{
		printf("Please enter a name (stop creation when the name is 0):"); gets_s(p1->name);
		if (strcmp(p1->name, "0") == 0) break;
		printf("Please enter the phone number:"); gets_s(p1->number);
		printf("Please enter the work unit:"); gets_s(p1->job);
		printf("Please enter E-mail:"); gets_s(p1->email);
		n = n + 1;
		if (n == 1)
			head = p1;
		else
			p2->next = p1;
		p2 = p1;
		p1 = (phone_book*)malloc(LEN);
	}p2->next = NULL;
	return head;

3.2 the secondary directory realizes the post addition of telephone book

After the first mock exam is built almost always with the creation of a module, it is only convenient to add them to the header pointer when the operation is done, so the list is not arranged in order of adding events. (header pointer yyds)

3.3 the secondary directory can delete the phone book

The deletion of contacts is somewhat difficult to understand, but it's also understandable. After finding this node, release it. Before that, mark the previous node and the next node to connect them. Directly on the code.
Through searching, we find that the node p to be deleted. If the person to be deleted happens to be the first, directly connect the header pointer to the back. If not, connect p1 to the node behind P and release P

        phone_book* p = head, * p1;//p1 is the previous node of p
		if (p == head)
		{
			head = p->next;
			free(p);
		}
		else
		{
			p1->next = p->next;
		    free(p);
		}

3.4 the secondary directory realizes the search of telephone book

You only need to compare information, but it can be realized by name and number, which requires three small functions. You can understand it by looking at the code

3.5 the secondary directory realizes the search of telephone book

You only need to compare information, but it can be realized by name and number, which requires three small functions. You can understand it by looking at the code

3.6 the secondary directory realizes the sorting of telephone books

You only need to compare information, but it can be realized by name and number, which requires three small functions. You can understand it by looking at the code

4. Source code and comments

#define _CRT_SECURE_NO_WARNINGS
#include<string.h> 
#include<stdio.h> 
#include<stdlib.h> 
#include<malloc.h> 
typedef struct phone_book
{
	char job[30];
	char number[20];
	char name[15];
	char email[30];
	struct phone_book* next;
}phone_book;
#define LEN sizeof(phone_book) 
phone_book* creat_list()
{
	phone_book* head, * p1, * p2;
	char name[15];
	int n = 0;
	head = NULL;
	p1 = (phone_book*)malloc(LEN);
	p2 = p1;
	while (1)
	{
		printf("Please enter a name (stop creation when the name is 0):"); gets_s(p1->name);
		if (strcmp(p1->name, "0") == 0) break;
		printf("Please enter the phone number:"); gets_s(p1->number);
		printf("Please enter the work unit:"); gets_s(p1->job);
		printf("Please enter E-mail:"); gets_s(p1->email);
		n = n + 1;
		if (n == 1)
			head = p1;
		else
			p2->next = p1;
		p2 = p1;
		p1 = (phone_book*)malloc(LEN);
	}p2->next = NULL;
	return head;
}//Linked list creation function 
void print_list(phone_book* head)
{
	void menu();
	int n = 0;
	printf("Now there are the following members in the address book:");
	while (head != NULL)
	{
		printf("\n name:"); puts(head->name);
		printf("\n Telephone number:"); puts(head->number);
		printf("\n Work unit:"); puts(head->job);
		printf("\nE-mail:"); puts(head->email);
		putchar('\n');
		head = head->next;
		n++;
		if (n % 8 == 0)
		{
			printf("Press enter to display the next page");
			getchar();
			system("cls");
			menu();
		}
	}
	printf("in total%d Contacts\n", n);
}
int length(phone_book* head)
{
	int n = 0;
	phone_book* p;
	p = head;
	while (p != NULL)
	{
		p = p->next; n++;
	}
	return n;
}//Judge the number of contacts
void sortbynumber(phone_book* head)
{
	void menu();
	int n = length(head);
	int i, j;
	phone_book temp, * p;
	p = head;
	phone_book a[1000];
	for (i = 1; i <= n; i++)
	{
		strcpy(a[i].name, p->name);
		strcpy(a[i].number, p->number);
		strcpy(a[i].job, p->job);
		strcpy(a[i].email, p->email);
		p = p->next;
	}
	for (i = 1; i <= n - 1; i++)
	{
		for (j = 1; j <= n - i; j++)
		{
			if (strcmp(a[j].number, a[j + 1].number) > 0)

			{
				temp = a[j];
				a[j] = a[j + 1];
				a[j + 1] = temp;
			}
		}
	}
	printf("Now there are the following members in the address book:");
	for (i = 1; i <= n; i++)
	{
		printf("\n name:"); puts(a[i].name);
		printf("\n Telephone number:"); puts(a[i].number);
		printf("\n Work unit:"); puts(a[i].job);
		printf("\nE-mail:"); puts(a[i].email);
		putchar('\n');
		if (i % 8 == 0)
		{
			printf("Press enter to display the next page");
			getchar();
			system("cls");
			menu();
		}
	}
	printf("in total%d Contacts\n", n);
}
void sortbyname(phone_book* head)
{
	void menu();
	int n = length(head);
	int i, j;
	phone_book temp, * p;
	p = head;
	phone_book a[100];
	for (i = 1; i <= n; i++)
	{
		strcpy(a[i].name, p->name);
		strcpy(a[i].number, p->number);
		strcpy(a[i].job, p->job);
		strcpy(a[i].email, p->email);
		p = p->next;
	}
	for (i = 1; i <= n - 1; i++)
	{
		for (j = 1; j <= n - i; j++)
		{
			if (strcmp(a[j].name, a[j + 1].name) > 0)
			{
				temp = a[j];
				a[j] = a[j + 1];
				a[j + 1] = temp;
			}
		}
	}
	printf("Now there are the following members in the address book:");
	for (i = 1; i <= n; i++)
	{
		printf("\n name:"); puts(a[i].name);
		printf("\n Telephone number:"); puts(a[i].number);
		printf("\n Work unit:"); puts(a[i].job);
		printf("\nE-mail:"); puts(a[i].email);
		putchar('\n');
		if (i % 8 == 0)
		{
			printf("Press enter to display the next page");
			getchar();
			system("cls");
			menu();
		}
	}
	printf("in total%d Contacts\n", n);
}//Sort by phone number
void sort_list(phone_book* head)
{
	void menu();
	if (head == NULL)
	{
		printf("Phone book is empty, please re create!");
		return;
	}
	char a;
	printf("1,Sort by phone number in ascending order\n2,Sort alphabetically\n");
	printf("Please select a sort method:");
	a = getchar();
	getchar();
	switch (a)
	{
	case'1':sortbynumber(head); break;
	case'2':sortbyname(head); break;
	default:printf("Incorrect input!\n"); break;
	}
}
void findbyname(phone_book* head)
{
	if (head == NULL)
	{
		printf("Phone book is empty, please re create"); return;
	}
	char name[15]; printf("Please enter a name:");
	gets_s(name);
	while (strcmp(name, head->name) != 0)
	{
		head = head->next;
		if (head == NULL)
		{
			printf("This contact does not exist in the phone book\n");
			return;
		}
	}
	printf("%s Your phone number is:", name);
	puts(head->number);
	printf("\n Work unit:");
	puts(head->job);
	printf("\nE-mail:");
	puts(head->email);
}
void findbynumber(phone_book* head)
{
	if (head == NULL)
	{
		printf("Phone book is empty, please re create");
		return;
	}
	char number[20];
	printf("Please enter the phone number:");
	gets_s(number);
	while (strcmp(number, head->number) != 0)
	{
		head = head->next;
		if (head == NULL)
		{
			printf("This contact does not exist in the phone book\n");
			return;
		}
	}
	printf("%s The owner is:", number);
	puts(head->name);
	printf("\n Work unit:");
	puts(head->job);
	printf("\nE-mail:");
	puts(head->email);
}
void find(phone_book* head)
{
	int n;
	printf("    1,Find by name\n    2,Find by phone number\n    3,sign out\n Please select the service you need:");
	scanf("%d", &n);
	getchar();
	while (1)
	{
		if (n == 1)
		{
			findbyname(head);
			printf("Please select a service item:");
			scanf("%d", &n);
			getchar();
		}
		if (n == 2)
		{
			findbynumber(head);
			printf("Please select a service item:");
			scanf("%d", &n);
			getchar();
		}
		if (n == 3)
			return;
		else
		{
			printf("Incorrect input!");
			printf("Please select a service item:");
			scanf("%d", &n);
			getchar();
		}
	}
}//find contact  
void add_list(phone_book* head)
{
	phone_book* p1, * p2, * h;
	h = NULL;
	char name[15];
	p1 = (phone_book*)malloc(LEN);
	p2 = p1;
	int n = 0;
	while (1)
	{
		printf("Please enter a name(Stop when name is 0)"); gets_s(p1->name);
		if (strcmp(p1->name, "0") == 0)break;
		printf("Please enter the phone number:"); gets_s(p1->number);
		printf("Please enter the work unit:"); gets_s(p1->job);
		printf("Please enter E-mail:"); gets_s(p1->email);
		n = n + 1;
		if (n == 1)
			h = p1;
		else
			p2->next = p1;
		p2 = p1;
		p1 = (phone_book*)malloc(LEN);
	}
	p1 = head->next;
	head->next = h;
	p2->next = p1;
}//Add linked list function 
phone_book* delete_list(phone_book* head)
{
	char a[20];
	printf("Please enter the contact name or phone number:");
	gets_s(a);
	if (head == NULL)
	{
		printf("Phone book is empty, please re create");  return head;
	}
	phone_book* p = head, * p1;
	p1 = NULL;
	while (strcmp(a, p->name) != 0 && strcmp(a, p->number) != 0)
	{
		p1 = p;
		p = p->next;
		if (p == NULL)
		{
			printf("This contact does not exist in the phone book\n");
			return head;
		}
	}
	printf("Found!");
	printf("\n name:"); puts(p->name);
	printf("\n Telephone number:"); puts(p->number);
	printf("\n Work unit:"); puts(p->job);
	printf("\nE-mail:"); puts(p->email);
	putchar('\n'); char b[10]; printf("Delete this contact(Y/N)");
	gets_s(b);
	if (strcmp(b, "y") == 0 || strcmp(b, "Y") == 0)
	{
		if (p == head)
		{
			head = p->next;
			free(p);
		}
		else
		{
			p1->next = p->next; free(p);
		}
		printf("Successfully deleted!");
	}
	else if (strcmp(b, "n") == 0 || strcmp(b, "N") == 0)
		printf("Cancel deletion!");
	else
		printf("Input error!");
	return head;
}//Delete linked list function 
void save_list(phone_book* head)
{
	FILE* fp;
	if ((fp = fopen("dianhuabu.dat", "wb")) == NULL)
	{
		printf("File cannot be opened\n");
		exit(0);
	}
	if (head == NULL)
	{
		printf("Address book is empty\n");
		return;
	}
	phone_book* p1 = head;
	while (p1 != NULL)
	{
		if (fwrite(p1, LEN, 1, fp) != 1)
		{
			printf("cannot open file\n");
			return;
		}
		p1 = p1->next;
	}
	printf("Save completed !\n"); fclose(fp);
}//File write function 
phone_book* load_list(phone_book* head)
{
	FILE* fp;
	if ((fp = fopen("dianhuabu.dat", "rb")) == NULL)
	{
		printf("Phone book is empty, please re create\n");
		exit(0);
	}phone_book* p1, * p2;
	p1 = (phone_book*)malloc(LEN);
	if (fread(p1, LEN, 1, fp) == 0)
	{
		printf("Phone book is empty, please re create");
		return head;
	}
	head = p1;
	p2 = p1; p1 = (phone_book*)malloc(LEN);
	while (fread(p1, LEN, 1, fp))
	{
		p2->next = p1;
		p2 = p1;
		p1 = (phone_book*)malloc(LEN);
	}
	p2->next = NULL;
	free(p1);
	return(head);
	fclose(fp);
}//File read function 
void menu()
{
	printf("           Welcome to the phonebook system            \n");
	printf("********************************************\n");
	printf("        1,Create phonebook(The original phone book will be overwritten)\n");
	printf("        2,find contact                \n");
	printf("        3,Add a Contact                \n");
	printf("        4,Delete Contact                \n");
	printf("        5,Show contacts               \n");
	printf("        6,View phone book(sort)         \n");
	printf("        7,Exit the system                 \n");
	printf("********************************************\n");
}//Menu interface 
void main()
{
	system("cls");
	menu();//Enter the menu interface  
	printf("          Please select the service you need:");
	int n;
	scanf("%d", &n);
	getchar();
	phone_book* head;
	head = NULL;
	while (1)
	{
		system("cls");
		menu();
		switch (n)
		{
		case 1:
		{
			head = creat_list();
			system("cls");
			menu();
			print_list(head);
			save_list(head);
			printf("********************************************\n");
			printf("\n If you need other services, please re-enter:");
			scanf("%d", &n);   getchar(); }break;//Create phone book (create linked list, write file, release linked list) 
		case 2:
		{
			head = load_list(head);
			find(head);
			printf("********************************************\n");
			printf("\n If you need other services, please re-enter:");
			scanf("%d", &n);   getchar(); }break;//Find contacts (read in files, find functions, release linked lists) 
		case 3:
		{
			head = load_list(head);
			add_list(head);
			system("cls");
			menu();
			save_list(head);
			printf("********************************************\n");
			printf("\n If you need other services, please re-enter:");
			scanf("%d", &n);   getchar(); }break;//Add contact (read file, add linked list, write file, release linked list) 
		case 4:
		{
			head = load_list(head);
			head = delete_list(head);
			save_list(head);
			printf("********************************************\n");
			printf("\n If you need other services, please re-enter:");
			scanf("%d", &n);
			getchar(); }break;//Delete contact (read in file, delete linked list, write file, release linked list)  
		case 5:
		{
			head = load_list(head);
			print_list(head);
			save_list(head);
		}
		case 6:
		{
			head = load_list(head);
			sort_list(head);
			printf("********************************************\n");
			printf("\n If you need other services, please re-enter:");
			scanf("%d", &n);   getchar(); }break;//View the phone book (read in the file, sort the linked list, release the linked list)   
		case 7:
		{   system("cls");   return; }break;//sign out  
		default:
		{
			printf("\n Incorrect input,Please re-enter:");
			scanf("%d", &n);
			getchar();
		}break;
		}
	}
}

5 Conclusion

The above is the complete process of phonebook management. Welcome to explore.

Keywords: C++ linked list Project

Added by lawnninja on Sat, 15 Jan 2022 09:16:25 +0200